playwright+allure+pytest失败添加截图和视频到报告

学自动化测试的时候,使用百度的方法不知道为啥都不生效。后面在conftest.py里面整了个蠢方法。

使用conftest.py的pytest_runtest_makereport方法,在用例失败的时候截图和把录屏放到报告里面。

关掉页面的时候,录屏文件可能还会被占用,导致加上去一个空文件,所以要等没被占用的时候再添加

import os
import sys
import time
import allure
import pytest
from allure_commons.types import AttachmentType

# 目录位置可以自己定义,我用的是父目录的父目录

dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def is_file_locked(filepath) -> bool:
    """
    检测文件是否被占用
    """
    locked = False
    try:
        with open(filepath, 'rb') as fp:
            pass
    except:
        locked = True
    finally:
        if fp and not fp.closed:
            os.close(fp.fileno())
    return locked


def wait_for_file_unlock(filepath, timeout=5):
    for i in range(timeout * 2):
        if is_file_locked(filepath):
            break
        else:
            time.sleep(0.5)


@pytest.hookimpl(t
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想使用 Playwright 和 Python 进行测试并获取信息,可以使用 Pytest 进行测试框架。以下是一些步骤: 1. 安装 PytestPlaywright-python ``` pip install pytest pip install playwright pip install pytest-playwright ``` 2. 编写测试用例 可以使用 Playwright 的 API 进行浏览器操作,例如打开一个网页,填写表单,点击按钮等等。在测试用例中,您可以使用 assert 语句来检查页面上的信息是否正确。 ```python import pytest from playwright.sync_api import Playwright, Page @pytest.fixture(scope="module") def playwright() -> Playwright: with Playwright() as playwright: yield playwright @pytest.fixture(scope="module") def browser(playwright): browser = playwright.chromium.launch() yield browser browser.close() @pytest.fixture(scope="module") def page(browser): page = browser.new_page() yield page page.close() def test_fill_form(page: Page): page.goto("https://www.example.com") page.fill("#username", "testuser") page.fill("#password", "testpass") page.click("#submit") assert page.url == "https://www.example.com/dashboard" assert page.title() == "Dashboard" assert page.query_selector("#welcome-message").inner_text() == "Welcome, testuser!" ``` 3. 运行测试用例并存储信息 运行 pytest 命令来运行测试用例,并使用 pytest报告功能来存储测试结果。您可以使用 `-r` 参数来指定报告格式,例如 `-r junit` 将生成 JUnit XML 报告。您还可以使用 `-o` 参数来指定报告文件的路径和名称。 ``` pytest -r junit -o results.xml ``` 除了存储测试结果,您还可以使用 Python 的文件操作功能来将测试结果写入 CSV 或 Excel 文件。 希望这可以帮助您开始使用 Playwright 和 Python 进行测试和信息获取!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值