小言_互联网的博客

Web自动化测试[playwright概述]

271人阅读  评论(0)

微软开源了一款Python自动化依赖库:playwright-python,它支持主流的浏览器,同时支持以无头模式、有头模式运行;playwright-python还提供了同步、异步的 API,可以结合 Pytest 测试框架使用;支持python、C#、JS等;详情可以通过Github:playwright-pythonAPI:playwright-pythonDocs:playwright-python查看

playwright 特色

  • Ever-green browser engines
  • Headless execution
  • Scenarios that span multiple pages, domains and iframes
  • Auto-wait for elements to be ready before executing actions (like click, fill)
  • Reliable signals instead of timeouts (like network response, popup, navigation)
  • Intercept network activity for stubbing and mocking network requests
  • Emulate mobile devices, geolocation, permissions
  • Support for web components via shadow-piercing selectors
  • Native input events for mouse and keyboard
  • Upload and download files
  • Language to use, one of javascript, python, python-async, csharp (default: “python”)

安装playwright

C:\Users\Administrator>pip3 install playwright
Collecting playwright
  Downloading playwright-0.162.2-py3-none-win32.whl (45.8 MB)
     |████████████████████████████████| 45.8 MB 33 kB/s
Collecting greenlet==1.0a1
  Downloading greenlet-1.0a1-cp37-cp37m-win32.whl (91 kB)
     |████████████████████████████████| 91 kB 13 kB/s
Collecting pyee>=8.0.1
  Downloading pyee-8.1.0-py2.py3-none-any.whl (12 kB)
Collecting typing-extensions
  Downloading typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Installing collected packages: typing-extensions, pyee, greenlet, playwright
  Attempting uninstall: greenlet
    Found existing installation: greenlet 0.4.17
    Uninstalling greenlet-0.4.17:
      Successfully uninstalled greenlet-0.4.17
Successfully installed greenlet-1.0a1 playwright-0.162.2 pyee-8.1.0 typing-extensions-3.7.4.3

C:\Users\Administrator>pip3 install playwright
Requirement already satisfied: playwright in c:\python378\lib\site-packages (0.162.2)
Requirement already satisfied: typing-extensions in c:\python378\lib\site-packages (from playwright) (3.7.4.3)
Requirement already satisfied: pyee>=8.0.1 in c:\python378\lib\site-packages (from playwright) (8.1.0)
Requirement already satisfied: greenlet==1.0a1 in c:\python378\lib\site-packages (from playwright) (1.0a1)

安装浏览器驱动

Downloading chromium v827102 - 86.1 Mb [====================] 100% 0.0s
chromium v827102 downloaded to C:\Users\Administrator\AppData\Local\ms-playwright\chromium-827102
Downloading firefox v1205 - 74.8 Mb [====================] 100% 0.0s
firefox v1205 downloaded to C:\Users\Administrator\AppData\Local\ms-playwright\firefox-1205
Downloading webkit v1383 - 51.4 Mb [====================] 100% 0.0s
webkit v1383 downloaded to C:\Users\Administrator\AppData\Local\ms-playwright\webkit-1383

实例使用

脚本录制

C:\Users\Administrator>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

实际执行一条命令

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

Google浏览器会自动弹出并打开百度页面

在页面上做一些操作,都会被记录在命令行窗口中,然后在C:\Users\Administrator下便可以找到自动生成的脚本文件

from playwright import sync_playwright

def run(playwright):
    browser = playwright.chromium.launch(headless=False)
    context = browser.newContext()

    # Open new page
    page = context.newPage()

    # Go to https://www.baidu.com/
    page.goto("https://www.baidu.com/")

    # Click input[name="wd"]
    page.click("input[name=\"wd\"]")

    # Fill input[name="wd"]
    page.fill("input[name=\"wd\"]", "davieyang")

    # Go to https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=davieyang&fenlei=256&rsv_pq=bd7fdf76000822b7&rsv_t=01963XhEzymZUudteU%2BMRAbE5nBWJjrYhyMTVUz3hq7j0bU5Z2gZAQ5uRnQ&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_sug3=11&rsv_sug1=6&rsv_sug7=100&rsv_btype=i&inputT=3810&rsv_sug4=5977
    page.goto("https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=davieyang&fenlei=256&rsv_pq=bd7fdf76000822b7&rsv_t=01963XhEzymZUudteU%2BMRAbE5nBWJjrYhyMTVUz3hq7j0bU5Z2gZAQ5uRnQ&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_sug3=11&rsv_sug1=6&rsv_sug7=100&rsv_btype=i&inputT=3810&rsv_sug4=5977")

    # Go to https://github.com/davieyang/
    page1.goto("https://github.com/davieyang/")

    # Close page
    page1.close()

    # Close page
    page.close()

    # ---------------------
    context.close()
    browser.close()

with sync_playwright() as playwright:
    run(playwright)

同步sync_playwright

from time import sleep

from playwright import sync_playwright

# 注意:默认是无头模式

with sync_playwright() as p:
    # 分别对应三个浏览器驱动
    for browser_type in [p.chromium, p.firefox, p.webkit]:

        # 指定为有头模式,方便查看
        browser = browser_type.launch(headless=False)
        page = browser.newPage()
        page.goto('http://baidu.com')

        # 执行一次搜索操作
        page.fill("input[name=\"wd\"]", "AirPython")
        with page.expect_navigation():
            page.press("input[name=\"wd\"]", "Enter")

        # 等待页面加载完全
        page.waitForSelector("text=百度热榜")

        # 截图
        page.screenshot(path=f'example-{browser_type.name}.png')

        # 休眠5s
        sleep(5)

        # 关闭浏览器
        browser.close()

异步async_playwright

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(headless=False)
            page = await browser.newPage()

            await page.goto('http://baidu.com')

            # 执行一次搜索操作
            await page.fill("input[name=\"wd\"]", "AirPython")
            await page.press("input[name=\"wd\"]", "Enter")

            # 等待页面加载完全
            await page.waitForSelector("text=百度热榜")

            # 截图
            await page.screenshot(path=f'example-{browser_type.name}.png')

            await browser.close()


asyncio.get_event_loop().run_until_complete(main())

转载:https://blog.csdn.net/dawei_yang000000/article/details/110817400
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场