当前位置:网站首页>Actual combat | UI automation test framework design and pageobject transformation
Actual combat | UI automation test framework design and pageobject transformation
2022-06-12 23:43:00 【Hua Weiyun】
This article is excerpted from Hogwarts 《 Advanced test and development 》 The teaching content of the course , Add groups at the end of the text .
stay UI Automated testing process , Facing complex business scenarios , We often encounter such challenges :
- Simple recording / Fast playback speed , But can't adapt to complex scenes ;
- Writing automated test scripts is more flexible , But the workload is heavy and the maintainability is poor ;
- Previous packaging technologies (PageObject) Can adapt to all kinds of UI scene , But the structure is loose , Cannot migrate in multiple projects ;
therefore , A testing team often needs to customize a testing framework , To make up for the shortcomings of the existing framework .
Test framework encapsulation idea
because UI The automated testing framework revolves around UI Interface to use , therefore , Still choose PageObject Design patterns for UI And test for packaging , Simultaneous coordination Pytest Unit test scripts can be effectively organized 、 Coherent application , So as to improve the maintainability and readability of the framework .
Because the test framework is based on PageObject Design patterns , The main direction is PO improvement , Data driven , Exception handling , such as :
- Data driven test data : Store data externally yaml In file , utilize yaml Tool for data reading ;
- Data driven of data steps : Put the operation steps outside yaml In file , utilize yaml The tool reads the operation steps , Analyze and implement the operation steps with special functions ;
- Automatic exception handling mechanism : Encapsulate and improve the element search module , Including how to deal with pop ups ;
Page_Object reform
As a universal UI The test framework , PageObjet Not just for Web automated testing , It also applies to Appium Mobile automation test , Its advantages are as follows :
- Reduce code duplication
- Improve the readability of test cases
- Improve maintainability of test cases
PO Examples of transformation ( Based on snowball App)
This case will be about snowball App Conduct Page Objetct Packaging and improvement .
When starting the snowball App when , Will enter the home page . Click the search box to enter the search page , Search for a stock and determine whether the share price is greater than 200:
PageObjetct The module relationship is as follows , All modules should inherit BasePage , App Implement start , restart , Stop and wait operation , Main Enter the search page , Enter the stock page and other operations :
base_page Modules are all page The parent of a class , It defines the public method , For example, encapsulate the following find After the method , You can let subclasses call find :
from appium.webdriver.webdriver import WebDriverclass BasePage: _driver: WebDriver def __init__(self, driver: WebDriver = None): self._driver = driver def find(self, locator, value: str = None): # If it comes in tuple, Just use one parameter :locator if isinstance(locator, tuple): return self._driver.find_element(*locator) else: return self._driver.find_element(locator, value)App Module encapsulation app Start of , restart , Stop, etc , When app When starting, it will enter main page , So in the following main Methods return Main ,Main The definition of class will be explained later :
from appium import webdriverfrom test_appium.page.base_page import BasePagefrom test_appium.page.main import Mainclass App(BasePage): # Appoint app The package name and activity name _package = "com.xueqiu.android" _activity = ".view.WelcomeActivityAlias" def start(self): # If driver If it is empty, initialize if self._driver is None: caps = {} caps["platformName"] = "android" caps["deviceName"] = "hogwarts" caps["appPackage"] = self._package caps["appActivity"] = self._activity caps["noReset"] = True # initialization driver self._driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) self._driver.implicitly_wait(30) # If driver Not empty , Then start directly activity else: print(self._driver) self._driver.start_activity(self._package, self._activity) return self def restart(self): pass def stop(self): pass def main(self) -> Main: # When app Startup time , Jump to ( Instantiation )Main return Main(self._driver)Main The module is the first page PageObject , The method encapsulates the important functions of the home page , For example, in the following code goto_search_page Encapsulated click search and jump to Search page :
from appium.webdriver.common.mobileby import MobileByfrom selenium.webdriver.common.by import Byfrom test_appium.page.base_page import BasePagefrom test_appium.page.profile import Profilefrom test_appium.page.search import Searchclass Main(BasePage): # After clicking the search button , Go to the search page def goto_search_page(self): self.find(MobileBy.ID, "tv_search").click() # Go to the search page return Search(self._driver) def goto_stocks(self): pass def goto_trade(self): pass def goto_messages(self): passSearch The module can search for a stock , You can also get the price of the stock , For example, below :
The encapsulation code is as follows :
from appium.webdriver.common.mobileby import MobileByfrom selenium.webdriver.remote.webdriver import WebDriverclass Search: _driver: WebDriver def __init__(self, driver): self._driver = driver # Enter what you want to search def search(self, key: str): self._driver.find_element(MobileBy.ID, "search_input_text").send_keys(key) self._driver.find_element(MobileBy.ID, "name").click() return self # Get the stock price , Used to judge def get_price(self, key: str) -> float: return float(self._driver.find_element(MobileBy.ID, "current_price").text)Finally, test the above code , New test module test_search :
import pytestfrom test_appium.page.app import Appclass TestSearch: def setup(self): self.main = App().start().main() def test_search(self): assert self.main.goto_search_page().search("alibaba").get_price("BABA") > 200above , For your reference , Welcome to leave a message to discuss .
Get more information : Please add vx,ceshiren001
https://qrcode.ceba.ceshiren.com/link?name=article&project_id=qrcode&from=hwyun×tamp=1654998320&author=MM
边栏推荐
- 应用最广泛的动态路由协议:OSPF
- Industry reshuffle, a large number of programmers are going to lose their jobs? How can we break the current workplace dilemma
- Enterprise wechat H5_ Authentication, H5 application web page authorization login to obtain identity
- CS for mobile security [nethunter]
- Printf segment error (core dump): a problem caused by formatted output
- 如何让矢量瓦片配图神器maputnik支持 geoserver
- Embedded pipeline out of the box
- DETR(Detection with Transformers) 学习笔记
- 设计消息队列存储消息数据的 MySQL 表格
- LeetCode 890 查找和替换模式[map] HERODING的LeetCode之路
猜你喜欢

QT actual combat case (38) -- using qpprocess class to realize the function of starting process

array

Preparing for the Blue Bridge Cup Day11__ Basic operation of serial port communication

Redis realizes SMS verification code login

Ast, really fragrant

2202-简历制作

数组

MySQL基础篇视图的总结

SAP QM qp03 displays an inspection plan with multiple specs inspection features

The most widely used dynamic routing protocol: OSPF
随机推荐
Hongmeng starts 2
The most widely used dynamic routing protocol: OSPF
QT actual combat case (38) -- using qpprocess class to realize the function of starting process
Zhengzhou University of light industry -- development and sharing of harmonyos pet health system
For product managers, which of the two certificates, PMP and NPDP, is more authoritative?
2022年R2移动式压力容器充装考试题及在线模拟考试
基于Three.js海上风电数字孪生三维效果
2022年电工(初级)操作证考试题库及在线模拟考试
同花顺开证券账户怎么样?到底安不安全呢
Deep learning neural network: implementation method of convolution [direct method (no loss of precision), GEMM (matrix multiplication, no loss of precision), FFT (Fourier transform, loss of precision)
How does idea switch the interface to Chinese
Common message oriented middleware selection
Test platform series (97) perfect the case part
华为云弹性云服务器ECS使用【华为云至简致远】
Deep feature synthesis and genetic feature generation, comparison of two automatic feature generation strategies
M_ 8: Design a MySQL table for message queue to store message data
C language: how to give an alias to a global variable?
Teach you how to grab ZigBee packets through cc2531 and parse encrypted ZigBee packets
dict和set的基本操作
InfoQ geek media's 15th anniversary solicitation | brief introduction to the four challenges of building a micro service architecture




