跳到主要內容

驗證

簡介

Playwright 在稱為瀏覽器 context 的隔離環境中執行測試。這種隔離模型提高了可重現性,並防止級聯的測試失敗。測試可以載入現有的已驗證狀態。這消除了在每個測試中進行驗證的需求,並加快了測試執行速度。

核心概念

無論您選擇哪種驗證策略,您都可能會將已驗證的瀏覽器狀態儲存在檔案系統上。

我們建議建立 playwright/.auth 目錄,並將其新增到您的 .gitignore。您的驗證常式將產生已驗證的瀏覽器狀態,並將其儲存到此 playwright/.auth 目錄中的檔案。稍後,測試將重複使用此狀態並以已驗證的狀態啟動。

mkdir -p playwright/.auth
echo $'\nplaywright/.auth' >> .gitignore

基本:在所有測試中共享帳戶

對於沒有伺服器端狀態的測試,這是建議的方法。在設定專案中驗證一次,儲存驗證狀態,然後重複使用它來引導每個已驗證的測試。

何時使用

  • 當您可以想像所有測試同時使用同一個帳戶運行,而不會互相影響時。

何時不使用

  • 您的測試會修改伺服器端狀態。例如,一個測試檢查設定頁面的渲染,而另一個測試正在更改設定,並且您平行運行測試。在這種情況下,測試必須使用不同的帳戶。
  • 您的驗證是瀏覽器特定的。

詳細資訊

建立 tests/auth.setup.ts,它將為所有其他測試準備已驗證的瀏覽器狀態。

tests/auth.setup.ts
import { test as setup, expect } from '@playwright/test';
import path from 'path';

const authFile = path.join(__dirname, '../playwright/.auth/user.json');

setup('authenticate', async ({ page }) => {
// Perform authentication steps. Replace these actions with your own.
await page.goto('https://github.com/login');
await page.getByLabel('Username or email address').fill('username');
await page.getByLabel('Password').fill('password');
await page.getByRole('button', { name: 'Sign in' }).click();
// Wait until the page receives the cookies.
//
// Sometimes login flow sets cookies in the process of several redirects.
// Wait for the final URL to ensure that the cookies are actually set.
await page.waitForURL('https://github.com/');
// Alternatively, you can wait until the page reaches a state where all cookies are set.
await expect(page.getByRole('button', { name: 'View profile and more' })).toBeVisible();

// End of authentication steps.

await page.context().storageState({ path: authFile });
});

在配置中建立新的 setup 專案,並將其宣告為所有測試專案的依賴項。此專案將始終在所有測試之前運行並進行驗證。所有測試專案都應使用已驗證的狀態作為 storageState

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

export default defineConfig({
projects: [
// Setup project
{ name: 'setup', testMatch: /.*\.setup\.ts/ },

{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
// Use prepared auth state.
storageState: 'playwright/.auth/user.json',
},
dependencies: ['setup'],
},

{
name: 'firefox',
use: {
...devices['Desktop Firefox'],
// Use prepared auth state.
storageState: 'playwright/.auth/user.json',
},
dependencies: ['setup'],
},
],
});

測試啟動時已驗證,因為我們在配置中指定了 storageState

tests/example.spec.ts
import { test } from '@playwright/test';

test('test', async ({ page }) => {
// page is authenticated
});

請注意,您需要在儲存的狀態過期時將其刪除。如果您不需要在測試運行之間保留狀態,請將瀏覽器狀態寫入 testProject.outputDir 下,該目錄會在每次測試運行前自動清除。

在 UI 模式中驗證

預設情況下,UI 模式不會運行 setup 專案,以提高測試速度。我們建議不時手動運行 auth.setup.ts 進行驗證,每當現有的驗證過期時。

首先在篩選器中啟用 setup 專案,然後點擊 auth.setup.ts 檔案旁邊的三角形按鈕,然後再次在篩選器中停用 setup 專案。

中等:每個平行 worker 一個帳戶

對於修改伺服器端狀態的測試,這是建議的方法。在 Playwright 中,worker 程序平行運行。在此方法中,每個平行 worker 驗證一次。由 worker 運行的所有測試都重複使用相同的驗證狀態。我們將需要多個測試帳戶,每個平行 worker 一個。

何時使用

  • 您的測試會修改共享的伺服器端狀態。例如,一個測試檢查設定頁面的渲染,而另一個測試正在更改設定。

何時不使用

  • 您的測試不會修改任何共享的伺服器端狀態。在這種情況下,所有測試都可以使用單一共享帳戶。

詳細資訊

我們將針對每個worker 程序驗證一次,每個程序都有一個獨特的帳戶。

建立 playwright/fixtures.ts 檔案,它將覆寫 storageState fixture,以便每個 worker 驗證一次。使用 testInfo.parallelIndex 來區分 worker。

playwright/fixtures.ts
import { test as baseTest, expect } from '@playwright/test';
import fs from 'fs';
import path from 'path';

export * from '@playwright/test';
export const test = baseTest.extend<{}, { workerStorageState: string }>({
// Use the same storage state for all tests in this worker.
storageState: ({ workerStorageState }, use) => use(workerStorageState),

// Authenticate once per worker with a worker-scoped fixture.
workerStorageState: [async ({ browser }, use) => {
// Use parallelIndex as a unique identifier for each worker.
const id = test.info().parallelIndex;
const fileName = path.resolve(test.info().project.outputDir, `.auth/${id}.json`);

if (fs.existsSync(fileName)) {
// Reuse existing authentication state if any.
await use(fileName);
return;
}

// Important: make sure we authenticate in a clean environment by unsetting storage state.
const page = await browser.newPage({ storageState: undefined });

// Acquire a unique account, for example create a new one.
// Alternatively, you can have a list of precreated accounts for testing.
// Make sure that accounts are unique, so that multiple team members
// can run tests at the same time without interference.
const account = await acquireAccount(id);

// Perform authentication steps. Replace these actions with your own.
await page.goto('https://github.com/login');
await page.getByLabel('Username or email address').fill(account.username);
await page.getByLabel('Password').fill(account.password);
await page.getByRole('button', { name: 'Sign in' }).click();
// Wait until the page receives the cookies.
//
// Sometimes login flow sets cookies in the process of several redirects.
// Wait for the final URL to ensure that the cookies are actually set.
await page.waitForURL('https://github.com/');
// Alternatively, you can wait until the page reaches a state where all cookies are set.
await expect(page.getByRole('button', { name: 'View profile and more' })).toBeVisible();

// End of authentication steps.

await page.context().storageState({ path: fileName });
await page.close();
await use(fileName);
}, { scope: 'worker' }],
});

現在,每個測試檔案都應該從我們的 fixtures 檔案匯入 test,而不是從 @playwright/test 匯入。配置中不需要進行任何變更。

tests/example.spec.ts
// Important: import our fixtures.
import { test, expect } from '../playwright/fixtures';

test('test', async ({ page }) => {
// page is authenticated
});

進階情境

使用 API 請求進行驗證

何時使用

  • 您的 Web 應用程式支援透過 API 進行驗證,這比與應用程式 UI 互動更輕鬆/更快速。

詳細資訊

我們將使用 APIRequestContext 發送 API 請求,然後照常儲存已驗證的狀態。

設定專案

tests/auth.setup.ts
import { test as setup } from '@playwright/test';

const authFile = 'playwright/.auth/user.json';

setup('authenticate', async ({ request }) => {
// Send authentication request. Replace with your own.
await request.post('https://github.com/login', {
form: {
'user': 'user',
'password': 'password'
}
});
await request.storageState({ path: authFile });
});

或者,在worker fixture

playwright/fixtures.ts
import { test as baseTest, request } from '@playwright/test';
import fs from 'fs';
import path from 'path';

export * from '@playwright/test';
export const test = baseTest.extend<{}, { workerStorageState: string }>({
// Use the same storage state for all tests in this worker.
storageState: ({ workerStorageState }, use) => use(workerStorageState),

// Authenticate once per worker with a worker-scoped fixture.
workerStorageState: [async ({}, use) => {
// Use parallelIndex as a unique identifier for each worker.
const id = test.info().parallelIndex;
const fileName = path.resolve(test.info().project.outputDir, `.auth/${id}.json`);

if (fs.existsSync(fileName)) {
// Reuse existing authentication state if any.
await use(fileName);
return;
}

// Important: make sure we authenticate in a clean environment by unsetting storage state.
const context = await request.newContext({ storageState: undefined });

// Acquire a unique account, for example create a new one.
// Alternatively, you can have a list of precreated accounts for testing.
// Make sure that accounts are unique, so that multiple team members
// can run tests at the same time without interference.
const account = await acquireAccount(id);

// Send authentication request. Replace with your own.
await context.post('https://github.com/login', {
form: {
'user': 'user',
'password': 'password'
}
});

await context.storageState({ path: fileName });
await context.dispose();
await use(fileName);
}, { scope: 'worker' }],
});

多個已登入的角色

何時使用

  • 您的端對端測試中有多個角色,但您可以跨所有測試重複使用帳戶。

詳細資訊

我們將在設定專案中多次驗證。

tests/auth.setup.ts
import { test as setup, expect } from '@playwright/test';

const adminFile = 'playwright/.auth/admin.json';

setup('authenticate as admin', async ({ page }) => {
// Perform authentication steps. Replace these actions with your own.
await page.goto('https://github.com/login');
await page.getByLabel('Username or email address').fill('admin');
await page.getByLabel('Password').fill('password');
await page.getByRole('button', { name: 'Sign in' }).click();
// Wait until the page receives the cookies.
//
// Sometimes login flow sets cookies in the process of several redirects.
// Wait for the final URL to ensure that the cookies are actually set.
await page.waitForURL('https://github.com/');
// Alternatively, you can wait until the page reaches a state where all cookies are set.
await expect(page.getByRole('button', { name: 'View profile and more' })).toBeVisible();

// End of authentication steps.

await page.context().storageState({ path: adminFile });
});

const userFile = 'playwright/.auth/user.json';

setup('authenticate as user', async ({ page }) => {
// Perform authentication steps. Replace these actions with your own.
await page.goto('https://github.com/login');
await page.getByLabel('Username or email address').fill('user');
await page.getByLabel('Password').fill('password');
await page.getByRole('button', { name: 'Sign in' }).click();
// Wait until the page receives the cookies.
//
// Sometimes login flow sets cookies in the process of several redirects.
// Wait for the final URL to ensure that the cookies are actually set.
await page.waitForURL('https://github.com/');
// Alternatively, you can wait until the page reaches a state where all cookies are set.
await expect(page.getByRole('button', { name: 'View profile and more' })).toBeVisible();

// End of authentication steps.

await page.context().storageState({ path: userFile });
});

之後,為每個測試檔案或測試群組指定 storageState而不是在配置中設定它。

tests/example.spec.ts
import { test } from '@playwright/test';

test.use({ storageState: 'playwright/.auth/admin.json' });

test('admin test', async ({ page }) => {
// page is authenticated as admin
});

test.describe(() => {
test.use({ storageState: 'playwright/.auth/user.json' });

test('user test', async ({ page }) => {
// page is authenticated as a user
});
});

另請參閱關於在 UI 模式中驗證

一起測試多個角色

何時使用

  • 您需要測試多個已驗證的角色如何在單一測試中一起互動。

詳細資訊

在同一個測試中使用多個具有不同儲存狀態的 BrowserContextPage

tests/example.spec.ts
import { test } from '@playwright/test';

test('admin and user', async ({ browser }) => {
// adminContext and all pages inside, including adminPage, are signed in as "admin".
const adminContext = await browser.newContext({ storageState: 'playwright/.auth/admin.json' });
const adminPage = await adminContext.newPage();

// userContext and all pages inside, including userPage, are signed in as "user".
const userContext = await browser.newContext({ storageState: 'playwright/.auth/user.json' });
const userPage = await userContext.newPage();

// ... interact with both adminPage and userPage ...

await adminContext.close();
await userContext.close();
});

使用 POM fixtures 測試多個角色

何時使用

  • 您需要測試多個已驗證的角色如何在單一測試中一起互動。

詳細資訊

您可以引入 fixtures,它們將提供以每個角色驗證的頁面。

以下範例建立 fixtures 給兩個 Page Object Models - 管理員 POM 和使用者 POM。它假設 adminStorageState.jsonuserStorageState.json 檔案是在全域設定中建立的。

playwright/fixtures.ts
import { test as base, type Page, type Locator } from '@playwright/test';

// Page Object Model for the "admin" page.
// Here you can add locators and helper methods specific to the admin page.
class AdminPage {
// Page signed in as "admin".
page: Page;

// Example locator pointing to "Welcome, Admin" greeting.
greeting: Locator;

constructor(page: Page) {
this.page = page;
this.greeting = page.locator('#greeting');
}
}

// Page Object Model for the "user" page.
// Here you can add locators and helper methods specific to the user page.
class UserPage {
// Page signed in as "user".
page: Page;

// Example locator pointing to "Welcome, User" greeting.
greeting: Locator;

constructor(page: Page) {
this.page = page;
this.greeting = page.locator('#greeting');
}
}

// Declare the types of your fixtures.
type MyFixtures = {
adminPage: AdminPage;
userPage: UserPage;
};

export * from '@playwright/test';
export const test = base.extend<MyFixtures>({
adminPage: async ({ browser }, use) => {
const context = await browser.newContext({ storageState: 'playwright/.auth/admin.json' });
const adminPage = new AdminPage(await context.newPage());
await use(adminPage);
await context.close();
},
userPage: async ({ browser }, use) => {
const context = await browser.newContext({ storageState: 'playwright/.auth/user.json' });
const userPage = new UserPage(await context.newPage());
await use(userPage);
await context.close();
},
});

tests/example.spec.ts
// Import test with our new fixtures.
import { test, expect } from '../playwright/fixtures';

// Use adminPage and userPage fixtures in the test.
test('admin and user', async ({ adminPage, userPage }) => {
// ... interact with both adminPage and userPage ...
await expect(adminPage.greeting).toHaveText('Welcome, Admin');
await expect(userPage.greeting).toHaveText('Welcome, User');
});

Session storage

重複使用已驗證的狀態涵蓋基於 cookieslocal storage 的驗證。很少情況下,session storage 用於儲存與已登入狀態相關聯的資訊。Session storage 特定於特定網域,並且不會跨頁面載入持續存在。Playwright 沒有提供 API 來持久化 session storage,但可以使用以下程式碼片段來儲存/載入 session storage。

// Get session storage and store as env variable
const sessionStorage = await page.evaluate(() => JSON.stringify(sessionStorage));
fs.writeFileSync('playwright/.auth/session.json', sessionStorage, 'utf-8');

// Set session storage in a new context
const sessionStorage = JSON.parse(fs.readFileSync('playwright/.auth/session.json', 'utf-8'));
await context.addInitScript(storage => {
if (window.location.hostname === 'example.com') {
for (const [key, value] of Object.entries(storage))
window.sessionStorage.setItem(key, value);
}
}, sessionStorage);

避免在某些測試中進行驗證

您可以在測試檔案中重設儲存狀態,以避免為整個專案設定的驗證。

not-signed-in.spec.ts
import { test } from '@playwright/test';

// Reset storage state for this file to avoid being authenticated
test.use({ storageState: { cookies: [], origins: [] } });

test('not signed in test', async ({ page }) => {
// ...
});