Playwright安装和简单演示

安装

Playwright是专门为满足端到端测试的需求而创建的。Playwright支持所有现代渲染引擎,包括Chromium,WebKit和Firefox。在本地或 CI 上在 Windows、Linux 和 macOS 上进行测试,在无头或以本机移动仿真为标题。

Playwright建议使用官方Playwright Pytest插件来编写端到端测试。它提供上下文隔离,开箱即用地在多个浏览器配置上运行它。或者,您可以使用该库通过首选测试运行程序手动编写测试基础结构。Pytest插件使用Playwright的同步版本,还有一个可通过库访问的异步版本。

首先安装 Playwright 并运行示例测试以查看其运行情况。

安装 Pytest 插件:

pip install pytest-playwright

安装所需的浏览器:

playwright install

添加示例测试

使用以下代码在当前工作目录或子目录中创建一个文件:test_my_application.py

import refrom playwright.sync_api 
import Page, expect
def test_homepage_has_Playwright_in_title_and_get_started_link_linking_to_the_intro_page(page: Page):    
    page.goto("https://playwright.dev/")    
# Expect a title "to contain" a substring.    
    expect(page).to_have_title(re.compile("Playwright"))    
    # create a locator      
    get_started = page.get_by_role("link", name="Get started")   
 # Expect an attribute "to be strictly equal" to the value.    
    expect(get_started).to_have_attribute("href", "/docs/intro")  
  # Click the get started link.    
    get_started.click()    # Expects the URL to contain intro.    
    expect(page).to_have_url(re.compile(".*intro"))

运行示例测试

默认情况下,测试将在铬上运行。这可以通过 CLI 选项进行配置。测试以无头模式运行,这意味着在运行测试时不会打开浏览器 UI。测试结果和测试日志将显示在终端中。

pytest

编写测试

PlayWright断言是专门为动态网络创建的。检查会自动重试,直到满足必要的条件。PlayWright内置自动等待功能,这意味着它会在执行操作之前等待元素可操作。PlayWright提供了一个期望函数来编写断言。

查看下面的示例测试,了解如何使用定位符和 Web 优先断言编写测试。

import refrom playwright.sync_api import Page, expectdef test_homepage_has_Playwright_in_title_and_get_started_link_linking_to_the_intro_page(page: Page):    page.goto("https://playwright.dev/")    # Expect a title "to contain" a substring.    expect(page).to_have_title(re.compile("Playwright"))    # create a locator    get_started = page.get_by_role("link", name="Get started")    # Expect an attribute "to be strictly equal" to the value.    expect(get_started).to_have_attribute("href", "/docs/intro")    # Click the get started link.    get_started.click()    # Expects the URL to contain intro.    expect(page).to_have_url(re.compile(".*intro"))

断言

PlayWright提供了期望功能,它将等待到满足预期条件。

import refrom playwright.sync_api 
import expectexpect(page).to_have_title(re.compile("Playwright"))

定位

定位器是PlayWright自动等待和重试功能的核心部分。定位符表示一种随时在页面上查找元素的方法,用于对元素等执行操作。.click.fill

from playwright.sync_api 
import expect
get_started = page.get_by_role("link", name="Get started")
expect(get_started).to_have_attribute("href", "/docs/installation")
get_started.click()

测试隔离

Playwright Pytest 插件基于测试夹具的概念,例如内置页面夹具,它被传递到您的测试中。由于浏览器上下文,页面在测试之间被隔离,这相当于一个全新的浏览器配置文件,其中每个测试都会获得一个新的环境,即使在单个浏览器中运行多个测试也是如此。

from playwright.sync_api import Page
def test_basic_test(page: Page):  
    # ...

使用测试挂钩

您可以使用各种夹具在测试之前或之后执行代码,并在它们之间共享对象。一个示波范围的夹具,例如具有自动使用功能的行为类似于之前每个/之后每个。具有自动使用功能的范围夹具的行为类似于在所有测试之前和之后运行的所有/之后。functionmodule

import pytestfrom playwright.sync_api import Page
@pytest.fixture(scope="function", autouse=True)
def before_each_after_each(page: Page):    
    print("beforeEach")    # Go to the starting url before each test.    
    page.goto("https://playwright.dev/")    
    yield    print("afterEach")
def test_main_navigation(page: Page):    # Assertions use the expect API.    
    expect(page).to_have_url("https://playwright.dev/")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值