po model design idea
Page Object The mode is mainly to design each page as a class, which contains the elements to be tested in the page (buttons, input boxes, titles, etc.), so that the page elements can be obtained by calling the page class in Selenium test page, so as to avoid changing the test code after the page element ID or position changes. When the element ID changes, you only need to change the page attribute in the test page class. Separate page positioning and business, separate test objects (element objects) and test scripts (use case scripts), and improve maintainability.
Page Object pattern is an automatic test design pattern, which separates page positioning from business operations, separates test objects (element objects) and test scripts (use case scripts), and improves the maintainability of use cases.
unittest is a unit test framework, which is used to design various test cases. You can call the page class (object) designed by PageObject to design more maintainable use cases. It provides use case organization and execution, rich comparison (assertion) methods and rich logs, which is uniformly applicable to the development and execution of web automation use cases.
The core problem of UI automation is to decouple from the business layer, deal with the positioning problem separately, and then implement the business layer. The method used is to test a UI page. We extract it into three layers, for example, BasePage+LoginPage+Unittest.
In the following examples, the basic idea of PO model is login_ Page (find page element class) - > login_ Handle (operation layer, passing data on the found element position) - > login_ Business (business layer: call the operation layer and judge the scenarios according to the results delivered by the operation layer, such as mailbox error scenario) - > first_ Case (encapsulating and calling the business layer for scenario assembly of test cases)
How to design the operation layer of po model
Find page element class: encapsulate the methods of locating page elements in a corresponding page, and the element location methods of this page can be found in this file
Operation layer: write the methods required in the registration process, such as entering user name, password and other page operations. Tool class - all methods to save page operations.
Business layer: assemble the operation layer
Find page element class: loginPage.py
Operation layer: loginrHandle.py
Business layer: loginbusiness.py
Next, take the 126 mailbox web login page as an example to show the PO model
Locate page element class
Example code: loginPage.py
1 from selenium import webdriver 2 3 #Locate page elements 4 class LoginPage: 5 def __init__(self): 6 self.driver = webdriver.Chrome() 7 self.driver.get('https://www.126.com/') 8 self.driver.maximize_window() 9 self.driver.implicitly_wait(20) 10 # Switch to form 11 self.driver.switch_to.frame(0) 12 13 #Locate mailbox input box 14 def get_email_element(self): 15 return self.driver.find_element_by_css_selector('input[name="email"]') 16 17 #Locate the password input box 18 def get_password_element(self): 19 return self.driver.find_element_by_css_selector('input[name="password"]') 20 21 #Define login button 22 def get_loginbutton_element(self): 23 return self.driver.find_element_by_id('dologin')
Operation layer
Example code: loginrHandle.py
1 #Operation layer 2 class loginHandle(LoginPage): 3 4 def __init__(self): 5 LoginPage.__init__(self) 6 7 #Enter mailbox 8 def send_user_email(self): 9 self.get_email_element().send_keys('Augus') 10 11 #Input password 12 def send_user_password(self): 13 self.get_password_element().send_keys("1234") 14 15 #Click the login button 16 def click_loginbutton(self): 17 self.get_loginbutton_element().click()
Business layer
Example code: loginbusiness.py
1 #Business layer 2 class LoginBusiness(loginHandle): 3 def __init__(self): 4 loginHandle.__init__(self) 5 #Login function 6 def login_case(self): 7 self.send_user_email() 8 self.send_user_password() 9 self.click_loginbutton() 10 11 if __name__ == '__main__': 12 l = LoginBusiness() 13 l.login_case()