Home > OS >  Browser doesn't close after running first test data to run second test data
Browser doesn't close after running first test data to run second test data

Time:12-20

I tried to run a data-based test where after each data, the browser needs to be closed, but it does not close. It takes the data from the second line also but the browser does not get closed to run the second test case.

The code is as follows:

import pytest
import softest
from ddt import ddt, data, unpack, file_data
from pages.homepage import Homepage
from pages.register import RegisterPage
    
from utilities.utils import Utils
    
    
@pytest.mark.usefixtures("setup")
@ddt
class TestRegister(softest.TestCase):
    log = Utils.custom_logger()

    @data(*Utils.read_data_from_excel("C:\\Users\\Bishal karki\\PycharmProjects\\Bizbazar\\testdata\\Register.xlsx","Sheet1"))
    @unpack
        
    def test_register(self,email,pass1,pass2,ftname,ltname,phnumber1,city,address,state,zipcode,phnumber2,testno):
        driver = self.driver
        hp = Homepage(driver)
        hp.form_location()
        rp = RegisterPage(driver)
        rp.registraionpage(email,pass1,pass2,ftname,ltname,phnumber1,city,address,state,zipcode,phnumber2,testno)

The code that is handling the browser is:

import pytest
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
    
    
@pytest.fixture(scope="class")
def setup(request, browser, url):
    if browser == "firefox":
        driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
    elif browser == "chrome":
        driver = webdriver.Chrome(ChromeDriverManager().install())
    elif browser == "edge":
        driver = webdriver.Edge(EdgeChromiumDriverManager().install())
    print("Browser Opened")
    driver.maximize_window()
    driver.get(url)
    request.cls.driver = driver
    yield
    driver.close()
    print('Browser Closed')   
    
    
def pytest_addoption(parser):
    parser.addoption("--browser")
    parser.addoption("--url")
    
@pytest.fixture(scope="class", autouse=True)
def browser(request):
    return request.config.getoption("--browser")
   
@pytest.fixture(scope="class", autouse=True)
def url(request):
    return request.config.getoption("--url")

CodePudding user response:

The answer has been given as As I have been using a class scope fixture, this is the expected and correct behavior

If I want per test teardown, I have to consider the function scope, which is the default

  • Related