Skip to main content

Istanbul 覆盖率收集 for Playwright Python,类似 @canyonjs/playwright

Project description

canyonjs-playwright-python

@canyonjs/playwright 的 Python 实现,为 Playwright Python 提供 Istanbul 覆盖率收集能力。

安装

pip install canyonjs-playwright

或从源码安装(开发模式):

pip install -e .

用法

方式一:pytest_plugins(推荐,类似 baseTest.ts)

conftest.py 中添加一行即可自动启用,所有使用 page 的测试都会自动收集覆盖率:

# conftest.py
pytest_plugins = ["canyonjs_playwright.pytest_plugin"]

测试用例无需修改,直接使用 page

def test_example(page):
    page.goto("https://example.com")
    assert "Example" in page.title()

方式二:自定义输出目录

# conftest.py
import os
os.environ["CANYON_OUTPUT_DIR"] = ".canyon_output"
pytest_plugins = ["canyonjs_playwright.pytest_plugin"]

方式三:手动使用 fixture

# conftest.py
from canyonjs_playwright import create_coverage_context_fixture

coverage_context = create_coverage_context_fixture(output_dir=".canyon_output")
# test_example.py
def test_example(page, coverage_context):
    page.goto("https://example.com")

运行步骤

1. 创建虚拟环境(推荐)

python3 -m venv venv
source venv/bin/activate   # macOS/Linux
# Windows: venv\Scripts\activate

2. 安装依赖

python -m pip install -r requirements.txt

3. 安装 Chromium 浏览器

python -m playwright install chromium

4. 运行测试

python -m pytest

可选参数

参数 说明
--headed 有界面模式运行(显示浏览器窗口)
-v 详细输出
--tracing=retain-on-failure 失败时保留 trace 便于调试

项目结构

canyonjs-playwright-python/
├── src/
│   └── canyonjs_playwright/    # 包源码
│       ├── __init__.py
│       ├── fixtures.py
│       └── pytest_plugin.py
├── tests/
│   ├── conftest.py
│   ├── test_baidu.py
│   └── test_canyon.py
├── pyproject.toml
├── requirements.txt
├── pytest.ini
└── .github/workflows/
    └── playwright.yml

输出

覆盖率数据写入 coverage-{timestamp}.json,默认目录为 .canyon_output

注意:仅当被测页面有 Istanbul 注入的 window.__coverage__ 时才会生成覆盖率文件。

依赖

  • Python >= 3.8
  • playwright >= 1.40.0
  • pytest-playwright >= 0.4.0

CI

推送到 GitHub 后,GitHub Actions 会自动运行测试。失败时可在 Actions 页面下载 trace 文件,在 trace.playwright.dev 查看。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

canyonjs_playwright-0.1.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

canyonjs_playwright-0.1.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file canyonjs_playwright-0.1.1.tar.gz.

File metadata

  • Download URL: canyonjs_playwright-0.1.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for canyonjs_playwright-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ddef6be31b71e8b2d729f3944752fcdb59bddc3c182fea9d785d36995713d645
MD5 734d9fbb7c93873fa18a06f21b06a215
BLAKE2b-256 2e234aadc4adf799a29c18f3a610ce4e255d37143e41ac85defa0b5d6b99c5fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for canyonjs_playwright-0.1.1.tar.gz:

Publisher: publish.yml on canyon-project/canyonjs-playwright-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file canyonjs_playwright-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for canyonjs_playwright-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7f14dff6c74f62b5c0341bf4bf52258b2e7262ddf6b22f2458b3a42cbb28b1ef
MD5 2ca83d20c98ff024d407de05d9843723
BLAKE2b-256 a7a968de4e0d5abe59a9ab2953a43cd26fd5632ba5b91f88cc004bbddf4de45d

See more details on using hashes here.

Provenance

The following attestation bundles were made for canyonjs_playwright-0.1.1-py3-none-any.whl:

Publisher: publish.yml on canyon-project/canyonjs-playwright-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page