当前位置:网站首页>Automated test Po design model
Automated test Po design model
2022-07-25 17:36:00 【Outsider 1】
POM(Page Object Model) That is, the page object model
Design patterns are not uncommon in development , We all know that back-end development has MTV/MVC And so on 、 So our PO Design patterns can also be used as a development pattern for automated test development , seeing the name of a thing one thinks of its function Page It's the page Object It's the object , Everything in the programming world is an object, so when we do automated testing, we can also package each page into one object after another for management
Its function is to put the elements of each page of the project and the business logic of the test 、 Data processing for separation , Encapsulate each page into a page class , Every page class needs to inherit from a class called PageBase Class as base class , This base class only encapsulates some that need to be used for each page selenium API Such as starting the browser to open url Interface and other commonly used methods , If you think these are not enough to meet your requirements, you can actually selenium/appium Secondary encapsulation is also placed in this class .
PO The design model is currently made by the industry UI Automated testing has become a mainstream and common design pattern , Almost as long as there is automation, it is necessary to use this design model , Because at this stage, there is no comparison PO Design pattern better design pattern .
PO What is it? ?
1、 Page object model (PO) It's a design pattern , Used to manage and maintain a set of web Object library of elements .
2、 stay PO Next , Each page of the application has a corresponding page class.
3、 every last page class Maintaining the web The set of elements of the page and the methods of manipulating these elements .
4、page class The method in should be named according to the corresponding business scenario .
Page Object Mode has the following advantages
1、PO It provides a mode of separating business process from page element operation , Test code becomes more readable 、 flexible 、 Maintainable .
2、 Separation of page objects and use cases , Makes us better reuse objects .
3、 Reusable page method code will become more optimized .
4、 A more effective naming method makes it easier for us to know what the method operates UI Elements .
5、 Reduce the maintenance cost of automated testing 、 The front-end page is changeable. During maintenance, only the page elements can be modified .
Layered architecture
PO The core idea of the model is stratification , Achieve loose coupling ! Achieve script reuse , Achieve script maintainability ! Layering mechanism , Let different layers do different types of things , Make the code structure clear , Increase reusability .
Architecture diagram

Page Object Model Realize the idea
step 1: So let's define a PageBaseClass Encapsulate some basic methods in this class Such as : Start browser operation 、 open URL operation ,selenium/appium API Secondary packaging, etc . Page Object models PageBase Used to start each page Preconditions for Such as starting the browser open URL
class PageBase:
def __init__(self,driver,timeout=3):
self.driver = driver
# obtain webdriver example
self.url = AUTO_PROJECT["front"]
# Default url Login interface for the project
self.driver.implicitly_wait(timeout)
# Global element positioning waiting
def open(self,url=None):
if url:
self.url = url
return self.driver.get(self.url)
......
step 2: Put every defined Page Class All inherited from PageBase Class、 And encapsulate the page 、 Each page should be a class 、 The operation elements in each page are only encapsulated and positioned once , And you don't need to encapsulate every element 、 Use element encapsulation 、 After encapsulation, each element needs to have an instance of the page object return get out ( If the operation is separated from the element ).
from common.Element import PageBase
from selenium.webdriver.common.by import By
from selenium import webdriver
class MallLoginPage(PageBase):
@property
def form_login(self):
""" Phone number """
return self.webWait(
by=(By.XPATH,'//*[@id="app"]/div/div/form/div[1]/div/div[2]/input')
)
@property
def form_password(self):
""" password """
return self.webWait(
by=(By.XPATH,'//*[@id="app"]/div/div/form/div[2]/div/div/input')
)
@property
def button_sign(self):
""" The login button """
return self.webWait(
by=(By.XPATH,'//*[@id="app"]/div/div/form/div[3]/div/button')
)
@property
def login_text(self):
""" Assert the text to get the login """
return self.webWait(
by=(By.XPATH,'//*[@id="app"]/div/div/div[1]/div/span/span[2]')
)
step 3: And then we will do the encapsulation of each element ( If yes, the keyword is that each method corresponds to an element location without operations ) Assemble into a complete operation process , Data needs to be separated, so parameters need to be defined , Used in unittest Don't write dead when calling in the framework .
def mall_login(self,phone,password):
self.form_login.send_keys(phone)
self.form_password.send_keys(password)
self.button_sign.click()
step 4: Use the unit test framework to manage test cases and process business logic
class Login(unittest.TestCase):
""" Login case """
def setUp(self):
self.driver = webdriver.Chrome()
self.LoginPage = LoginElement(driver=self.driver)
def test_login_normally(self):
""" Normal login """
self.MallLoginPage.mall_login(
username=61234567,
password="gfl13453001",
des=" Normal login "
)
text = self.LoginPage.login_text.text
self.assertEqual(text,' Login successful ',msg=" The login user name is inconsistent with the expectation ")
def test_login_error_password(self):
""" Wrong password login """
self.MallLoginPage.mall_login(
username=61234567,
password="gfl1345300",
des=" Wrong password login "
)
text = self.LoginPage.login_text_error.text
self.assertEqual(text,' Wrong phone number or password ',msg=" Wrong phone number or password ")
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
The above is a general idea of a three-tier implementation 、 Through the above case, you can have your own enterprise project to make corresponding modifications as long as you follow PO Just design the model .
Used PO Design patterns clearly show that test cases become concise , And it is very clear and easy to understand , And easy to maintain .
边栏推荐
- Technical difficulties and applications of large humanoid robots
- 【Cadence Allegro PCB设计】error: Possible pin type conflict GND/VCC Power Connected to Output
- 关于flickr的数据集笔记
- What is metauniverse gamefi chain game system development? Development and application case and analysis of gamefi metauniverse NFT chain game system
- Sogou batch push software - Sogou batch push tool [2022 latest]
- 【硬件工程师】DC-DC隔离式开关电源模块为什么会用到变压器?
- 带你初步了解多方安全计算(MPC)
- How to prevent the unburned gas when the city gas safety is alarmed again?
- 我也是醉了,Eureka 延迟注册还有这个坑!
- 爬虫框架-crawler
猜你喜欢

第三章、数据类型和变量

HCIP第一天实验

对灰度图像的三维函数显示
![[Hardware Engineer] can't select components?](/img/bd/fdf62b85c082f7e51bf44737f1f787.png)
[Hardware Engineer] can't select components?

Outlook tutorial, how to search for calendar items in outlook?

POWERBOARD coco! Dino: let target detection embrace transformer

How to fix the first row title when scrolling down in Excel table / WPS table?

Redis源码与设计剖析 -- 17.Redis事件处理

How to install govendor and open a project

Customize MVC project login registration and tree menu
随机推荐
什么是元宇宙Gamefi链游系统开发?Gamefi元宇宙NFT链游系统开发应用案例及分析
How to fix the first row title when scrolling down in Excel table / WPS table?
「数字安全」警惕 NFT的七大骗局
[Hardware Engineer] Why do DC-DC isolated switching power modules use transformers?
Redis源码与设计剖析 -- 17.Redis事件处理
【硬件工程师】元器件选型都不会?
Crawler framework crawler
我想理财,不懂,有没有保本金的理财产品?
02. Add two numbers
双向链表的基本操作
多项式相加
Summary of 80 domestic database operation documents (including tidb, Damon, opengauss, etc.)
交友活动记录
Starting from business needs, open the road of efficient IDC operation and maintenance
Thesis reading_ Multi task learning_ MMoE
PostgreSQL里有只编译语句但不执行的方法吗?
超越 ConvNeXt、RepLKNet | 看 51×51 卷积核如何破万卷!
02.两数相加
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
How to delete Microsoft Pinyin input method in win10