当前位置:网站首页>Automated test tool playwright (quick start)
Automated test tool playwright (quick start)
2022-07-26 18:35:00 【wangmcn】
Automated test tool
Playwright( Quick start )
Catalog
1、 Preface
2、 brief introduction
3、 install
4、 Interactive mode
5、 Recording mode
6、 Writing mode
1、 Preface
At the mention of UI Automated test tool , The first recommendation must be Selenium, Its advantage lies in cross platform 、 Cross language 、 Fully open source 、 There are no restrictions on business users 、 Support distributed 、 Have mature communities and learning documents , It has been iteratively updated to 4 edition . Then there are shortcomings , For example, environment configuration 、 Low loading efficiency 、 Slow running speed, etc .
except Selenium Is there no other tool recommended ? Of course not. , There are also many excellent tools , such as Cypress、Robot Framework etc. .
This article will introduce another powerful and easy-to-use UI Automated test tool -Playwright.
2、 brief introduction
Microsoft open source automated testing tool Playwright, Support for mainstream browsers , Include :Chrome、Firefox、Safari etc. , It also supports headless mode 、 Head mode operation , And provides synchronization 、 Asynchronous API, Can combine Pytest The test framework uses , It also supports automatic script recording and other functions on the browser side .
characteristic :
1、 Cross browser .Playwright Support all modern rendering engines , Include Chromium、WebKit and Firefox.
2、 Cross platform . stay Windows、Linux and macOS Local or CI、 Headless or headless test .
3、 Cross language . stay TypeScript、JavaScript、Python、.NET、Java Use in Playwright API.
4、 Test the mobile network . Apply to Android and Mobile Safari Of Google Chrome Native mobile simulation . The same rendering engine applies to your desktop and cloud .
Official website address :
https://playwright.dev
GitHub Address :
https://github.com/microsoft/playwright
3、 install
Playwright Support for cross language , This article will use Python Explain .
First install Python( Yes Python Environmental Science ).
Then open the command line , Enter the installation command .
pip install --upgrade pip
pip install playwright
playwright install4、 Interactive mode
Playwright Support interaction mode , That is, run single line code or code block , It can give the running result immediately .
because Playwright Support synchronous and asynchronous API, Then you should first understand what is synchronous and asynchronous ?
Sync , After executing a function or method , Wait for the system to return a value or message , At this point, the program is blocking , Only after receiving the returned value or message, can other commands be executed .
asynchronous , After executing a function or method , There is no need to block waiting for return values or messages , Just delegate an asynchronous procedure to the system , So when the system receives a return value or message , The system will automatically trigger the asynchronous process of the delegation , To complete a complete process .
Next, open the browser by operation , Visit Baidu Homepage , Take closing the browser as an example .
1、 Synchronization command
Open the command line , Input python
Enter into Python In interactive mode , Enter the following command :
from playwright.sync_api import sync_playwright
playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()
page.goto("https://www.baidu.com/")
browser.close()
playwright.stop()Enter the command to visit Baidu homepage , The browser page will jump to Baidu home page at the same time , At the same time, the command line outputs the response and request information .
2、 Asynchronous command
Open the command line , Input python -m asyncio
Enter into Python In interactive mode , Enter the following command :
from playwright.async_api import async_playwright
playwright = await async_playwright().start()
browser = await playwright.chromium.launch(headless=False)
page = await browser.new_page()
await page.goto("https://www.baidu.com/")
await browser.close()
await playwright.stop()Enter the command to visit Baidu homepage , The browser page will also jump to Baidu homepage , The command line will also output response and request information .
5、 Recording mode
Playwright With command line tools ( Recording function ), It can be used to record user interactions and generate code (Java、Python etc. ). In fact, it is similar to Selenium IDE.
1、 General recording
Open the command line , Input
playwright codegen baidu.comOpen browser automatically , And jump to Baidu homepage .
At the same time, the recording window also pops up , You can see that it is recording , The scripting language is Python.
By manipulating the ( Click on 、 Input, etc ) Browser page , The script will also automatically add operation steps .
Besides , The recording tool can also get the location of elements . Click stop recording , Then click Explore after , Click the element you want to locate on the page , You can get the value of the element location .
Finally, copy the recorded script , It can be adjusted appropriately .
Adjusted script code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# official account :AllTests software test
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
# Open new page
page = context.new_page()
# Go to https://www.baidu.com/
page.goto("https://www.baidu.com/")
# Click input[name="wd"]
page.locator("input[name=\"wd\"]").click()
# Fill input[name="wd"]
page.locator("input[name=\"wd\"]").fill(" automated testing ")
# Click text= use Baidu Search
page.locator("text= use Baidu Search ").click()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)2、 Simulate mobile device recording
open It can simulate mobile devices and tablet devices
Simulation, for example iPhone 13 Visit the author's CSDN.
playwright open --device="iPhone 13" blog.csdn.net/wangmcnPictured : simulation iPhone 13 The effect of opening the browser .
6、 Writing mode
Use IDE( Such as PyCharm、Visual Studio Code etc. ) Write code and run programs .
1、 Launch the browser ( Headless mode )
Playwright You can launch chromium、firefox、webkit Any kind of .
The example operation is as follows , Open the browser 、 Jump to Baidu 、 Screen capture 、 Output page title 、 Close the browser .
Script code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# official account :AllTests software test
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://www.baidu.com/")
page.screenshot(path="example.png")
print(page.title())
browser.close()Running results : Console output page title .
2、 Launch the browser ( Head mode )
By default ,Playwright Run the browser in headless mode . To view the browser UI( Head mode ), Please pass it when you start the browser headless=False sign , You can also use slow_mo To slow down execution .
Script code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# official account :AllTests software test
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=False, slow_mo=50)
page = browser.new_page()
page.goto("https://www.baidu.com/")
page.screenshot(path="example.png")
print(page.title())
browser.close()3、 asynchronous
Playwright Support API Two variants of : Synchronous and asynchronous .
Asynchronous Support , If your project uses asyncio, Should be used async API.
Script code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# official account :AllTests software test
import asyncio
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
await page.goto("https://www.baidu.com/")
print(await page.title())
await browser.close()
asyncio.run(main())边栏推荐
- mpc5744p烧录到98%无法继续下载程序
- 14. Gradient detection, random initialization, neural network Summary
- The step jumping expansion problem of sword finger offer
- Leetcode 50 day question brushing plan (day 1 - add two numbers 11.00-12.30)
- SQL判断某列中是否包含中文字符、英文字符、纯数字,数据截取
- 详解 gRPC 客户端长连接机制实现
- Leetcode 0139. word splitting
- LeetCode50天刷题计划(Day 3—— 串联所有单词的子串 10.00-13.20)
- 455. 分发饼干【双指针 ++i、++j】
- Download and configuration of irrklang audio library
猜你喜欢

Meta Cambria手柄曝光,主动追踪+多触觉回馈方案

ssm练习第二天_项目拆分moudle_基本增删改查_批量删除_一对一级联查询

Redis persistent rdb/aof
![[a little knowledge] thread pool](/img/47/7296e47b53e728d2d3b9db198243f4.png)
[a little knowledge] thread pool

SSM practice day 5

14. Gradient detection, random initialization, neural network Summary

During the oppo interview, 16 questions were thrown over. I was stupid

Maximum sum of continuous subarray of sword finger offer (2)

J9数字论:如何避免踩雷多头陷阱?

8.2 一些代数知识(群、循环群和子群)
随机推荐
还在用Xshell?推荐这个更现代的终端连接工具
剑指offer 连续子数组的最大和(二)
Apartment rental system based on JSP
China polyisobutylene Market Research and investment value report (2022 Edition)
Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection
Linked list - reverse linked list
剑指offer 正则表达式匹配
Continue to work hard on your skills, and the more you learn, the more you will learn
If the recommendation effect is not satisfactory, it's better to try to learn the propeller chart
Duplicate gallerycms character length limit short domain name bypass
[brother hero July training] day 25: tree array
How to assemble a registry
SSH based online mall
贪心——455. 分发饼干
.net CLR GC dynamic loading transient heap threshold calculation and threshold excess calculation
剑指offer 跳台阶扩展问题
Arm中国回应“断供华为”事件!任正非表示“没有影响”!
14. Gradient detection, random initialization, neural network Summary
Understand in depth why not use system.out.println()
ICML 2022 (Part 4) | | graph hierarchical alignment graph kernel to realize graph matching