PyTest – BDD (Behavioural Driven Development) with Selenium

HOME

pip install pytest
pip install pytest-bdd

pip install pytest-selenium

Feature: Login Page

    Scenario: Successful Application Login
        Given User is on login page
        When User enter username "Admin" and password "admin123"
        Then User should be able to login successfully and new page open "Dashboard"

import pytest

from pytest_bdd import scenarios, given, when, then, parsers
from selenium import webdriver
from selenium.webdriver.common.by import By

# Constants
TIMEOUT = 4
URL = 'https://opensource-demo.orangehrmlive.com/'


# Scenarios
scenarios('../features/Login.feature')


# Fixtures
@pytest.fixture
def driver():
    driver = webdriver.Firefox()
    driver.implicitly_wait(TIMEOUT)
    driver.maximize_window()
    yield driver
    driver.quit()


# Given Steps
@given('User is on login page')
def open_browser(driver):
    driver.get(URL)


# When Steps
@when(parsers.parse('User enter username "{username}" and password "{password}"'))
def search_phrase(driver, username, password):
    driver.find_element(By.NAME, "username").send_keys(username)
    driver.find_element(By.NAME, "password").send_keys(password)
    driver.find_element(By.XPATH, "//*[@class='oxd-form']/div[3]/button").click()


# Then Steps
@then(parsers.parse('User should be able to login successfully and new page open "{heading}"'))
def search_results(driver, heading):
    homepage_title = driver.find_element(By.XPATH, "//*[@class='oxd-topbar-header-breadcrumb']/h6").text
    assert homepage_title == heading

scenarios('../features/Login.feature')

pytest tests

pytest tests --html=Report.html -s

Leave a comment