Cross Browser Testing in PyTest

HOME

import pytest
from selenium import webdriver


def pytest_addoption(parser):
    """Custom Pytest command line options"""
    parser.addoption(
        "--browser_name", action="store", default="chrome"
    )


@pytest.fixture(scope="class")
def driver(request):

    browser_name = request.config.getoption("browser_name")

    if browser_name == "chrome":
        options = webdriver.ChromeOptions()
        driver = webdriver.Chrome(options)
        print("Chrome Browser is opened")
    elif browser_name == "firefox":
        options = webdriver.FirefoxOptions()
        driver = webdriver.Firefox(options)
        print("Firefox Browser is opened")
    elif browser_name == "edge":
        options = webdriver.EdgeOptions()
        driver = webdriver.Edge(options)
        print("Edge Browser is opened")
    else:
        print("No browser is selected")
    driver.get("https://opensource-demo.orangehrmlive.com/")
    driver.maximize_window()
    driver.implicitly_wait(5)
    yield driver
    driver.close()

from selenium.webdriver.common.by import By


class Test_Login:

    def test_login(self,driver):
        driver.find_element(By.NAME, "username").send_keys("Admin")
        driver.find_element(By.NAME, "password").send_keys("admin123")
        driver.find_element(By.XPATH, "//*[@class='oxd-form']/div[3]/button").click()
        print("Successful login to the application")
        homePageTitle = driver.find_element(By.XPATH, "//*[@class='oxd-topbar-header-breadcrumb']/h6").text
        print("Heading of DashBoard Page: ", homePageTitle)
        assert homePageTitle == "Dashboard", "Heading is different"

pytest test_login.py -s

 pytest test_login.py --browser_name firefox -s

Leave a comment