I want to optimize code in a way that functions related to homepage should run once at first time in python selenium

40 Views Asked by At

TestData file:

class LoginData:
    test_login_data = [{"phone": "3494797711"}, {"phone": "54646"}, {"phone": "357657"}]

Testcase file:

import pytest
from selenium import webdriver
from time import sleep
from webdriver_manager.chrome import ChromeDriverManager
from TestData.LoginData import LoginData
from pageobject.HomePage import HomePage
from pageobject.LoginPage import LoginPage
from utilities.BaseClass import BaseClass

@pytest.fixture(params=LoginData.test_login_data)
def getData(request):
    return request.param

class TestLogin(BaseClass):
    def test_login(self, getData):
        homepage = HomePage(self.driver)
        loginpage = LoginPage(self.driver)
        loginpage.getNumber().send_keys(getData["phone"])
        sleep(5)
        self.driver.refresh()

In the test case file, self.closePopup() and homepage.ClickLogin() belongs to homepage and I want to run them only first execution of testcase. how can i do that?

1

There are 1 best solutions below

0
Qeyzho On

If I understand your question, this codes help you save cookies and logging information.

import json
#Create a cookies folder 
#by selenium login to your account and go to home page and run below code.

Path('cookies/cookies.json').write_text(json.dumps(driver.get_cookies(), indent=2))

for next times run this codes:

driver.get(url) #url of homepage
for cookie in json.loads(Path('__cookies/cookies.json').read_text()):
    driver.add_cookie(cookie)
driver.get(url) #reload