本文介绍了示例测试使用的 Playwright 配置,并介绍了如何根据自己的环境对其进行调整。
playwright.config.ts
配置文件位于 packages/e2e-tests/playwright.config.ts:
import { defineConfig, devices } from '@playwright/test';
import path from 'path';
import { getStorageStatePath } from 'power-platform-playwright-toolkit';
import dotenv from 'dotenv';
dotenv.config();
const storageStatePath = getStorageStatePath(process.env.MS_AUTH_EMAIL!);
const mdaStorageStatePath = path.join(
path.dirname(storageStatePath),
`state-mda-${process.env.MS_AUTH_EMAIL}.json`
);
export default defineConfig({
testDir: './tests',
fullyParallel: false, // Power Platform tests share a single org; run serially
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 1 : 0,
workers: 1, // Single worker prevents auth conflicts
reporter: [
['html', { outputFolder: 'playwright-report' }],
['junit', { outputFile: 'test-results/results.xml' }],
],
use: {
baseURL: process.env.CANVAS_APP_URL,
trace: 'on-first-retry',
screenshot: 'only-on-failure',
video: 'retain-on-failure',
},
projects: [
{
name: 'canvas',
use: {
...devices['Desktop Chrome'],
storageState: storageStatePath,
},
testMatch: '**/canvas/**/*.test.ts',
},
{
name: 'mda',
use: {
...devices['Desktop Chrome'],
storageState: mdaStorageStatePath,
},
testMatch: '**/mda/**/*.test.ts',
},
],
globalSetup: './globals/global-setup',
globalTeardown: './globals/global-teardown',
});
密钥配置选项
以下部分介绍了配置文件中最重要的设置以及何时更改它们。
fullyParallel 和 workers
Power Platform 测试共享单个 Dataverse 环境。 并行运行测试会创建数据冲突,例如删除同一记录的两个测试。 设置这两个选项以序列化执行:
fullyParallel: false,
workers: 1,
注释
如果有多个环境,则可以使用单独的.env文件或环境变量将每个项目增加workers并隔离到不同的环境。
retries
重试可以掩盖 CI 中的浮躁。 在 retries: 1 CI 中使用来处理暂时性网络问题。 在本地设置 retries: 0 以获取即时反馈:
retries: process.env.CI ? 1 : 0,
trace、screenshot、video
仅在未能节省磁盘空间时捕获诊断:
trace: 'on-first-retry',
screenshot: 'only-on-failure',
video: 'retain-on-failure',
在 CI 中,项目在测试运行后上传。 请参阅 CI/CD 集成。
projects
使用 Playwright 项目分隔画布和模型驱动测试套件。 每个项目都可以使用不同的存储状态:
projects: [
{
name: 'canvas',
use: { storageState: storageStatePath },
testMatch: '**/canvas/**/*.test.ts',
},
{
name: 'mda',
use: { storageState: mdaStorageStatePath },
testMatch: '**/mda/**/*.test.ts',
},
],
运行单个项目:
npx playwright test --project=canvas
npx playwright test --project=mda
globalSetup
全局安装脚本在所有测试之前运行一次。 它验证身份验证状态,如果存储状态已过期或缺失,则运行无外设身份验证:
// global-setup.ts
import { validateAndRefreshAuthState } from './utils/validate-auth-state';
export default async function globalSetup() {
await validateAndRefreshAuthState();
}
验证检查:
- 存储状态文件存在
- MSAL 访问令牌未过期(画布/Gen UX 测试)
- CRM 会话 Cookie 存在且有效(MDA 测试)
tsconfig.json
tsconfig.json文件控制测试包的 TypeScript 编译设置:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"outDir": "dist",
"rootDir": "."
},
"include": ["**/*.ts"],
"exclude": ["node_modules", "dist"]
}
.env 文件
设置 .env 本地运行的环境变量时的文件 packages/e2e-tests/.env 。 有关完整参考,请参阅 环境变量。
MS_AUTH_EMAIL=testuser@contoso.com
MS_AUTH_CREDENTIAL_TYPE=password
MS_USER_PASSWORD=<your-password>
CANVAS_APP_URL=https://apps.powerapps.com/play/<app-id>?tenantId=<tenant-id>
MODEL_DRIVEN_APP_URL=https://<org>.crm.dynamics.com/main.aspx?appid=<app-id>
CUSTOM_PAGE_NAME=AccountsCustomPage
package.json 脚本
以下 npm 脚本可用于 packages/e2e-tests/package.json 身份验证和测试执行:
| 脚本 | 命令 | 说明 |
|---|---|---|
auth:headful |
ts-node auth/auth-maker-portal.ts |
用于Power Apps的交互式身份验证(画布测试) |
auth:mda:headful |
ts-node auth/auth-mda.ts |
模型驱动应用的交互式身份验证 |
auth |
ts-node auth/auth-maker-portal.ts --headless |
无外设身份验证 (CI) |
auth:mda |
ts-node auth/auth-mda.ts --headless |
无外设模型驱动应用身份验证 (CI) |
test |
playwright test |
运行所有测试 |
test:ui |
playwright test --ui |
使用 Playwright UI 运行 |
test:debug |
playwright test --debug |
使用调试检查器运行 |