Recently, Microsoft open-sourced a project called " playwright-python
", which is simply a bunker! This project is Python
a pure automation tool for the language, which can achieve automation without even writing code.
You might think it's a little weird, but it's just that powerful. Let's take a look at this artifact.
1. Introduction to Playwright
Playwright
It is a powerful Python library that uses only one API to automatically execute Chromium
, Firefox
, , WebKit
and other mainstream browser automation operations, and supports running in headless mode and head mode at the same time.
The automation technology provided by Playwright is green, powerful, reliable and fast, support Linux
, Mac
and Windows
operating system.
2. Playwright use
Install
Playwright
The installation is very simple, two steps.
# 安装playwright库
pip install playwright
# 安装浏览器驱动文件(安装过程稍微有点慢)
python -m playwright install
The above two pip operations are installed separately:
-
Install Playwright dependency library, requires Python3.7+
-
Install driver files for browsers such as Chromium, Firefox, and WebKit
record
No need to write a Playwright
line of code, we only need to manually operate the browser, it will record our operations, and then automatically generate code scripts.
Below is the recorded command codegen
, just one line.
# 命令行键入 --help 可看到所有选项
python -m playwright codegen
codegen
The usage can be --help
viewed. If it is used simply, add the url link directly after the command. If there are other needs, you can add it options
.
python -m playwright codegen --help
Usage: index codegen [options] [url]
open page and generate code for user actions
Options:
-o, --output <file name> saves the generated script to a file
--target <language> language to use, one of javascript, python, python-async, csharp (default: "python")
-h, --help display help for command
Examples:
$ codegen
$ codegen --target=python
$ -b webkit codegen https://example.com
options meaning:
-
-o: save the recorded script to a file
-
--target: specifies the language for generating the script, there are
JS
twoPython
, and the default is Python -
-b: Specifies the browser driver
For example, I want to baidu.com
search, use the chromium
driver, and save the results as my.py
a python
file.
python -m playwright codegen --target python -o 'my.py' -b chromium https://www.baidu.com
After the command line is entered, the browser will be automatically opened, and then you can see that every move on the browser will be automatically translated into code, as shown below.
After the end, the browser is automatically closed, and the generated automation script is saved to the py file.
from playwright import sync_playwright
def run(playwright):
browser = playwright.chromium.launch(headless=False)
context = browser.newContext()
# Open new page
page = context.newPage()
page.goto("https://www.baidu.com/")
page.click("input[name=\"wd\"]")
page.fill("input[name=\"wd\"]", "jingdong")
page.click("text=\"京东\"")
# Click //a[normalize-space(.)='京东JD.COM官网 多快好省 只为品质生活']
with page.expect_navigation():
with page.expect_popup() as popup_info:
page.click("//a[normalize-space(.)='京东JD.COM官网 多快好省 只为品质生活']")
page1 = popup_info.value
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
Synchronize
The following sample code: open three browsers in turn, go to baidu search, and exit after taking a screenshot.
from playwright import sync_playwright
with sync_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = browser_type.launch()
page = browser.newPage()
page.goto('https://baidu.com/')
page.screenshot(path=f'example-{browser_type.name}.png')
browser.close()
asynchronous
Asynchronous operations can be combined asyncio
with three simultaneous browser operations.
import asyncio
from playwright import async_playwright
async def main():
async with async_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = await browser_type.launch()
page = await browser.newPage()
await page.goto('http://baidu.com/')
await page.screenshot(path=f'example-{browser_type.name}.png')
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
mobile
What's more, it playwright
also supports browser emulation on the mobile terminal. Below is a piece of code provided by the official documentation, which simulates the Safari browser on the mobile phone iphone 11 pro at a given geographic location, first navigate to maps.google.com
, then perform the positioning and take a screenshot.
from playwright import sync_playwright
with sync_playwright() as p:
iphone_11 = p.devices['iPhone 11 Pro']
browser = p.webkit.launch(headless=False)
context = browser.newContext(
**iphone_11,
locale='en-US',
geolocation={ 'longitude': 12.492507, 'latitude': 41.889938 },
permissions=['geolocation']
)
page = context.newPage()
page.goto('https://maps.google.com')
page.click('text="Your location"')
page.screenshot(path='colosseum-iphone.png')
browser.close()
In addition, it can also be used with pytest
plugins, if you are interested, you can try it yourself.
3. Summary
playwright
Compared with existing automated testing tools, it has many advantages, such as:
- Cross-browser, supports Chromium, Firefox, WebKit
- Cross operating system, support Linux, Mac, Windows
- It can provide the function of recording and generating code, freeing hands
- Available for mobile
The current disadvantage is that the ecology and documentation are not very complete. For example, there is no API Chinese documentation, no good tutorials and examples for learning. But I believe that as more and more people know, the future will get better and better.
Today's content is shared here, and the editor finally prepared a python spree for everyone [Jiajun Yang: 419693945] to help everyone learn better!