0

I am learning automated testing, and when testing login scenarios, it shows me this error:

pytest_bdd.exceptions.StepDefinitionNotFoundError: Step definition is not found: Given "I am on the login page". Line 5 in scenario "OK Login" in the feature "C:\Users\maraj\Documents\Python\BeeIT\Automatizované testování\Moje testy\OrangeHRM\test_cases\features\login.feature"

.venv_python_tests\lib\site-packages\pytest_bdd\scenario.py:192: StepDefinitionNotFoundError

pytest_bdd.exceptions.StepDefinitionNotFoundError: Step definition is not found: Given "I am on the login page". Line 11 in scenario "NON OK Login" in the feature "C:\Users\maraj\Documents\Python\BeeIT\Automatizované testování\Moje testy\OrangeHRM\test_cases\features\login.feature"

.venv_python_tests\lib\site-packages\pytest_bdd\scenario.py:192: StepDefinitionNotFoundError

Does anyone know why this error occurred? Here is my code:

login.feature

@allure.feature 
Feature: Login

    Scenario: OK Login
    Given I am on the login page
    When I enter valid credentials
    And I press on the login button #ve stepech používáme @when. Tady je to, aby se to líp četlo
    Then I see that I am logged in

    Scenario: NON OK Login
    Given I am on the login page # ten už ve stepech mám, tudíž ho nemusím psát znovu
    When I enter invalid credentials
    And I press on the login button # ten už ve stepech mám taky, tudíž ho nepíšu
    Then I see error message

login_steps.py

from pytest_bdd import given, when, then, scenarios
from selenium import webdriver 
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager 
from selenium.webdriver.common.by import By
import pytest

scenarios("../features/login.feature") 

@pytest.fixture
def setup():

    global driver 
    options = Options() 
    options.add_argument("start-maximized")
    #options.add_experimental_option("excludeSwitches", ["enable-automation"])
    #options.add_experimental_option("excludeSwitches", ["enable-logging"])
    #options.add_experimental_option("useAutomationExtension", False)
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) #inicializace Chromedriveru
    driver.implicitly_wait(10)

    yield driver
    driver.quit()

    @pytest.fixture
    @given("I am on the login page", target_fixture="I_am_on_login") 
                                     
    def I_am_on_login(setup): 
        driver = setup                           
        driver.get("https://opensource-demo.orangehrmlive.com/")
                             
    @when("I enter valid credentials")
    def enter_correct_username_and_pwd():
        driver.find_element(By.NAME, "username").clear()
        driver.find_element(By.NAME, "username").send_keys("Admin") 

        driver.find_element(By.NAME, "password").clear()
        driver.find_element(By.NAME, "password").send_keys("admin123")

    @when("I press on the login button")
    def click_on_login():
        
        driver.find_element(By.XPATH, "/html/body/div/div[1]/div/div[1]/div/div[2]/div[2]/form/div[3]/button").click()

    @then("I see that I am logged in")
    def verify_ok_login():
        title_text = driver.find_element(By.CLASS_NAME, "oxd-topbar-header-breadcrumb").text
        if title_text == "Dashboard":
            assert True

        else:
            print("Test Failed!")
            assert False

    @when("I enter invalid credentials")
    def enter_incorrect_username_and_pwd():
        driver.find_element(By.NAME, "username").clear() 
        driver.find_element(By.NAME, "username").send_keys("Admi") 

        driver.find_element(By.NAME, "password").clear()
        driver.find_element(By.NAME, "password").send_keys("admi123")

    @then("I see error message")
    def verify_non_ok_login():
        
        get_error_message = driver.find_element(By.CLASS_NAME, "oxd-text oxd-text--p oxd-alert-content-text").text
        
        if get_error_message == "Invalid credentials":
            assert True

        else:
            print(get_error_message)
            print("Test Failed!")
            assert False

Here is the link to my GitHub project: https://github.com/kickerzit/OrangeHRM

Thank you for any help.

I have upgraded both BDD and Pytest or tried to delete one of the fixtures, but the same problem. I'm a beginner of that.

Mara
  • 1
  • 1

0 Answers0