Page Object Model - Implementation

In the previous post, we discussed Page Object Model concepts. In this post, discussing its implementation with Python and Java

Python Implementation

Let's look into the implementation with Python. The framework has developed with PyTest. The folder structure of the test suite is given below

The base class file name is UIBase.py and it contains the commonly used UI methods such as click, enter_text, assert_text, etc

def click(self, by_locator):
"""
Clicking on a specific element, if element not found within timeout then error thrown
:Args:
- by_locator - locator of the element to be clicked
"""
WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(by_locator)).click()
def assert_element_text(self, by_locator, element_text):
"""
compare the element text with the given text
:Args:
- by_locator - locator of the element in which text value will taken
- element_text - compare text
"""
web_element=WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(by_locator))
return bool (web_element.get_attribute('textContent') == element_text)
view raw UIbase.py hosted with ❤ by GitHub

Let's have a look at page object class files. we've two class files, are Login and Registration. As mentioned in the previous post, The page object class file should contain the UI elements and methods associated with the Login page

#---- Login Page Locators ----#
username_txt = (By.ID, "username")
password_txt = (By.ID, "password")
login_btn = (By.ID, "loginbtn")
h1_tag = (By.XPATH, "/html/body/div[1]/h1")
def __init__(self, driver):
self.driver = driver
def load_login(self, url):
"""
Loading login page
:Args:
- url - login page url
"""
self.driver.get(url)
view raw Login.py hosted with ❤ by GitHub

Now we're looking at how the test class files are created. Class files are created in the AAA pattern like Arrange, Act and Assert. All tests are independent, Have a look at the below code snippet

def test_Login_load(self, driver, url):
# Arrange
# driver and class initialization
login_page = Login(driver)
# Act
# Loading login page
login_page.load_login(url+"login")
# Assert
# Verify whether login page loaded or not
assert login_page.check_login_h1()
def test_valid_login(self, driver, url):
# Arrange
# driver and class initialization
login_page = Login(driver)
view raw TestLogin hosted with ❤ by GitHub

The complete framework is available in the git repository 

Java Implementation 

Technically, there is no difference with the above implementation. There are page object class files and test class files. Please have a look at its Github repository. 


أحدث أقدم