Playwright is a new generation of automated testing tools released by Microsoft in early 2020, compared to the most commonly used Selenium, it only uses an API to automate Chromium, Firefox, WebKit and other major browser automation operations. As a pure automation tool for Python language, it can be automated faster in regression testing.
1. Why Playwright
1.1 Strengths of Playwright
(1) Selenium needs to operate the browser through WebDriver; Playwright interacts with the browser through developer tools, which is simple to install and does not require the installation of various drivers.
(2) Playwright supports almost all languages and does not rely on various drivers, and starts faster by calling the built-in browser.
(3) Selenium is based on HTTP protocol (one-way communication), Playwright is based on Websocket (two-way communication) can automatically get the actual browser.
(4) Playwright is automatically waiting.
- Wait for element to appear (automatically waits 30s when positioning the element, wait time can be customized in milliseconds)
- Waiting for an event to happen
1.2 Known limitations
(1) Playwright does not support older versions of Microsoft Edge or IE11. newer Microsoft Edge is supported (on Chromium); so projects with hard requirements for browser versions are not applicable.
(2) Websites that require SSL certificates for access may not be recorded, and the process needs to be written in a separate location.
(3) Mobile testing simulates a mobile device through a desktop browser (equivalent to a self-contained emulator), with no control over the real machine.
2. Use of Playwright
2.1 Installation
(1) Install the Playwright dependency library (Playwright supports Async\Await syntax, so it requires Python 3.7+)
pip install playwright
(2) Installation of driver files for Chromium, Firefox, WebKit and other browsers (built-in browsers)
python -m playwright install
2.2 Automatic recording
(1) Type --help at the command line to see all the postable options.
python -m playwright codegen --help
(2) Start recording from the start page.
python -m playwright codegen /
(3) Open, using the Chromium driver, the python file to which the results are saved as
python -m playwright codegen --target python -o '' -b chromium /
-target: specify the language to generate the script, there are two kinds of JS and Python, the default is Python.
-b: Specify the browser driver
-o: save the recorded script to a file
2.3 Customized authoring
(1) Element positioning
- Selects a single element: querySelector(engine=body)
- Select multiple elements: querySelectorAll(engine=body)
- Selects a single element and waits automatically: waitForSelector(engine=body)
By's 8 ways of positioning, actually 4
- id, name, tag name, class name (java and pythona categorize all 4 as CSS)
- xpath、link text、partial link text、css selector
The W3C standard specifies the webDriver protocol as five positioning methods
- CSS、Link text、Partial link text、Tag name、XPath
Playwright summarizes the selectors into 3 categories
- CSS, XPATH (supports logical expressions and functions), TEXT
(2) Selector rules
- CSS: ID selectors, class selectors, element selectors, attribute selectors, wildcard selectors, hierarchical selectors.
- XPath: XML path language, through the "path identifier", navigation XML document, in the kind of XML (HTML) can also be used.
- Text: structured content (html, xml, json) using fuzzy matching (ignore case, ignore spaces, search for substrings) and exact matching, unstructured content using regular matching.
(3) Common operations on elements
- Dropdown selection box: selectOpion, value, labei, index
- File uploads: setInputFiles, single file, multiple files, drag and drop uploads
- Mouse click: click, dbclick
- Mouse drag: down, up
- Mouse movement: move
- Touch screen: tag
- Keyboard keys: press
- Screenshot, Record: screenshot, recordVideo
2.4 Network Intercept (Mock Interface), example below:
page = () def Whether_intercept() -> bool: return True # Make an interception # return False # no interception def handler(route:Route): print() # Normal access # route.continue_() # Denial of access # ("Network Intercept") # Redirected to non-target address ( status=302, headers={ 'Location' : "/" } ) (Whether_intercept,handler)
2.5 Synchronized execution, as exemplified below:
# Open all three browsers in turn, go to the Walker website, take a screenshot and exit. from playwright import sync_playwright with sync_playwright() as p: for browser_type in [, , ]: # Designated as header mode, Ture as headerless mode browser = browser_type.launch(headless=False) page = () ('/') # Screenshot after waiting for the page to load completely ("text=Intelligent Content Audit") (path=f'example-{browser_type.name}.png') ()
2.6 Asynchronous execution, as exemplified below:
#Three simultaneous browser operations import asyncio from playwright import async_playwright async def main(): async with async_playwright() as p: for browser_type in [, , ]: browser = await browser_type.launch() page = await () await ('/') await ("text=Intelligent Content Audit") await (path=f'example-{browser_type.name}.png') await () asyncio.get_event_loop().run_until_complete(main())
2.7 Pytest combination, example below:
Installation:pip install pytest-playwright
def test_playwright(page): ("/") with page.expect_popup() as popup_info: ('text="Intelligent Content Review"') assert "Intelligent Content Review" == ()
2.8 Mobile operation, example below:
Currently fewer models are supported for emulation, see: Emulation Device List
from time import sleep from playwright import sync_playwright with sync_playwright() as p: GalaxyS5 = ['Galaxy S5'] browser = (headless=False) context = (**GalaxyS5) page = () ('/') ('text="Intelligent Content Review"') # Screenshots # (path='') sleep(10) ()
3. Summary
Playwright as a new generation of automation testing tools, compared to Selenium whether it is easy to use, or practical have been improved in all aspects. To do a simple but not simple, I believe that the use of the tool can help us to improve the efficiency of automation work.
This article on Playwright Quick Start Guide (Getting Started Tutorial) is introduced to this article, more related Playwright Getting Started content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!