当前位置:网站首页>How to apply Po mode in selenium automated testing
How to apply Po mode in selenium automated testing
2022-07-24 20:23:00 【Zeze said test】
PO The pattern is UI A design pattern that is frequently used in automated testing , After using this mode , It can effectively improve the reusability of code , And make the maintenance of automatic test code more convenient .
PO The full name of the pattern is page object model(POM), Sometimes it's called page object pattern. First proposed by Martin Fowler , This model is affected selenium Automated testing framework is vigorously promoted , Therefore, it has become a very mainstream automatic test design pattern .
stay PO In the mode , every last UI Pages are represented by classes in the programming language . In this class , Define the behavior and operation of the page in the form of function . This allows the caller not to pay attention to whether the specific operation is to click or drag , But focus on specific business , For example, login. 、 Shopping and so on , Even if the programmer shows the code directly to the product manager , He can also understand .

Not used PO Mode time
Write browser operations directly in test cases API, It's not very difficult for coders , Because he himself has been to these API very familiar , But these browser operations do not reflect the business , At least not as familiar as the product manager , So it's hard for him to communicate with the product manager , It's also difficult to communicate with developers , Even half a month later , He had forgotten what he had written .
def test_login_mail(self):
driver = self.driver
driver.get("http://www.xxx.xxx.com")
driver.find_element_by_id("idInput").clear()
driver.find_element_by_id("xxxxxxx").send_keys("xxxxx")
driver.find_element_by_id("xxxxxxx").clear()
driver.find_element_by_id("xxxxxxx").send_keys("xxxxxx")
driver.find_element_by_id("loginBtn").click()
Use PO Pattern
Use PO The mode is conducive to combing the business , It's also good for communicating with others . When you show the following code to the product manager , He may also know what business you are measuring , Can help you correct your test process , Or make some more constructive suggestions , This is very useful when large projects need to communicate and sort out business frequently .
def test_login_mail(self):
LoginPage(driver).login()
The operation of the browser itself , Will be separated into a lower level module , This code you can not expose to the caller , The product manager doesn't care what elements of your page are positioned , He doesn't understand .
class LoginPage:
username_loc=(By.ID,"idInput")
password_loc =(By.ID,"pwdInput")
submit_loc =(By.ID,"loginBtn")
span_loc=(By.CSS_SELECTOR,"div.error-tt>p")
dynpw_loc =(By.ID,"lbDynPw")
userid_loc =(By.ID,"spnUid")
def __init__(self, driver):
self.driver = driver
def login(self):
self.driver.find_element(*self.username_loc).clear()
self.driver.find_element(*self.username_loc).send_keys("xxxxx")
self.driver.find_element(*self.password_loc).clear()
self.driver.find_element(*self.password_loc).send_keys("xxxxxx")
self.driver.find_element(*self.submit_loc).click()
This method separates the element positioning method . However, the readability of this element positioning expression is not very strong , It can be replaced by property Way to represent elements , All the elements are put together , It's also convenient to modify .
class LoginPage:
def __init__(self, driver)
self.driver = driver
@property
def username_element(self):
return self.driver.find_element('id', 'idInput')
@property
def password_element(self):
return self.driver.find_element('id', 'pwdInput')
@property
def submit_element(self):
return self.driver.find_element('id', 'loginBtn')
def login(self, name, password):
self.username_element.send_keys(name)
self.password_element.send_keys(password)
self.submit_element.click()
The third way can make full use of Python Descriptor properties , You will find many serialization libraries or ORM Frameworks have similar uses .
class LoginPage:
def __init__(self, driver)
self.driver = driver
username = Element(css='#idInput', desc=' User name input box ')
password = Element(css='#pwdInput', desc=' Password input box ')
confirm = Element(css='#loginBtn', desc=' Login confirmation button ')
def login(self, name, password):
self.username.send_keys(name)
self.password.send_keys(password)
self.confirm.click()
and Element Class can pass through Python Descriptor implementation , This is just for convenience , It only defines xpath The element positioning method of :
class Element:
def __init__(self,xpath=None,desc=''):
self.xpath = xpath
self.desc = desc
def __get__(self, instance, owner):
driver = instance.browser
el = driver.find_element('xpath', self.xpath)
return el
PO Patterns and DDD
PO The pattern is DDD( Domain-driven design ) A simple implementation of , But it's not thorough enough . If you want to implement in automated testing DDD, I think there is still some room for optimization .
First of all, a business is not necessarily just the operation of a single page , For example, login does not necessarily involve only LoginPage This page , So directly in LoginPage Written in login Function is not very reasonable . For the caller , It should be clear who is logging in , Not a page . like this :
user.login()
# or
login(user)
The code we write is like natural language , Anyone who knows English knows what the code is doing , stay DDD in , It's called domain specific language (DSL), To implement this logic , stay Page There should also be a hierarchy between classes and calls to encapsulate user.
secondly ,Page Pages rely on lower level resources , Like components , Element type . So in Page Class should use InputElement, ButtonElement 、SelectElement Such element classes and HeaderComponent、FooterComponent Such component classes .
class LoginPage:
username_filed = InputElement('xxx')
password_filed = PasswordElement('xxx')
Domain driven design is very important for large-scale projects 、 Synchronization service 、 Communicating business is very helpful , It is a business centered design paradigm .PO Pattern for DDD Small scale application of , And specific enough benefits :
- Easy to maintain . The operation of each page is stored separately in a class file , After the current page is modified , You just need to find the corresponding class file to modify , Other code does not need to be modified , This is in line with the principle of single responsibility .
- Easy to reuse . During automated testing , A test consists of multiple test steps , These test steps may involve the operation of multiple pages . The operations between use cases may coincide .PO Patterns can reuse these test steps , Simplify the writing of code .
- Improved readability . The operations of the page are encapsulated in the form of functions . The function name has the function of annotation , When others read the code, they can understand the business through functions .
The end of this paper , If it's not fun , I also sorted out the complete notes from the introduction to advanced software testing , You can click to view
What does software testing need to learn ?
The contents that have been updated include Selenium Web side web automated testing :
One 、 Why Selenium Do automated tests
Two 、Selenium chromedriver Installation tutorial and quick use
Four 、Selenium How elements are positioned
5、 ... and 、Selenium The way to wait
6、 ... and 、Selenium Scrolling pages
7、 ... and 、Selenium How to use POM Hierarchical mode
8、 ... and 、Selenium Keyword driven automated test framework
Appium Mobile App automated testing
One 、Appium Environment construction nanny level tutorial
Two 、Appium Get started quickly in five minutes
3、 ... and 、Appium How to locate elements
Four 、Appium The core API operation
5、 ... and 、Appium Get and click coordinates
6、 ... and 、Appium Zoom in and out of the picture
7、 ... and 、Appium H5 How to test the page
8、 ... and 、Appium 2.0 Release , How to upgrade
Nine 、Appium How to do concurrent testing
Come in and have a look :
Software testing and automated testing learning roadmap
边栏推荐
- BGP - border gateway protocol
- The U.S. economy continues to be weak, and Microsoft has frozen recruitment: the cloud business and security software departments have become the hardest hit
- Machine learning job interview summary: five key points that resume should pay attention to
- Software testing interview tips | if you don't receive the offer, I'll wash my hair upside down
- 微服务架构 | 服务监控与隔离 - [Sentinel] TBC...
- Bypass using the upper limit of the maximum number of regular backtracking
- Flink window & time principle
- English translation Chinese common dirty words
- Methods of using tyrosine modified peptide nucleic acid PNA | Tyr PNA | BZ Tyr PNA | 99Tcm survivinmrna antisense peptide nucleic acid
- Do you want to enroll in a training class or study by yourself?
猜你喜欢

vlan技术

Easy to use office network optimization tool onedns

Elastomer simulation (elasticity)

Synthesis route of ALA PNA alanine modified PNA peptide nucleic acid | AC ala PNA
![[training Day10] tree [interval DP]](/img/2d/807cabc257f67fb708ed9588769de3.png)
[training Day10] tree [interval DP]

(posted) differences and connections between beanfactory and factorybean

Connect the smart WiFi remote control in the home assistant

Understand the domestic open source Magnolia license series agreement in simple terms

Covid-19-20 - basic method of network segmentation based on vnet3d

Choose the appropriate container runtime for kubernetes
随机推荐
Leetcode 1928. minimum cost of reaching the destination within the specified time
Redis basic knowledge, application scenarios, cluster installation
[training Day10] silly [simulation] [greed]
存储类别
Install MySQL 5.7.37 on windows10
[training Day10] linear [mathematics] [thinking]
Valdo2021 - vascular space segmentation in vascular disease detection challenge (2)
Wechat stores build order pages and automatically grab tickets
英文翻译中文常见脏话
Leetcode 146: LRU cache
Introduction to fastdfs high availability
Alibaba sentinel basic operation
[training Day8] [luogu_p6335] staza [tarjan]
(posted) differences and connections between beanfactory and factorybean
Modulenotfounderror: no module named 'pysat.solvers' (resolved)
Synthesis of peptide nucleic acid PNA labeled with heptachydrin dye cy7 cy7-pna
Thymeleaf application notes
872. Maximum common divisor
API data interface for historical data of A-share index
[trial experience of Yuxin micro Wiota ad hoc network protocol development kit] RT thread BSP Software package production