Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Python Selenium Page Object Model Tutorial

In Selenium Test Automation, Page Object Model is a framework in which all page elements are defined in a SiteModel class. In other modules of the test framework, the SiteModel class will be called to interact with elements. This makes the test maintenance easy because all the UI change will only affect the SiteModel class.

With Python language, we can define all site elements in an external xml file and read element information from this file and use in the main test code. When UI changes frequently, we just need to update the xml file with the updated UI elements and some find by logic.

Test Case: authorized users are able to login Mahara Demo site. Here are test steps.

1. Navigate to demo.mahara.org and verify that web page title "Home-Mahara" displays.

2. Enter "student1" in the Username filed. 

3. Enter "Testing1" in the Password field.

4. Click on the Login button

5. Assert that Logout link is present.

Note: if the above username and password do not work, try registering one and use your own user account for testing because the demo user password may have been changed by others. 

Test Implementation

Step 1: create a xml fie and save it as "SiteElement.xml" and add the following element information.

Step 2: launch Python IDE, create a new file , and save it as "PythonSeleniumPageObjectModel.py" with the following code. 


import unittest
import xml.etree.ElementTree as ET
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
tree=ET.parse('SiteElement.xml')
root=tree.getroot()

def getSearchValue(pagenameattribute,elementkeystring):
    xpathstring="./pages/page[@name='"+pagenameattribute+"']/element[@Key='"+elementkeystring+"']"
    ##print(xpathstring)
    for element in root.findall(xpathstring):
        value=element.find('value').text
        return value
	
def getSearchBy(pagenameattribute,elementkeystring):
    xpathstring="./pages/page[@name='"+pagenameattribute+"']/element[@Key='"+elementkeystring+"']"
    ##print(xpathstring)
    for element in root.findall(xpathstring):
        searchby=element.find('searchby').text
        return searchby
    
class DemoMaharaOrgLogin(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_login_in_demo_mahara_org(self):
        driver = self.driver
        driver.get("http://demo.mahara.org/")
        self.assertIn("Home - Mahara", driver.title)
        username = driver.find_element_by_id(getSearchValue("LoginPage","LoginUserName"))
        username.send_keys("student1")
        password=driver.find_element_by_id(getSearchValue("LoginPage","LoginPassword"))
        password.send_keys("Testing1")
        loginbutton=driver.find_element_by_id(getSearchValue("LoginPage","SubmitButton"))
        loginbutton.click()
        self.assertTrue(driver.find_element_by_link_text(getSearchValue("DashboardPage","LogoutLink")),"Logout link")

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

Step 3: run the above code and see the result. Here is the code result.  Test passed. 

Ran 1 test in 20.094s

OK
>>>