Microsoft open source Python automation artifact Playwright! Don't write a line of code!


Hello, I'm Mu Mu.

I believe that friends who have played reptiles know selenium, an artifact tool for automatic testing. Writing a Python automated script to free your hands is basically a routine operation. If a crawler can't climb, use automated testing.

Although selenium has complete documents, it also needs a certain learning cost. For a pure Xiaobai, there is still some threshold.

Microsoft has opened a project called "playwright Python", which is a blockhouse! This project is a pure automation tool for Python language. You can realize the automation function without even writing code.

You may think it's a little incredible, but it's so powerful. Let's take a look at this artifact.

1. Playwright introduction

Playwright is a powerful Python library. It can automatically execute the automation operations of mainstream browsers such as Chromium, Firefox and WebKit with only one API, and supports running in headless mode and headless mode at the same time.

The automation technology provided by Playwright is green, powerful, reliable and fast, and supports Linux, Mac and Windows operating systems.

Use playwright 2

install

Playwright installation is very simple, two-step.

# Install playwright Library
pip install playwright

# Install browser driver files (the installation process is a little slow)
python -m playwright install

The above two pip operations are installed separately:

  • Python 3.0 is required to install the Playwright dependency library 7+
  • Install the driver files of chrome, Firefox, WebKit and other browsers

Recording

There is no need to write a line of code to use Playwright. We just need to manually operate the browser, which will record our operations and automatically generate code scripts.

Here is the recorded command codegen, just one line.

# On the command line, type -- help to see all options
python -m playwright codegen

The usage of codegen can be viewed with -- help. If it is simple to use, it is to add url links directly after the command. If there are other needs, you can add 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 means:

  • -o: Save the recorded script to a file
  • – target: Specifies the language for generating scripts, including JS and Python. The default is Python
  • -b: Specify browser driver

For example, I want to be in Baidu Com search, driven by chromium, save the results as my python file of py.

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

After entering the command line, the browser will be opened automatically, and then you can see that every move on the browser will be automatically translated into code.

Automatically close the browser after completion and save the generated automation script to 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=\"JD.COM\"")
  
    # Click //a[normalize-space(.)=' JD.COM Com official website, more, faster, better and more economical, just for quality life ']
    with page.expect_navigation():
        with page.expect_popup() as popup_info:
            page.click("//a[normalize-space(.)=' JD.COM Com official website, more, faster, better and more economical, just for quality life '] ")
        page1 = popup_info.value
    # ---------------------
    context.close()
    browser.close()

with sync_playwright() as playwright:
    run(playwright)

In addition, playwright also provides synchronous and asynchronous API interfaces.

synchronization

The following example code: open three browsers in turn, go to baidu to search, take a screenshot and exit.

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 operation can be combined with asyncio to perform three browser operations at the same time.

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 terminal

What's more, playwright can also support browser simulation on mobile terminals. The following is a piece of code provided by the official document to simulate the Safari browser on the iphone 11 pro in a given geographical location. First, navigate to maps google. COM, then perform location and 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 plug-in. If you are interested, you can try it yourself.

3. Summary

playwright has many advantages over existing automated testing tools, such as:

  • Cross browser, support Chromium, Firefox and WebKit
  • Cross operating system, support Linux, Mac and Windows
  • It can provide the function of recording and generating code to liberate both hands
  • Can be used for mobile terminal

At present, the disadvantage is that the ecology and documentation are not very complete. For example, there are no good tutorials and examples for learning. However, I believe that as more and more people know, the future will be better and better.

Finally, thank you for reading. Each of your likes, comments and sharing is our greatest encouragement. Refill ~

If you have any questions, please discuss them in the comment area!

Keywords: Python Back-end Open Source

Added by serenade2 on Sun, 30 Jan 2022 13:12:29 +0200