'Selenium with Python(POM)

I am new in automation testing. I am learning automation using selenium with python(page object model). From learning YouTube I see that, log in is done for every test case, which is redundant. I would like to login once and execute for multiple test case. This is a sample code.

tests/test_dashboard.py
class TestDashboard:
     def test_dashboard(self, setup):
        self.driver = setup
        self.driver.get(self.base_url)
        self.lp = Login(self.driver)
        self.lp.set_email(self.user_email)
        self.lp.set_password(self.user_password)
        self.lp.sign_in()
   def test_dashboard_checking_fire(self, setup):
        self.driver = setup
        self.driver.get(self.base_url)
        # self.driver.maximize_window()
        self.lp = Login(self.driver)
        self.lp.set_email(self.user_email)
        self.lp.set_password(self.user_password)
        self.lp.sign_in()

You see, for 2 teset cases i have to execute log in everytime. How I can login once and execute that 2 test cases at once? I am using pytest framework.How to do this with pytest. There is a setup method.

tests/conftest.py
@pytest.fixture()
def setup(browser):
    if browser == "chrome":
        driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
    elif browser == "firefox":
        driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))
    else:
        driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()))
    return driver


Solution 1:[1]

Finally, I solve my Problem. I update my setup function like this:

@pytest.fixture(scope="class", autouse=True)
def setup(request, browser, url):
    global driver
    # my code
    request.cls.driver = driver
    yield
    driver.close()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1