selenium3 web automated testing framework III: Design and packaging model project combat PO

po design model


  Page Object  mode is primarily designed as a page of each class, which contains elements of the page to be tested (buttons, fields, title, etc.), so that the page can be retrieved based on the page to get Selenium test page elements, this avoids the id of the element when a page or a position change, the need to change the test code. When the change element ID, only need to change the test page to page properties of Class. Separating the pages and positioning operations, the separation of the test object (target element), and a test script (script use cases), to improve the maintainability.

  Page Object Model is an automated test pattern designed to separate pages and locating business operations, the separation of the test object (target element), and a test script (script use cases), to improve the maintainability of use cases.

       unittest is a unit testing framework for the design of a wide variety of test cases, call page class (object) of the PageObject design, a design with more maintainable embodiment. It provides the organization and implementation of use cases, provide a rich comparison (assertion) method, provides a wealth of logs, apply uniformly to the development and implementation of web automation use cases.

    How Google Test (Google Testing of the Road), book on the web product testing on agile testing is divided into three pyramids, and workload ratio Shi, Unit (unit testing): 70%, Service (API): 20%, UI (GUI): 10%.

  So the UI Automation core issue position is decoupled with the business layer out and deal with the problem of locating the individual, and then implement the business layer. The method is to use a UI page do we test for the three extract, for example as BasePage + LoginPage + Unittest.   

  Subsequent examples, the basic idea PO Model: register_page (lookup page element class) -> register_handle (operation layer, passes the data to find the element to a position) -> register_business (business layer: invoking operation layer transfer according to the operation layer the scene determination result, the error scenario as mail, etc.) -> first_case (call service encapsulating layer, for test case scenario assembly)

 

How to design the operating model of the layer po


Find a page element classes: the positioning of page elements are encapsulated in a corresponding way of a corresponding page, targeting elements of this page can be found in this document

Operating Layer: Write method requires the registration process, such as enter a user name, password and other page operation, tools - all methods to save the page operation.

Business layer: the assembled handle layer

Find a page element classes: register_page.py

Operation Layer: register_handle.py

Business layer: register_business.py

 

Look for elements like page


 Sample Code: register_page.py

# -*-  coding:utf-8 -*-
from  util.find_elemnet import FindElement

class RegisterPage():
    def __init__(self,driver):
        self.find_element = FindElement(driver)

    def get_email_element(self):
        return self.find_element.get_element("element_email")

    def get_name_elemnet(self):
        return self.find_element.get_element("element_name")

    def get_pwd_element(self):
        return self.find_element.get_element("element_pwd")

    # Error checking 
    DEF get_email_error_info (Self):
         return self.find_element.get_element ( " element_email_error " )

    def get_name_error_info(self):
        return self.find_element.get_element("element_name_error")

    def get_pwd_error_info(self):
        return self.find_element.get_element("element_pwd_error")

 

Operating Layer


 Sample Code: register_handle.py

# -*-  coding:utf-8 -*-
from selenium import webdriver

from page.register_page import RegisterPage

class RegisterHandle(object):

    def __init__(self,driver):
        self.register_p = RegisterPage(driver)

    # Enter your e- 
    DEF send_user_email (Self, Email):
        self.register_p.get_email_element().send_keys(email)

    # Enter the user name 
    DEF send_user_name (Self, name):
        self.register_p.get_name_elemnet().send_keys(name)

    # Password 
    DEF send_user_pwd (Self, pwd):
        self.register_p.get_pwd_element().send_keys(pwd)

    # Get error messages 
    DEF get_user_text (Self, info, user_info):
        text = None
        if info == "element_email_error":
            text . self.register_p.get_email_error_info = () get_attribute ( " value " ) # value is the html tag attribute values 
        elif info == " element_name_error " :
            text = self.register_p.get_name_error_info().get_attribute("value")
        elif info == "element_pwd_error":
            text = self.register_p.get_pwd_error_info () get_attribute (. " value " )
         the else :
             Print ( " Parameter error value of the input parameter " )
         return text

if "__name__" == "__main__":
    pass

 

Business Layer


Sample Code: register_business.py

# -*-  coding:utf-8 -*-
from handle.register_handle import RegisterHandle
from selenium import webdriver

class RegisterBusiness():
    def __init__(self,driver):
        self.register_h = RegisterHandle(driver)

    def base_case(self,email,name,pwd):
        self.register_h.send_user_email(email)
        self.register_h.send_user_name(name)
        self.register_h.send_user_pwd(pwd)

    # Check the mailbox input errors 
    DEF email_error (Self, Email, name, pwd):
        self.base_case(email,name,pwd)
        IF self.register_h.get_user_text ( " element_email_error " , " Please enter a valid email address " ) == None:
             Print ( " Mailbox check unsuccessful " )
             return AssertionError
         the else :
             return False


if __name__ == "__main__":
    driver = webdriver.Chrome()
    driver.get("http://www.5itest.cn/register")
    testcase1 = RegisterBusiness(driver)
    testcase1.base_case("111","222","111")

 

Guess you like

Origin www.cnblogs.com/wuzhiming/p/11768889.html