Page (fifty-five) advanced application of automated test design patterns -Page Object instance Object

 

Essays and records to facilitate their access to fellow travelers.

 

#------------------------------------------------I ------------------------------------------- dividing line is a shame

 

  Learning before selenium automation, it is best to learn HTML, CSS, JavaScript and other knowledge, help to understand the principles of operation and positioning elements. About python and selenium install any additional information please search on their own here do not introduced, all examples are performed using python3.6 + selenium.

 

#------------------------------------------------I ------------------------------------------- dividing line is a shame

 

Page Object example

Below to log QQ mailbox, for example, by Page Object to implement design patterns. New po_model.py

 

# !/usr/bin/env python
# -*- coding: UTF-8 –*-
__author__ = 'Mr.Li'

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

class Page(object):
    '''
    Base class for inheriting a page object class
    '''
    login_url = 'https://www.126.com'

    def __init__(self,selenium_driver,base_url=login_url):
        self.base_url = base_url
        self.driver = selenium_driver
        self.timeout = 30

    def on_page(self):
        return self.driver.current_url == (self.base_url + self.url)

    def _open(self,url):
        url = self.base_url + url
        self.driver.get(url)
        self.driver.find_element_by_link_text ( ' password ' ) .click ()

        self.driver.switch_to_frame(self.driver.find_element_by_xpath("//iframe[@scrolling='no']"))  # 进入iframe框架
        assert self.on_page(),'Did not land on %s' % url

    def open(self):
        self._open(self.url)

    def find_element(self,*loc):
        return self.driver.find_element(*loc)

class LoginPage(Page):
    '''
    126 E-mail login page model
    '''
    url = '/'

    #定位器
    username_loc = (By.XPATH,"//input[@name='email']")
    passwrod_loc = (By.XPATH,"//input[@name='password']")
    submit_loc = (By.XPATH, "//a[@id='dologin']")

    #Action
    def type_username(self,username):
        self.find_element(*self.username_loc).send_keys(username)
        sleep(1)

    def type_password(self,password):
        self.find_element(*self.passwrod_loc).send_keys(password)
        sleep(1)

    def submit(self):
        self.find_element(*self.submit_loc).click()
        sleep(1)

def test_user_login(driver,username,password):
    '''
    Use cases to test whether the acquired name / password can log
    :param username:
    :param password:
    :return:
    '''
    login_page = LoginPage(driver)
    login_page.open()
    login_page.type_username(username)
    login_page.type_password(password)
    login_page.submit()

def main():
    try:
        driver = webdriver.Chrome()
        driver.maximize_window()
        username = 'xxxx'
        password = 'xxxx'
        test_user_login(driver,username,password)
        sleep(3)
        text = driver.find_element_by_xpath ( " // span [@ the above mentioned id = 'spnUid'] " ) .text
         the Assert text == ' [email protected] ' , ' user names do not match, the login fails! '

    a finally :
         # close the browser window 
        driver.close ()

if __name__ == '__main__':
    main()

 

Page Object implementation design pattern is clearly the structure complicates a lot. Here we analyze them paragraph by paragraph, to appreciate the benefits of this design.

 

  1. Create a page class

 

cclass Page(object):
    '''
    Base class for inheriting a page object class
    '''
    login_url = 'https://www.126.com'

    def __init__(self,selenium_driver,base_url=login_url):
        self.base_url = base_url
        self.driver = selenium_driver
        self.timeout = 30

    def on_page(self):
        return self.driver.current_url == (self.base_url + self.url)

    def _open(self,url):
        url = self.base_url + url
        self.driver.get(url)
        self.driver.find_element_by_link_text ( ' password ' ) .click ()

        self.driver.switch_to_frame(self.driver.find_element_by_xpath("//iframe[@scrolling='no']"))  # 进入iframe框架
        assert self.on_page(),'Did not land on %s' % url

    def open(self):
        self._open(self.url)

    def find_element(self,*loc):
        return self.driver.find_element(*loc)

 

First, create a base class Page , in the initialization method __init __ () is defined in the drive ( Driver ), basic the URL of ( base_url ) and timeout ( timeout ) and so on.

The definition of open () method is used to open the URL site, but it does not do this thing, but making friends _open () method to achieve. About URL asserted that part of the address, then there on_page () method to achieve, and find_element () method for locating elements.

  2. Create a LoginPage class

Page methods defined in the class are the basic method of operation of the page. The following eradicate the login page features the creation LoginPage class and inherited Page class, which is the Page Object design pattern is the most important object layer.

 

class LoginPage(Page):
    '''
    126 E-mail login page model
    '''
    url = '/'

    #定位器
    username_loc = (By.XPATH,"//input[@name='email']")
    passwrod_loc = (By.XPATH,"//input[@name='password']")
    submit_loc = (By.XPATH, "//a[@id='dologin']")

    #Action
    def type_username(self,username):
        self.find_element(*self.username_loc).send_keys(username)
        sleep(1)

    def type_password(self,password):
        self.find_element(*self.passwrod_loc).send_keys(password)
        sleep(1)

    def submit(self):
        self.find_element(*self.submit_loc).click()
        sleep(1)

 

LoginPage category of major elements of the login page for the package, making it a more specific operation method. For example, a user name, password and login button are packaged in a method. 

  3. Create test_user_login () function

 

def test_user_login(driver,username,password):
    '''
    Use cases to test whether the acquired name / password can log
    :param username:
    :param password:
    :return:
    '''
    login_page = LoginPage(driver)
    login_page.open()
    login_page.type_username(username)
    login_page.type_password(password)
    login_page.submit()

 

test_user_login () function to operate a single element consisting of a complete action, and this action contains Open your browser and enter your user name / password, click on the sign-on single step. When using this function is needed to Driver , username , password and other information as a function of the parameters, so that the function is highly reusable.

  4. Create a main () function

 

def main():
    try:
        driver = webdriver.Chrome()
        driver.maximize_window()
        username = 'lirongyang123'
        password = '123lirongyang'
        test_user_login(driver,username,password)
        sleep(3)
        text = driver.find_element_by_xpath ( " // span [@ the above mentioned id = 'spnUid'] " ) .text
         the Assert text == ' [email protected] ' , ' user names do not match, the login fails! '

    a finally :
         # close the browser window 
        driver.close ()

if __name__ == '__main__':
    main()

 

main () function is closer to a user's operation. For users, log on to the mailbox, you need to care about is to open the mailbox URL by which browser, what user name and password are, as input boxes, buttons is how to locate, you do not need to be concerned about.

The benefits of this stratification is that different layers concerned about different issues. Page object layer only care about positioning of elements, the test case concerned only with test data.

A place where there are differences is page whether the object should assert itself contains, or simply to provide data to the test script to set the assertion. In the page that contains the object advocates assert that this helps to avoid duplication assertions appear in the test script. It makes it easier to provide better error messages, and provides a closer only style API . Not in the page object contains the assertion advocates argued that contains mixed responsibilities and implement data access pages assertion logic assertions, and leads page objects too bloated.

I favor the page does not contain assertions object, although we can be eliminated through the provision of assertion libraries for common assertion repeated to provide better diagnosis, but from the user's point of view to automation point of view, to determine whether the login was successful the user needs to be done, should not be handed over to the page object layer to complete.

Use Page Object Another benefit is that after the model helps to reduce redundancy. If you need to enter the ten different user name use cases / password, then use the main () method of writing will become very simple.

Therefore, Page Object role model when a tester to write their own test cases the main scene is not easy to understand, because you do not need and development, business exchanges case, do not write a lot of repetitive motions. But when you really start to try ATDD or BDD , when you start to write some important exceptions branch of the process, when you start a new demand frequent maintenance modification case, it will realize Page Object action.

Finally, Page Object is not a panacea, nor is it the only program to improve the readability of test cases, to avoid redundant steps Case is the ultimate goal.

Guess you like

Origin www.cnblogs.com/lirongyang/p/11595898.html