Playwright automated testing tool quick actual combat

1.  Introduction

When it comes to automation, friends of testing and development know selenium. An artifact tool for automated testing, writing a Python automated script to free your hands is basically a routine operation. Although selenium has complete documentation, it also requires a certain learning cost. For a pure novice, there are still some thresholds. If you don’t want to spend a lot of time on learning positioning and writing scripts, you can try today’s protagonist: Microsoft’s open source project "playwright-python". This project is a purely automated tool for the Python language. You don’t even need to write code. , Can realize the automation function.

1.1 Basic introduction

The e2e test tool developed by Playwright Microsoft can provide faster and stronger functions than selenium, the next-generation end-to-end test tool. v1.0 was released on May 6, 2020. It is a node.js program. And playwright-python is playwright's python client, allowing python code to write use cases and call playwright to complete the test

Why use playwright? There are many sayings on the Internet, so I won’t list them one by one. What attracts me most is the three characteristics:

1. Support browser-side recording, generate automated scripts, and support headless running scripts

2. Fast speed, basically several times that of selenium, and supports browser asynchronous operation

3. Automatically wait for API, can intercept request, mock at will

2.  Quick to use

2.1 Installation

Project address: https://github.com/microsoft/playwright-python

Note: The python version is at least 3.7 and above when required, and 3.8 and above are recommended

Installation command:

pip install playwright (use Ali source, download speed is faster)

image.png

Remarks: pip install playwright==1.9.0 (it is recommended to use the version after 1.9.0, add debugging, very cool)

 

python -m playwright install (安装chromium、frefox、webkit)


image.pngimage.png

Note: It is not necessary to install the browser driver separately like selenium, it will install the browser driver file during pip install.

2.1.1 Download speed-up plan

In the second step, you can see that it has mainly downloaded the kernels of the three browsers. You can directly store the corresponding files downloaded in advance in this path and you don't need to download them.

The address is as follows:

C:\Windows\System32\config\systemprofile\AppData\Local\ms-playwright

Download package address:

https://kdocs.cn/l/crj83h6w0YDl

[Jinshan Document] ms-playwright.zip

2.2 Recording use

Recording code: python -m playwright codegen

playwright is very powerful, type --help on the command line to see all the options.

image.png


Specific meaning:

-h show all commands

--Target in what language to generate

-o save the script as a file

Use Baidu to give a simple example:

python -m playwright codegen --target python -o binyuTest.py' -b chromium https://www.baidu.com

The Baidu webpage will be opened, and the recorded code will be generated in the binyuTest.py file in the current directory. The length is too long and will not be shown temporarily. For specific operations, you can find detailed doc and api interfaces on https://playwright.dev/ Documentation

2.2.1 Advanced usage of recording code

–Save-storage and –load-storage is a very useful command, you can save the login status, load the login status

Use the recording as follows to save the login state file

python -m playwright codegen --target python -o formsavecookie.py -b chromium f.wps.cn/picker --save-storage formTest134

Use the recording as follows to load the login status file

python -m playwright codegen --target python -o formgetcookie.py -b chromium f.wps.cn/form-list  --load-storage formTest134

2.2.2 Recording video

The recordVideo parameter sets the video address and the size of the recording screen. dir must exist, otherwise it cannot be recorded; if size is not filled in, the default recording screen size will be recorded; if it is filled in, the area with the specified length and width will be recorded

image.png



2.3 Writing code

code show as below:

1. Use chrom, firefox, sarafi browsers to open the browser, then click Baidu, then take a screenshot of the search interface, and finally close the browser

image.png


note:

Ø with context manager, automatically close the browser and playwright, only one of Playwright-cli can be enabled

Ø browser.close() is manually closed, not necessary, but it is recommended that you close it manually

Ø Each Page is an instance, which solves the problem of page switching and is born with POM

2. Combined with asyncio, the above operations can be performed asynchronously at the same time.

image.png


Guess you like

Origin blog.51cto.com/xqtesting/2675964
Recommended