当前位置:网站首页>10 minute quick start UI automation ----- puppeter
10 minute quick start UI automation ----- puppeter
2022-07-02 04:42:00 【Peng Yuyan in the testing industry】
This time, I will introduce you to the internal implementation UI A scheme of automated testing : Puppeteer+Mocha
Currently used for transferring books 、 luxury goods 、 Business and other businesses Web/H5 Online process monitoring of business ;
Just a quick introduction :
Mocha yes JavaScript A unit testing framework of
Puppeteer It's a Node library , It offers an advanced API To pass DevTools Protocol control Chromium
Compared with the familiar Appium programme , adopt Appium The server Control connected mobile devices Conduct UI operation
Puppeteer+Mocha The plan , Is in the Mocha In frame Write some js Code , call Puppeteer Start a browser and manipulate it to complete your UICase The operation of
If a worker wants to do a good job, he must sharpen his tools first , First, install the necessary tools
1. install node
https://nodejs.org/zh-cn/download/ Download and install directly
Configure environment variables
View version :
node -v
2. install npm (node It will be installed automatically npm, If installed node, Please ignore this step )
Install the latest version :
npm install [email protected] -g
View version :
npm -v
3. install cnpm (npm Failed to download package , choice cnpm install )
Carry out orders :
sudo npm install -g cnpm --registry=https://registry.npm.taobao.org ( Set Taobao image )
verification :
cnpm -v
4. Create a new one node engineering
npm i --save express
The root directory will appear : package.json、package-lock.json and node_modules Folder
Installation works depend on package.json increase mocha、puppeteer Version information
"mocha": "^8.3.2",
"puppeteer": "^1.18.0",
And then execute The following command , install puppeteer and mocha
npm install
The tools are installed ,case Write it down !!
First try the ox knife , Write a simple demo
Create a js file , Write the following code
const puppeteer = require('puppeteer');
async function test()
{
// Create a browser instance Browser object
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'], // Open headless
headless:false,
ignoreHTTPSErrors: true,
});
// Through the browser instance Browser Object creation page Page object
const page = await browser.newPage();
// adopt url Parameter to open the specified page
await page.goto('[https://m.zhuanzhuan.com/open/ZZBook/index.html?needHideHead=3#/book-home](https://m.zhuanzhuan.com/open/ZZBook/index.html?needHideHead=3#/book-home)');
// Take a screenshot of the page
await page.screenshot({path: 'homePage.png'});
// Close the browser
await browser.close();
};
module.exports = test
test()
Run the demo, You have completed a simple UICase
Then we can demo Edit your own based on case
First step : Get the of the element you want to operate selector
On the browser page Press F12 Open the debugging tool perhaps Right mouse button menu - Click on “ Check ”, View the page control tree
Choose the corresponding element , In the control tree - Right mouse button - Copy copy -selector
The second step : According to the following corresponding puppeteer Corresponding api, Enrich your uicase The operation of
// Page back
page.goBack([options])
// wait for xx Elements
page.waitForSelector(selector[, options])
// Jump
page.goto(url[, options])
// Set up cookies
page.setCookie(...cookies)
// Touch click operation
page.tap(selector)
// Mouse click operation
page.click(selector[, options])
//Puppeteer Chinese Manual
//https://zhaoqize.github.io/puppeteer-api-zh_CN/
Repeat the above two steps , You can realize your own UICase La
In addition, share a little white welfare Chrome plug-in unit :puppeteer recorder
It can automatically record the operation on the page , Generate puppeteer Code
visit https://cloud.tencent.com/developer/article/1470481 Download plug-ins
- The installation to Chrome In the expansion program , Turn it on
- After opening , In the upper right corner of the browser, there is a label of the video recorder , Click it and start recording , Window display Recording...
- Operate the page to execute your case technological process , After the operation, click the video recorder icon , Stop recording , Copy build puppeteer The code can be
case I can already write , So we use mocha The test framework will case Execute gracefully
because mocha Default execution test A file in a directory , So in test Create a new one in the directory caseList.js, As an executive document The file format is as follows :
const case1 = require('../cases/demo')
// describe Define the test suite name
describe(' Use case set ',function() {
before(function() {console.log(' Start to execute the use case ')});
// it Define a single test case
it('case1',async function() {
[//this.timeout](//this.timeout)(0);
await case1();});
it('case2',async function() {
[//this.timeout](//this.timeout)(0);
await case1();});
after(function() {console.log(' The use cases have been executed ')});
});
Mocha It also provides only,skip And other control case execution methods and hook functions before,after etc. .
Execute under the project path mocha command :
mocha test/caseList
You can get a concise test report
If you think this report is too concise , that mocha You can also generate visual reports First install the visual report plug-in
npm install --save-dev mochawesome
Re execution
mocha test/caseList --reporter mochawesome
You can be in case After execution , stay mochawesome-report Generate under directory HTML The test report of
Come here , You can use puppeteer+mocha Edit yourself uicase, And generate a good test report
Last
Puppeteer、Mocha It's more than that
The following is the supporting information , For doing 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you !
Last : It can be in the official account : Programmer Xiaohao ! Get a free copy of 216 Page software testing engineer interview guide document information . And the corresponding video learning tutorial is free to share !, It includes basic knowledge 、Linux necessary 、Shell、 The principles of the Internet 、Mysql database 、 Special topic of bag capturing tools 、 Interface testing tool 、 Test advanced -Python Programming 、Web automated testing 、APP automated testing 、 Interface automation testing 、 Testing advanced continuous integration 、 Test architecture development test framework 、 Performance testing 、 Safety test, etc. .
If my blog helps you 、 If you like my blog content , please “ give the thumbs-up ” “ Comment on ” “ Collection ” One button, three links ! Friends who like software testing , You can join our testing technology exchange group :779450660 There are various software testing resources and technical discussions )
边栏推荐
- Pytorch-Yolov5從0運行Bug解决:
- GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?
- AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
- 【提高课】ST表解决区间最值问题【2】
- Learn BeanShell before you dare to say you know JMeter
- Realize the function of data uploading
- 正大留4的主账户信息汇总
- 60后关机程序
- [C language] Dynamic Planning --- from entry to standing up
- There is no prompt for SQL in idea XML, and the dialect setting is useless.
猜你喜欢
Playing with concurrency: what are the ways of communication between threads?
Let正版短信测压开源源码
Vmware安装win10报错:operating system not found
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Analyze the space occupied by the table according to segments, clusters and pages
Markdown编辑语法
Exposure X8标准版图片后期滤镜PS、LR等软件的插件
Ognl和EL表达式以及内存马的安全研究
Cannot activate CONDA virtual environment in vscode
Why can't you remember when reading? Why can't you remember- My technology learning methodology
随机推荐
Rhcsa --- work on the fourth day
oracle 存储过程与job任务设置
Introduction to Luogu 3 [circular structure] problem list solution
CorelDRAW Graphics Suite2022免费图形设计软件
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
idea自動導包和自動删包設置
Let正版短信测压开源源码
Mysql表insert中文变?号的问题解决办法
CY7C68013A之keil编译代码
Play with concurrency: what's the use of interruptedexception?
How to write a client-side technical solution
CorelDRAW graphics suite2022 free graphic design software
Free drawing software recommended - draw io
What data does the main account of Zhengda Meiou 4 pay attention to?
[Yu Yue education] autumn 2021 reference materials of Tongji University
Pytoch --- use pytoch to predict birds
Design and implementation of general interface open platform - (44) log processing of API services
深圳打造全球“鸿蒙欧拉之城”将加快培育生态,优秀项目最高资助 1000 万元
win10 磁盘管理 压缩卷 无法启动问题
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market