Playwright 测试报告中显示的标签和注释。

简介: Playwright 测试报告中显示的标签和注释。

Playwright 支持测试报告中显示的标签和注释。

¥Playwright supports tags and annotations that are displayed in the test report.

你可以随时添加自己的标签和注释,但 Playwright 附带了一些内置标签和注释:

test.skip() 将测试标记为不相关。Playwright 没有进行这样的测试。当测试在某些配置中不适用时使用此注释。

test.fail() 将测试标记为失败。Playwright 将运行此测试并确保它确实失败。如果测试没有失败,Playwright 就会抗诉。

test.fixme() 将测试标记为失败。与 fail 注释相反,Playwright 不会运行此测试。当运行测试缓慢或崩溃时使用 fixme。

test.slow() 将测试标记为慢速并将测试超时增加三倍。

注释可以添加到单个测试或一组测试中。

内置注释可以是有条件的,在这种情况下,它们在条件为真时应用,并且可能取决于测试装置。同一个测试可能有多个注释,可能处于不同的配置中。

集中测试
你可以集中一些测试。当存在集中测试时,仅运行这些测试。
test.only('focus this test', async ({ page }) => {
// Run only focused tests in the entire project.
});

跳过测试
将测试标记为已跳过。

test.skip('skip this test', async ({ page }) => {
// This test is not run
});

有条件地跳过测试
你可以根据情况跳过某些测试。

test('skip this test', async ({ page, browserName }) => {
test.skip(browserName === 'firefox', 'Still working on it');
});
群组测试
你可以对测试进行分组,为它们指定一个逻辑名称,或者在组的钩子之前/之后确定范围。

import { test, expect } from '@playwright/test';

test.describe('two tests', () => {
test('one', async ({ page }) => {
// ...
});

test('two', async ({ page }) => {
// ...
});
});

标签测试
有时你想将测试标记为 @fast 或 @slow,然后在测试报告中按标记进行过滤。或者你可能只想运行具有特定标签的测试。

要标记测试,请在声明测试时提供额外的详细信息对象,或将 @-token 添加到测试标题。请注意,标签必须以 @ 符号开头。
import { test, expect } from '@playwright/test';

test('test login page', {
tag: '@fast',
}, async ({ page }) => {
// ...
});

test('test full report @slow', async ({ page }) => {
// ...
});

你还可以标记组中的所有测试或提供多个标签:

import { test, expect } from '@playwright/test';

test.describe('group', {
tag: '@report',
}, () => {
test('test report header', async ({ page }) => {
// ...
});

test('test full report', {
tag: ['@slow', '@vrt'],
}, async ({ page }) => {
// ...
});
});
你现在可以使用 --grep 命令行选项运行具有特定标记的测试。

Bash
PowerShell
Batch
npx playwright test --grep @fast

或者,如果你想要相反的结果,你可以跳过带有特定标签的测试:

Bash
PowerShell
Batch
npx playwright test --grep-invert @fast

要运行包含任一标签(逻辑 OR 运算符)的测试:

Bash
PowerShell
Batch
npx playwright test --grep "@fast|@slow"

或者使用正则表达式先行运行包含两个标签(逻辑 AND 运算符)的测试:

npx playwright test --grep "(?=.@fast)(?=.@slow)"
你还可以通过 testConfig.grep 和 testProject.grep 过滤配置文件中的测试。

注释测试
如果你想使用比标签更实质性的内容来注释你的测试,则可以在声明测试时执行此操作。注释具有 type 和 description,可提供更多上下文,并在报告器 API 中可用。Playwright 的内置 HTML 报告器显示所有注释,但 type 以 _ 符号开头的注释除外。

例如,要使用问题 url 注释测试:

import { test, expect } from '@playwright/test';

test('test login page', {
annotation: {
type: 'issue',
description: 'https://github.com/microsoft/playwright/issues/23180',
},
}, async ({ page }) => {
// ...
});

你还可以注释一组中的所有测试或提供多个注释:
import { test, expect } from '@playwright/test';

test.describe('report tests', {
annotation: { type: 'category', description: 'report' },
}, () => {
test('test report header', async ({ page }) => {
// ...
});

test('test full report', {
annotation: [
{ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/23180' },
{ type: 'performance', description: 'very slow test!' },
],
}, async ({ page }) => {
// ...
});
});

有条件地跳过一组测试
例如,你可以通过传递回调仅在 Chromium 中运行一组测试。
example.spec.ts

test.describe('chromium only', () => {
test.skip(({ browserName }) => browserName !== 'chromium', 'Chromium only!');

test.beforeAll(async () => {
// This hook is only run in Chromium.
});

test('test 1', async ({ page }) => {
// This test is only run in Chromium.
});

test('test 2', async ({ page }) => {
// This test is only run in Chromium.
});
});

在 beforeEach 钩子中使用 fixme
为了避免运行 beforeEach 钩子,你可以在钩子本身中添加注释。
example.spec.ts

test.beforeEach(async ({ page, isMobile }) => {
test.fixme(isMobile, 'Settings page does not work in mobile yet');

await page.goto('http://localhost:3000/settings');
});

test('user profile', async ({ page }) => {
await page.getByText('My Profile').click();
// ...
});

运行时注释
当测试已经运行时,你可以向 test.info().annotations 添加注释。

example.spec.ts

test('example test', async ({ page, browser }) => {
test.info().annotations.push({
type: 'browser version',
description: browser.version(),
});

// ...
});

目录
相关文章
|
JSON 测试技术 数据格式
Playwright 测试报告器
Playwright 测试报告器
769 4
|
资源调度
yarn配置镜像源
node学习笔记
3731 0
yarn配置镜像源
|
测试技术 虚拟化 iOS开发
iOS自动化测试方案(二):Xcode开发者工具构建WDA应用到iphone
这篇文章是iOS自动化测试方案的第二部分,详细介绍了在Xcode开发者工具中构建WebDriverAgent(WDA)应用到iPhone的全过程,包括环境准备、解决构建过程中可能遇到的错误,以及最终成功安装WDA到设备的方法。
2080 0
iOS自动化测试方案(二):Xcode开发者工具构建WDA应用到iphone
|
关系型数据库 分布式数据库 数据库
PostgreSQL+Citus分布式数据库
PostgreSQL+Citus分布式数据库
737 15
|
数据安全/隐私保护
|
负载均衡 Java 应用服务中间件
|
6月前
|
测试技术
Playwright测试策略:智能断言与软断言的应用
断言是自动化测试的基石,但传统断言常因一错即停、信息模糊而效率低下。本文将深入Playwright的智能断言与软断言策略,教你如何用自动等待简化代码,用错误收集替代立即中断。掌握这些技巧,你的测试将更健壮、更易维护。
Playwright 测试重试
Playwright 测试重试
450 2
|
安全 jenkins 测试技术
Jenkins配置测试报告后无法正常显示或显示空白 的解决方法(问题集锦)
Jenkins配置测试报告后无法正常显示或显示空白 的解决方法(问题集锦)
1166 0