本文記錄了範例測試中使用的 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,
Note
如果你有多個環境,可以用不同的workers檔案或環境層級變數來擴充.env並隔離每個專案到不同的環境。
retries
重試可以掩蓋 CI 的不穩定。 在 CI 中用於 retries: 1 處理暫時性網路問題。 設定 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 存取權杖沒有過期(Canvas/Gen UX 測試)
- CRM 會話 Cookies 存在且有效(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 認證與測試執行:
| Script | 命令 | Description |
|---|---|---|
auth:headful |
ts-node auth/auth-maker-portal.ts |
Interactive auth for Power Apps (canvas tests) |
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 |
搭配除錯檢查器執行 |