current position:Home>Python + selenium automated test: page object mode

Python + selenium automated test: page object mode

2021-08-23 04:47:58 Test development technology

Page Objects yes selenium A test design pattern for , Think of each page as a class.class The main contents of the system include properties and methods , Attributes are not hard to understand , Is the element object in this page , For example, the input box for entering the user name , Input box for entering login password 、 Login button 、 Of this page url etc. . The method , It mainly refers to the specific functions that this page can provide .


Why choose POM?

Let's start with a simple code as follows :

from selenium import webdriver

This is a simple little script . Script maintenance looks simple . But over time, the test suite grew . As you add more and more lines to your code , Things get tough . The main problem with script maintenance is , If 10 Different scripts use the same page elements , And any changes in that element , You need to change all 10 Script . This is a Time consuming and error prone .

A better way to maintain scripts is to create a separate class file , It can be found Web Elements , Fill in or verify them . This class can be used in In all scripts reusing . future , If web Elements change , We need to be in 1 Make changes in a class file , instead of 10 A different script .


What is? POM?

Page object model Is for Web UI Element creation Object Repository Design mode of .

Under this model , For every web page in the application , There should be corresponding page classes . this Page Class will find the Web Page WebElements, And it's also about these WebElements Page methods to perform operations .

The names of these methods should be given according to the task they are performing , namely If a loader is waiting for the payment gateway to appear ,POM The method name can be waitForPaymentScreenDisplay().

Below is a POM and POM Contrast figure :

In automated testing , Introduced Page Object Model(POM): Page object mode to solve ,POM Can make our test code more readable , High maintainability , High reusability .

POM The advantages of :

  • POM Provides a way to UI Layer operation 、 Business process and verification are separated , This makes the test code clearer and more readable .
  • Object libraries are separated from use cases , Makes us better reuse objects , It can even be deeply combined with different tools .
  • Reusable page method code will become more optimized .
  • A more effective naming method makes it easier for us to know what the method operates UI Elements . For example, we want to go back to the home page , The method name is : gotoHomePage(), You can clearly know the specific function implementation through the method name .


Case description

Here is a simple and common login test case :

def test_login_mail(self):
 driver = self.driver

Then how can we carry out a transformation and upgrading ?

Reform case ideas : 1. We're going to separate the test objects ( Element object ) And test scripts ( Use case scripts ), So let's create two script files , Respectively Used to define page element objects , Every element is encapsulated as a component ( It can be seen as a warehouse for storing page element objects ) Test case scripts .

2. The idea of design and implementation , All elements and their operation components are defined in Page page , Use case script page , By calling Page Component objects in , To piece together a login script .

#-*- coding: utf-8-*-

#-*- coding: utf-8-*-
from importBy
import BasePage
# Inherit BasePage class 
class LoginPage(BasePage.Action):
# positioner , Locate the element object through the element attribute 
 password_loc =(By.ID,"pwdInput")
 submit_loc =(By.ID,"loginBtn")
 dynpw_loc =(By.ID,"lbDynPw")
 userid_loc =(By.ID,"spnUid")

def open(self):
# call page Medium _open Open the connection 
# call send_keys object , enter one user name 
def input_username(self, username):
# call send_keys object , Input password 
def input_password(self, password):
# call send_keys object , Click login 

def click_submit(self):
# The user name or password is unreasonable Tip Box content display 
def show_span(self):
# Switch the login mode to dynamic password login (IE Effective under )
def swich_DynPw(self):
# Users in the login success page ID lookup 
def show_userid(self):
returnself.find_element(*self.userid_loc).text*- coding: utf-8-*-
import sys
sys.setdef aultencoding('utf-8')
import unittest
from POimportLoginPage
from seleniumimport webdriver
 Sign in case
def setUpClass(cls):
 cls.driver = webdriver.Chrome()
 cls.url =""
 cls.username ="xxxxx"
 cls.password ="xxxxx"
# Use case executor 
def test_login_mail(self):
# Statement LoginPage Class object 
login_page=LoginPage.LoginPage(self.driver, self.url, u”xxxxx”)
# Call the open page component
# Call the user name input component 
# Call the password input component 
# Call the click login button component 
def tearDownClass(cls):
if __name__=="__main__":

Use POM Reconstruct the code structure after , It is found that the readability of code test case code is improved a lot , The way elements are written as components , You don't have to write every time findElement You can use components directly in scripts .

stay CaseLoginTest In the script case executor , Once we type in login_page And type in a point ,LoginPage The elements, objects and components in the page are displayed . also Defined PageObject Components can be reused in other scripts , Reduces the amount of code , It is also convenient for the maintenance and management of the script , When the attributes of an element change , We just need to deal with one PageObaject Change the definition of the object component in the page .

Finally, make a conclusion , Please enter all codes manually , Do not copy directly . Again POM Make a summary :

  • POM yes selenium webdriver Automatic test practice object library design pattern
  • POM Make test scripts easier to maintain
  • POM The elements are further optimized by means of object library 、 Use cases 、 Data maintenance organization

This article is from WeChat official account. - Test development technology (mikezhou_talk) , author : automated testing

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the [email protected] Delete .

Original publication time : 2021-08-05

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

copyright notice
author[Test development technology],Please bring the original link to reprint, thank you.

Random recommended