Page object modal is a script design model that splits page elements, business operations, and when the actual page changes, only the page element file needs to be modified, and the business operation does not need to be modified.
The specific implementation needs to write a page public class, which write public methods, such as the element positioning, element manipulation and other methods to reseal. After all the page classes inherit the page public class, the page class only writes the element that the page needs to locate, and encapsulates the action of the corresponding element into a method. The final test class calls these methods to form a business test case.
or Baidu search as an example
?
Put some public classes under the framework package
PageObject the page class to test under the package
Test class for the corresponding page under the Testsuites package
classBasePage (object):def __init__(self,driver): Self.driver=Driverdeffind_element (self,selector): by=Selector[0] Value= Selector[1] Element=Noneifby = ='ID' orby = ='name' orby = ='class' orby = ='Tag' orby = ='Link' orby = ='Plink' orby = ='CSS' orby = ='XPath': ifby = ='ID': Element=self.driver.find_element_by_id (value)elifby = ='name': Element=self.driver.find_element_by_name (value)elifby = ='class': Element=self.driver.find_element_by_class_name (value)elifby = ='Tag': Element=self.driver.find_element_by_tag_name (value)elifby = ='Link': Element=Self.driver.find_element_by_link_text (value)elifby = ='Plink': Element=Self.driver.find_element_by_partial_link_text (value)elifby = ='CSS': Element=Self.driver.find_element_by_css_selector (value)elifby = ='XPath': Element=Self.driver.find_element_by_xpath (value)Else: Print('no element found') returnelementElse: Print('INPUT element is not positioned in the wrong way') defSend (Self,selector,value): element=self.find_element (selector)#calling the encapsulated anchored element method Try: Element.send_keys (value)Print('Content entered%s'%value)exceptbaseexception:Print('Error') defClick (self,selector): element=self.find_element (selector) Element.click ()
This is the public method of encapsulation. Encapsulated element positioning method needs to pass an array, array subscript The first is the positioning method, the second is a specific value. Element positioning is written in the encapsulated element operation, not written in the following page class because it is convenient to reuse elements within the page class.
fromFramework.base_pageImportBasePageclassBaidu (basepage): kw= ['ID','kw']#Locate search Input BoxSU = ['ID','su']#Search Button defKw_send (Self,value):#search box Input contentself.send (Self.kw,value)defSu_click (self):#Click the Search buttonSelf.click (self.su)
This is the Baidu search page of the page class. The positioned elements are taken out individually so that they can be reused. The operation of the element is then encapsulated into a method. For example, the above search box input content. Value, when testing the class later, can be assembled into different test cases depending on the input.
fromSeleniumImportWebdriver fromPageObject. BaidupageImportBaiduImportUnitTestclassBaiduserch (unittest. TestCase):defTest_baidu (self): Dr=Webdriver. Firefox () Dr.get ('https://www.baidu.com') Bai=Baidu (DR) Bai.kw_send ('Selenium') Bai.su_click ()if __name__=='__main__': Unittest.main ()
The last is the test class, where the UnitTest unit test framework is used. Bai = Baidu (DR) each page needs to pass driver in. One thing to keep in mind when using Webdriver is that the script runs with only one driver.
Python3+selenium Frame Design 01-page Object