PlayWright安装及使用

发布时间 2023-12-25 11:27:31作者: 乐乐乐乐乐乐樂

PlayWright是由业界大佬微软(Microsoft)开源的端到端 Web 测试和自动化库,可谓是大厂背书,功能满格,虽然作为无头浏览器,该框架的主要作用是测试 Web 应用,但事实上,无头浏览器更多的是用于 Web 抓取目的,也就是爬虫。

PlayWright的安装和使用
pip3 install playwright
安装驱动

playwright可以安装支持的浏览器。运行不带参数的命令将安装默认浏览器

playwright install

可以通过提供参数来安装特定的浏览器

playwright install webkit

查看所有支持的浏览器

playwright install --help

默认会下载chromium内核,firefox以及webkit驱动

demo1,到该页面截图
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("https://cnblogs.com/luckyletop")
    page.screenshot(path="example.png")
    browser.close()
playwroght支持api的俩种变体:同步和异步
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("http://cnblogs.com/luckyletop")
        print(await page.title())
        await browser.close()

asyncio.run(main())

定位器

定位器是playwright自动等待和重试能力的核心部分。定位器代表了一种随时在页面上查找元素的方法。

page.get_by_role() 通过显式和隐式可访问性属性进行定位
page.get_by_text() 按文本内容定位
page.get_by_label()通过关联标签的文本定位表单控件
page.get_by_placeholder() 通过占位符定位输入
page.get_by_alt_text() 通过替代文本来定位元素 (一般指图像 具有alt描述图像的属性v)
page.get_by_title()通过元素的标题属性来定位元素
page.get_by_test_id()根据元素的data_testid属性定位元素
page.locator('css=button').click()   #通过css
page.locator('xpath=//button').click() #通过xpath

断言

Assertion Description
expect(locator).to_be_checked() Checkbox is checked 复选框被选中
expect(locator).to_be_disabled() Element is disabled 元素被禁用
expect(locator).to_be_editable() Element is editable 元素可编辑
expect(locator).to_be_empty() Container is empty 容器是空的
expect(locator).to_be_enabled() Element is enabled 元素已启用
expect(locator).to_be_focused() Element is focused 元素已聚焦
expect(locator).to_be_hidden() Element is not visible 元素不可见
expect(locator).to_be_visible() Element is visible 元素可见
expect(locator).to_contain_text() Element contains text 元素包含文本
expect(locator).to_have_attribute() Element has a DOM attribute 元素具有DOM属性
expect(locator).to_have_class() Element has a class property 元素具有类属性
expect(locator).to_have_count() List has exact number of children
expect(locator).to_have_css() Element has CSS property 列表具有css
expect(locator).to_have_id() Element has an ID 元素有一个id
expect(locator).to_have_js_property() Element has a JavaScript property
expect(locator).to_have_text() Element matches text 元素与文本匹配
expect(locator).to_have_value() Input has a value
expect(locator).to_have_values() Select has options selected
expect(page).to_have_title() Page has a title 页面有标题
expect(page).to_have_url() Page has a URL 页面有一个url
expect(response).to_be_ok() Response has an OK status 响应状态为ok

pytest-playwright

官方的pytest-playwright插件可以编写端到端的是。塔提供上下文隔离,开箱即用地在多个浏览器配置上运行。它继承了pytest框架,以及支持playwright的一些基础使用

pip install pytest-playwright

安装这个插件后,无需自定义fixture,直接使用内置的前置page即可

import re
from playwright.sync_api import Page, expect

def test_has_title(page: Page):
    page.goto("https://cnblogs.com/luckyletop")

    # Expect a title "to contain" a substring.
    expect(page).to_have_title(re.compile("乐乐"))
    
#说明:直接在用例函数里声明了一个page,格式:def test_has_title(page: Page),即可跳转访问页面
全局配置base_url
pip install pytest-base-url
pytest.ini

[pytest]
base_url = https://cnblogs.com
import re
from playwright.sync_api import Page, expect

def test_has_title(page: Page,base_url):
    page.goto(base_url+'/luckyletop')
    
#说明:配置后,可直接获取到base_url的,前提是先安装插件,然后在ptest.ini文件里配置