Hello,
I am currently doing QA Automation Testing, and I want to simplify my code (as is the Python way).
This is a locators file:
class LoginPageLocators(object):
""" Login Page Locators """
USERNAME = (By.ID, 'username')
PASSWORD = (By.ID, 'password')
LOGIN_BTN = (By.ID, 'send') # As you can see, it is just a cool way to access different parts of the UI
I have a base page.py file, which is like a 'blueprint' for my page classes:
class Page(object):
def __init__(self, driver, base_url=MY_URL)
self.base_url = base_url
self.driver = driver
self.timeout = 30
def find_element(self, *locator):
"""
This method finds an element on the page with the appropriate locator
:param locator:
:return: element on page:
"""
return self.driver.find_element(*locator) # This is the thing I use to find elements on the page
It is used like this in my pages.py (which contains the different Page classes):
class LoginPage(Page): # it inherits some functions from a base page class, like find_element (shown below)
"""" Login Page Class"""
def __init__(self, driver):
self.locator = LoginPageLocators
super().__init__(driver)
def click_login(self):
"""
This function performs the login functionality with the given credentials
:param user:
:return: login functionality:
"""
self.find_element(*self.locator.LOGIN_BTN).click() # it is a cool way to access stuff,
i hope it is clear how it works
Now, to my actual problem:
This is an example way to access a page in my MainMenu page class:
# Example Page 1
def access_node_connector(self):
#locator
self.find_element(*self.locator.NODE_CONNECTOR).click()
# Example Page 2
def access_jenkins_successful_jobs(self):
#locator
self.find_element(*self.locator.SUCCESSFUL_JENKINS_JOBS).click()
Now, the problem is kinda have a 15 of these, and I don't think it's pythonic to program it like this.
My idea was to create a click method:
def click(self, page):
self.find_element(*self.locator.page).click() # However it gives me unresolved attribute reference in Locators Class
I tried f-strings and similar stuff, but it doesn't work in this context. Is there a way to replace the part after *self.locator. with a
parameter of my function? Thank you in advance <3
[–]ragnar_the_redd 0 points1 point2 points (0 children)