Playwright Python

pip install tracera-playwright

需要 Python ≥ 3.11 和 pytest ≥ 8(见 自动化测试适配器)。tracera-playwright on PyPI。

不要在同一次 pytest 运行中同时加载 tracera-pytest 与 tracera-playwright。

page fixture 由 pytest-playwright 提供。在 Playwright 配置中启用 screenshot、video 和 trace — 产物会加入 Tracera 结果。

环境变量、运行模式与重试见 自动化测试适配器。

重试时 Env 键:PLAYWRIGHT_RETRY。

绑定 testCaseId 与 autotestName

照常使用 pytest 和 Playwright 标准 API,并从 tracera_playwright 导入 tracera 和 id。

在声明时(推荐 — 即使测试提前跳过也会绑定):

from tracera_playwright import id as tracera_id

@tracera_id(42, "e2e/login")
def test_login(page):
    page.goto("https://example.com/login")

在测试中(若 @tracera.id 已绑定用例则可省略):

from tracera_playwright import tracera

def test_login(page):
    tracera.test_case(42, "e2e/login")
    page.goto("https://example.com/login")

Skip

使用 pytest 标准 skip(pytest.skip、@pytest.mark.skip 等)。Skip reason 作为 comment 添加到结果。

import os
import pytest
from tracera_playwright import id as tracera_id

@tracera_id(42, "e2e/login")
def test_login(page):
    if not os.environ.get("READY"):
        pytest.skip("env not ready")
    page.goto("https://example.com/login")

步骤

使用 tracera.step(title, body)、@tracera.step("…")、with tracera.step("…"): 或 async with tracera.step("…"):(async def 也可用 @tracera.step / await tracera.step(…))。在装饰器 @tracera.step("add {item}") 中,{item} 由该函数的 item 参数代入。function-scoped fixture 内的步骤进入结果的 准备 / 清理;测试内的步骤进入结果的 测试 区。套件级钩子不上报。

import pytest
from tracera_playwright import tracera

@pytest.fixture(autouse=True)
def open_shop(page):
    with tracera.step("open shop"):
        page.goto("https://example.com/shop")
    yield
    with tracera.step("cleanup"):
        page.goto("https://example.com/cart/clear")

def test_checkout(page):
    tracera.test_case(10, "e2e/checkout")

    @tracera.step("add {item}")
    def add_to_cart(item: str):
        page.get_by_role("button", name="Add").click()

    add_to_cart("sku-1")  # → 步骤 "add sku-1"

注释、错误与附件

注释、错误与附件见 自动化测试适配器。Playwright 会自行附上截图、视频和 Playwright trace — 在 Tracera 中,图片在灯箱中打开,视频可内联播放,trace 在 Trace Viewer 中打开。

from tracera_playwright import tracera

def test_login(page):
    tracera.test_case(42, "e2e/login")
    tracera.comment("result-level note")
    tracera.error("result-level error text")

    with tracera.step("open login"):
        page.goto("https://example.com/login")
        tracera.comment("landed on login")
        tracera.attachment(
            file_name="suite-log.txt",
            mime_type="text/plain",
            data=b"finished login flow",
        )

参数化测试

@tracera.id 装饰器的第二个参数是 autotest 名称。其中可用 {参数名} / {param_id} 占位符;{参数名} 须与测试函数的参数同名;每一行 @pytest.mark.parametrize 用该行的值填入(若 pytest 提供参数 id,可用 {param_id})。下面的示例会得到绑定到用例 42 的两个结果,autotest 名称分别为 login as ok 和 login as sso:

import pytest
from tracera_playwright import id as tracera_id

@tracera_id(42, "login as {login}")
@pytest.mark.parametrize("login", ["ok", "sso"])
def test_login(page, login):
    page.goto("https://example.com/login")

当用例 id 也变化时,也可在测试中绑定:

import pytest
from tracera_playwright import tracera

@pytest.mark.parametrize(
    "case_id,name,path",
    [
        (42, "e2e/login-ok", "/login"),
        (43, "e2e/login-sso", "/login/sso"),
    ],
)
def test_login_row(page, case_id, name, path):
    tracera.test_case(case_id, name)

    with tracera.step("open"):
        page.goto(f"https://example.com{path}")