Home > database >  How do I pass the driver object around?
How do I pass the driver object around?

Time:06-08

Updated to reflect the current issues!


I'm very very new to Python and am really struggling to structure my tests according to page object model (something which I can do in C#). I just can't figure out how to pass the driver object / if I'm even trying to solve the right problem! Please help.

For reference, here is the test file and the login page. When the test runs, it launches the browser then immediately fails because it can't find the emailField object.

"File "c:\Users\079382\Downloads\QA Technical Task 1\Grain Supplier Solution\grains\tests\tests.py", line 19, in loginPage.emailField.send_keys("[email protected]") AttributeError: 'LoginPage' object has no attribute 'emailField'"

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from pages.adminpage import AdminPage
from pages.loginpage import LoginPage

# def test_accessibility_tab_order_admin_page(self):
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('http://localhost:8000/admin/')
driver.maximize_window()

loginPage = LoginPage(driver)

#Ensure that the field selected by default is the email field
assert driver.switch_to.active_element == loginPage.emailField
driver.find_element_by_id('djHideToolBarButton').click()

loginPage.emailField.send_keys("[email protected]")
loginPage.passwordField.send_keys("admin")
loginPage.loginButton.click()

adminPage = AdminPage(driver)
adminPage.checkTabOrder()

driver.quit()


from selenium import webdriver
from selenium.webdriver.common.by import By
from pages.basepage import BasePage

class LoginPage(BasePage):
    #login page methods
    def __init__(self, driver):
        super().__init__(driver)
        self.driver = driver

        # #login page elements
        emailField = self.driver.find_element_by_css_selector('input[name="username"]')
        passwordField = self.driver.find_element_by_css_selector('input[name="password"]')
        loginButton = self.driver.find_element_by_css_selector('input[type="submit"]')

CodePudding user response:

I'm not sure I understand the problem, but it looks as if you have solved the problem of "how to pass the driver" to your page objects, but there are a few Python problems. The key is that the class BasePage takes care of keeping track of the driver and the derived class LoginPage(BasePage) uses BasePage as expected.

The there are a few problems:

  • The class AdminPage is missing its base class and should probably read class AdminPage(BasePage).

  • The code code of your classes must be in individual methods that each have a self argument and driver must be accessed as self.driver.

Have a look at the following canonical example:

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

class MainPage(BasePage):
    def __init__(self, driver):
        super().__init__(driver)
        
    def test(self):
        print(self.driver)

mainPage = MainPage(4711)
mainPage.test()

CodePudding user response:

Refined answer based on your new example:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from pages.adminpage import AdminPage
from pages.loginpage import LoginPage

# def test_accessibility_tab_order_admin_page(self):
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('http://localhost:8000/admin/')
driver.maximize_window()

loginPage = LoginPage(driver)
loginPage.test()

#adminPage = AdminPage(driver)
#adminPage.checkTabOrder()

driver.quit()
from selenium import webdriver
from selenium.webdriver.common.by import By
from pages.basepage import BasePage

class LoginPage(BasePage):
    #login page methods
    def __init__(self, driver):
        super().__init__(driver)
        self.driver = driver

        # login page elements
        self.emailField = self.driver.find_element_by_css_selector('input[name="username"]')
        self.passwordField = self.driver.find_element_by_css_selector('input[name="password"]')
        self.loginButton = self.driver.find_element_by_css_selector('input[type="submit"]')

    def test(self):
        # ensure that the field selected by default is the email field
        assert self.driver.switch_to.active_element == self.emailField
        self.driver.find_element_by_id('djHideToolBarButton').click()

        self.emailField.send_keys("[email protected]")
        self.passwordField.send_keys("admin")
        self.loginButton.click()

  • Related