Filtering by tags in PyTest-BDD

HOME

@Login
Feature: Login Page

    @ValidCredentials
    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"

@ScenarioOutline
Feature: Scenario Outline Example

    @InvalidCredentials
    Scenario Outline: Invalid credentials generate error message
        Given User is on OrangeHome page
        When User enters username as "<username>" and password as "<password>"
        Then User should be able to see error message as "Invalid credentials"

      Examples:
       | username | password    |
       | admin    | admin12345  |
       | Admin123 | admin       |
       | 123      | admin       |


    @MissingUsername
    Scenario: Missing Username generates error
        Given User is on OrangeHome page
        When User enters username as " " and password as "admin123"
        Then User should be able to see error message as "Required" below the username field

pytest

[pytest]
markers =
    Login: mark all tests as login
    ValidCredentials: mark a test as a valid credentials (deselect with '-m "not ValidCredentials"')
	ScenarioOutline: mark all tests as ScenarioOutline
	InvalidCredentials: mark test as invalid credentials (deselect with '-m "not InvalidCredentials"')
	MissingUsername: mark test as missing username (deselect with '-m "not MissingUsername"')

 pytest -k "MissingUsername"

pytest -k  "Login"

pytest -m "ScenarioOutline and MissingUsername"

pytest -m "ValidCredentials or MissingUsername"

 pytest -m "not InvalidCredentials"

Scenario Outline in PyTest – BDD

HOME

pip install pytest
pip install pytest-bdd
pip install pytest-selenium (If using Selenium)

Feature: Scenario Outline Example

    @InvalidCredentials
    Scenario Outline: Unsuccessful Application Login
        Given User is on OrangeHome page
        When User enters username as "<username>" and password as "<password>"
        Then User should be able to see error message as "Invalid credentials"

      Examples:
       | username | password    |
       | admin    | admin12345  |
       | Admin123 | admin       |
       | 123      | admin       |

import pytest

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


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


# Scenarios
scenarios('../features/ScenarioOutline.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 OrangeHome page')
def open_browser(driver):
    driver.get(URL)


# When Steps
@when(parsers.parse('User enters username as "{username}" and password as "{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 see error message as "{expected_error_message}"'))
def search_results(driver, expected_error_message):
    actual_error_message = driver.find_element(By.XPATH, "//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/div/div[1]/div[1]/p").text
    assert actual_error_message == expected_error_message

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

pytest stepdefinitions/test_scenario_outline_definitions.py -s

 pytest stepdefinitions/test_scenario_outline_definitions.py --html=Report.html 

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

PyTest Framework

HOME