pythonseleniumselenium-webdriverpytestconftest

how to launch seperate browser for each test in python selenium


while running test_login.py , 3 browsers should be launched and verify the tests but only one is getting launched and second test is failing as second is continuing after first test steps.

*********** Conftest.py********** below is conftest file

import pytest
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

    @pytest.fixture(scope="class")
    def setup(request):
        driver = webdriver.Chrome(ChromeDriverManager().install())
        driver.get("https://itera-qa.azurewebsites.net/Login")
        driver.maximize_window()
        request.cls.driver = driver
    
        yield
        driver.close()

test_login.py this test file contains 3 tests to verify

import pytest
from pages.loginpage import PageLogin


@pytest.mark.usefixtures("setup")
class TestLogin:

    @pytest.fixture(autouse=True)
    def class_setup(self):
        self.tl = PageLogin(self.driver)
        

    @pytest.mark.login
    def test_login1(self):
        self.tl.enter_user_name("sysadm")
        self.tl.enter_password("sysadm")
        self.tl.click_login()

    

    def test_empty_user_pwd(self):
        # self.driver.get(self.url)
        self.tl.enter_user_name("")
        self.tl.enter_password("")
        self.tl.click_login()
        if self.tl.error_text() == 'Login error: Enter your username.':
            assert True
        else:
            assert False

    def test_empty_pwd(self):
        # self.driver.get(self.url)
        self.tl.enter_user_name("sysadm")
        self.tl.enter_password("")
        self.tl.click_login()
        if self.tl.error_text() == 'Login error: Enter your password.':
            assert True
        else:
            assert False

********* PageLogin*********** this is loginpage where all the xpaths were defined

import time

from selenium.webdriver.common.by import By


class PageLogin(SelectCustomer):
    textbox_username_id = "user"
    textbox_password_id = "pass"
    button_login_xpath = "//input[@value='Log In']"
    get_error_text_xpath = "//p[@class='error']"

    def __init__(self, driver):
        self.driver = driver

    def enter_user_name(self, username):
        self.driver.find_element(By.ID, self.textbox_username_id).send_keys(username)

    def enter_password(self, password):
        self.driver.find_element(By.ID, self.textbox_password_id).send_keys(password)

    def click_login(self):
        self.driver.find_element(By.XPATH, self.button_login_xpath).click()

    def error_text(self):
        returned_error_text = self.driver.find_element(By.XPATH, self.get_error_text_xpath).text
        return returned_error_text
        time.sleep(4)

Solution

  • Issue resolved after changing code in conftest.py below code: able to launch separate browser for each test case

    import pytest
    from selenium import webdriver
    from webdriver_manager.chrome import ChromeDriverManager
    from webdriver_manager.firefox import GeckoDriverManager
    from webdriver_manager.microsoft import EdgeChromiumDriverManager
    driver = None
    
    
    @pytest.fixture(autouse=True)
    def setup(request, browser, url):
        global driver
        if browser == "chrome":
            driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
        elif browser == "firefox":
            driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
        elif browser == "edge":
            driver = webdriver.Edge(EdgeChromiumDriverManager().install())
        driver.get(url)
        driver.maximize_window()
        request.cls.driver = driver
        yield
        driver.close()
    
    
    #this will get the value of CLI/Hooks
    def pytest_addoption(parser):
        parser.addoption("--browser")
        parser.addoption("--url")
    
    
    #This will return the browser value to setup method
    @pytest.fixture(scope="class", autouse=True)
    def browser(request):
        return request.config.getoption("--browser")
    
    
    #This will return the url value to setup method
    @pytest.fixture(scope="class", autouse=True)
    def url(request):
        return request.config.getoption("--url")