Playwright 运行项目。

简介: Playwright 运行项目。

介绍
项目是使用相同配置运行的测试的逻辑组。我们使用项目,以便可以在不同的浏览器和设备上运行测试。项目在 playwright.config.ts 文件中配置,配置完成后,你可以在所有项目或仅在特定项目上运行测试。你还可以使用项目在不同的配置中运行相同的测试。例如,你可以在登录和注销状态下运行相同的测试。

通过设置项目,你还可以运行一组具有不同超时或重试的测试,或者针对不同环境(例如登台和生产、按包/功能拆分测试等)运行一组测试。

为多个浏览器配置项目
通过使用项目,你可以在多种浏览器(例如 chromium、webkit 和 firefox)以及品牌浏览器(例如 Google Chrome 和 Microsoft Edge)中运行测试。Playwright 还可以在模拟平板电脑和移动设备上运行。请参阅 设备参数注册表,了解所选台式机、平板电脑和移动设备的完整列表。

import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},

{
  name: 'firefox',
  use: { ...devices['Desktop Firefox'] },
},

{
  name: 'webkit',
  use: { ...devices['Desktop Safari'] },
},

/* Test against mobile viewports. */
{
  name: 'Mobile Chrome',
  use: { ...devices['Pixel 5'] },
},
{
  name: 'Mobile Safari',
  use: { ...devices['iPhone 12'] },
},

/* Test against branded browsers. */
{
  name: 'Microsoft Edge',
  use: {
    ...devices['Desktop Edge'],
    channel: 'msedge'
  },
},
{
  name: 'Google Chrome',
  use: {
    ...devices['Desktop Chrome'],
    channel: 'chrome'
  },
},
AI 代码解读

],
});
运行项目
Playwright 将默认运行所有项目。
npx playwright test

Running 7 tests using 5 workers

✓ [chromium] › example.spec.ts:3:1 › basic test (2s)
✓ [firefox] › example.spec.ts:3:1 › basic test (2s)
✓ [webkit] › example.spec.ts:3:1 › basic test (2s)
✓ [Mobile Chrome] › example.spec.ts:3:1 › basic test (2s)
✓ [Mobile Safari] › example.spec.ts:3:1 › basic test (2s)
✓ [Microsoft Edge] › example.spec.ts:3:1 › basic test (2s)
✓ [Google Chrome] › example.spec.ts:3:1 › basic test (2s)
使用 --project 命令行选项运行单个项目。

npx playwright test --project=firefox

Running 1 test using 1 worker

✓ [firefox] › example.spec.ts:3:1 › basic test (2s)

VS Code 测试运行程序在 Chrome 的默认浏览器上运行你的测试。要在其他/多个浏览器上运行,请单击测试侧栏中的播放按钮下拉菜单,然后选择另一个配置文件,或通过单击“选择默认配置文件”修改默认配置文件,然后选择你希望运行测试的浏览器。
为多个环境配置项目
通过设置项目,我们还可以运行一组具有不同超时或重试的测试,或者针对不同环境运行一组测试。例如,我们可以在暂存环境中重试 2 次,也可以在生产环境中重试 0 次。

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
timeout: 60000, // Timeout is shared between all tests.
projects: [
{
name: 'staging',
use: {
baseURL: 'staging.example.com',
},
retries: 2,
},
{
name: 'production',
use: {
baseURL: 'production.example.com',
},
retries: 0,
},
],
});

目录
打赏
0
3
3
0
72
分享
相关文章
|
8月前
Playwright 测试重试
Playwright 测试重试
211 2
Playwright 测试并行性
Playwright 测试并行性
299 0
|
8月前
playwright Test 录制视频
playwright Test 录制视频
194 3
|
8月前
参数化测试
参数化测试
154 0
C# 单元测试框架 NUnit 一分钟浅谈
【10月更文挑战第17天】单元测试是软件开发中重要的质量保证手段,NUnit 是一个广泛使用的 .NET 单元测试框架。本文从基础到进阶介绍了 NUnit 的使用方法,包括安装、基本用法、参数化测试、异步测试等,并探讨了常见问题和易错点,旨在帮助开发者有效利用单元测试提高代码质量和开发效率。
410 64
在Ubuntu 18.04上安装和配置GitLab的方法
在Ubuntu 18.04上安装和配置GitLab的方法
216 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问