This chapter was a tutorial introduction to page objects design pattern. A Page object represents an area in the Web application user interface This your test is interacting.
Benefits of using Page object pattern:
- Creating reusable code that can is shared across multiple test cases
- Reducing the amount of duplicated code
- If the user interface changes, the fix needs changes
1. Test case
Here's a test case which searches for a word in python.org website and ensure some results are found.
ImportUnitTest fromSeleniumImportWebdriverImportpageclassPythonorgsearch (unittest. TestCase):"""A sample test class to show how Page object works""" defsetUp (self): Self.driver=Webdriver. Firefox () Self.driver.get ("http://www.python.org") deftest_search_in_python_org (self):"""Tests python.org Search feature. Searches for the word "pycon" then verified that some results show up. Note that it does is not a look for any particular the text in search results page. This test verifies, the results were not empty. """ #Load the main page. The the home page of python.org.Main_Page =page. MainPage (Self.driver)#Checks If the word "Python" is in title assertMain_page.is_title_matches (),"python.org title doesn ' t match." #sets the text of the search textbox to "Pycon"Main_page.search_text_element ="Pycon"Main_page.click_go_button () search_results_page=page. Searchresultspage (Self.driver)#verifies that the results page was not empty assertSearch_results_page.is_results_found (),"No results found." defTearDown (self): Self.driver.close ()if __name__=="__main__": Unittest.main ()2. Page Object Classes
The Page object pattern intends creating an object to each Web page. By following this technique a layer of separation between the test code and technical implementation are created.
The would look like this page.py :
fromElementImportbasepageelement fromLocatorsImportmainpagelocatorsclasssearchtextelement (basepageelement):"""This class gets the search text from the specified locator""" #The locator for Search box where search string is enteredLocator ='Q'classBasePage (object):"""base class to initialize the base page that would be called from all pages""" def __init__(self, driver): Self.driver=DriverclassMainPage (basepage):"""Home Page action methods come here. i.e. python.org""" #declares a variable that'll contain the retrieved textSearch_text_element =searchtextelement ()defis_title_matches (self):"""verifies the hardcoded text "Python" appears in page title""" return "Python" inchSelf.driver.titledefClick_go_button (self):"""Triggers the search"""element= Self.driver.find_element (*Mainpagelocators.go_button) Element.click ()classsearchresultspage (basepage):"""Search Results Page action methods come here""" defIs_results_found (self):#Probably should search for this text in the specific page #element, but as for now it works fine return "No results found." not inchSelf.driver.page_source
3. Page elements
The would look like this element.py :
fromSelenium.webdriver.support.uiImportwebdriverwaitclassbasepageelement (object):"""The Base Page class is initialized on every Page object class.""" def __set__(self, obj, value):"""sets the text to the value supplied"""Driver=obj.driver webdriverwait (Driver,100). Until (LambdaDriver:driver.find_element_by_name (Self.locator)) Driver.find_element_by_name (Self.locator). Send_keys ( Value)def __get__(self, obj, owner):"""Gets The text of the specified object"""Driver=obj.driver webdriverwait (Driver,100). Until (LambdaDriver:driver.find_element_by_name (self.locator)) element=driver.find_element_by_name (self.locator)returnElement.get_attribute ("value")4. Locators
One of the practices is-separate the locator strings from the place where they is being used. In this example, locators of the same page belong to same class.
The would look like this locators.py :
fromSelenium.webdriver.common.byImport byclassmainpagelocators (object):"""A class for main page locators. All main Page Locators should come here"""Go_button= (By.id,'Submit')classsearchresultspagelocators (object):"""A class for search results locators. All search results Locators should come""" Pass
Selenium-webdriver:page Objects