Playwright for Python入门指南——Pytest插件参考

自动化测试 · 2023-06-29

Playwright提供了一个Pytest插件,用于编写端到端测试。要开始使用它,请参考入门指南

用法

要运行您的测试,请使用Pytest命令行界面(CLI)。

pytest --browser webkit --headed

如果您想自动添加CLI参数而无需指定它们,可以使用pytest.ini文件:

CLI参数

  • --headed:以有头模式运行测试(默认:无头模式)。
  • --browser:在不同的浏览器中运行测试,如chromiumfirefoxwebkit。可以多次指定(默认:chromium)。
  • --browser-channel:要使用的浏览器渠道
  • --slowmo:将Playwright操作放慢指定的毫秒数。这对于查看操作过程很有用(默认:0)。
  • --device:要模拟的设备
  • --output:测试生成的文件存放目录(默认:test-results)。
  • --tracing:是否为每个测试记录跟踪。选项为onoffretain-on-failure(默认:off)。
  • --video:是否为每个测试录制视频。选项为onoffretain-on-failure(默认:off)。
  • --screenshot:是否在每个测试后自动捕获屏幕截图。选项为onoffonly-on-failure(默认:off)。

Fixtures

该插件为Pytest配置了Playwright特定的fixtures。要使用这些fixtures,将fixture名称作为测试函数的参数。

函数范围:这些fixtures在测试函数中被请求时创建,并在测试结束时销毁。

会话范围:这些fixtures在测试函数中被请求时创建,并在所有测试结束时销毁。

  • playwrightPlaywright实例。
  • browser_type:当前浏览器的BrowserType实例。
  • browser:由Playwright启动的Browser实例。
  • browser_name:浏览器名称(字符串形式)。
  • browser_channel:浏览器渠道(字符串形式)。
  • is_chromiumis_webkitis_firefox:各自浏览器类型的布尔值。

自定义fixture选项:对于browsercontext fixtures,使用以下fixtures来定义自定义启动选项。

并行执行:同时运行多个测试

如果您的测试在具有大量CPU的机器上运行,可以使用pytest-xdist同时运行多个测试以加快整体执行时间:

pip install pytest-xdist

pytest --numprocesses auto

根据硬件和测试的性质,可以将numprocesses设置为从2到机器上的CPU数量的任何值。如果设置得太高,可能会注意到意外行为。

有关pytest选项的一般信息,请参阅Running Tests

示例

配置Mypy类型以进行自动完成

test_my_application.py

from playwright.sync_api import Page

def test_visit_admin_dashboard(page: Page):
    page.goto("/admin")

配置慢速模式

使用--slowmo参数运行测试以启用慢速模式。

将Playwright操作减慢100毫秒。

按浏览器跳过测试

test_my_application.py

import pytest

@pytest.mark.skip_browser("firefox")
def test_visit_example(page):
    page.goto("https://example.com")

在特定浏览器上运行

conftest.py

import pytest

@pytest.mark.only_browser("chromium")
def test_visit_example(page):
    page.goto("https://example.com")

使用自定义浏览器渠道,如Google Chrome或Microsoft Edge运行

pytest --browser-channel chrome

test_my_application.py

def test_example(page):
    page.goto("https://example.com")

配置base-url

使用base-url参数启动Pytest。使用pytest-base-url插件,可以从配置、命令行参数或fixture设置基本URL。

pytest --base-url http://localhost:8080

test_my_application.py

def test_visit_example(page):
    page.goto("/admin")

忽略HTTPS错误

conftest.py

import pytest

@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
    return {**browser_context_args, "ignore_https_errors": True}

使用自定义窗口大小

conftest.py

import pytest

@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
    return {**browser_context_args, "viewport": {"width": 1920, "height": 1080}}

设备仿真

conftest.py

import pytest

@pytest.fixture(scope="session")
def browser_context_args(browser_context_args, playwright):
    iphone_11 = playwright.devices['iPhone 11 Pro']
    return {**browser_context_args, **iphone_11}

或通过CLI使用--device="iPhone 11 Pro"

持久上下文

conftest.py

import pytest
from playwright.sync_api import BrowserType
from typing import Dict

@pytest.fixture(scope="session")
def context(
    browser_type: BrowserType, browser_type_launch_args: Dict, browser_context_args: Dict
):
    context = browser_type.launch_persistent_context(
        "./foobar",
        **{**browser_type_launch_args, **browser_context_args, "locale": "de-DE"}
    )
    yield context
    context.close()

使用该配置,测试中的所有页面都是从持久上下文创建的。

unittest.TestCase一起使用

以下示例演示与unittest.TestCase一起使用的情况。这有一个限制,即只能指定一个浏览器,并且在指定多个浏览器时不会生成多个浏览器矩阵。

import pytest
import unittest

from playwright.sync_api import Page


class MyTest(unittest.TestCase):
    @pytest.fixture(autouse=True)
    def setup(self, page: Page):
        self.page = page

    def test_foobar(self):
        self.page.goto("https://microsoft.com")
        self.page.locator("#foobar").click()
        assert self.page.evaluate("1 + 1") == 2

调试

使用pdb

在测试代码中使用breakpoint()语句暂停执行并获取pdb REPL。

def test_bing_is_working(page):
    page.goto("https://bing.com")
    breakpoint()

部署到CI

有关将测试部署到CI/CD的信息,请参阅CI提供商指南

Playwright
Theme Jasmine by Kent Liao