Skip to main content

專案

簡介

專案是使用相同組態設定執行的測試邏輯群組。我們使用專案可以在不同的瀏覽器和裝置上執行測試。專案在 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'
},
},
],
});

執行專案

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 瀏覽器上執行您的測試。若要在其他/多個瀏覽器上執行,請從測試側邊欄點擊播放按鈕的下拉式選單並選擇其他設定檔,或點擊 Select Default Profile 來修改預設設定檔並選擇您希望執行測試的瀏覽器。

selecting browsers

選擇特定設定檔、多個設定檔或所有設定檔來執行測試。

choosing default profiles

為多個環境設定專案

透過設定專案,我們還可以執行具有不同逾時或重試次數的測試群組,或針對不同環境執行測試群組。例如,我們可以針對測試環境執行重試 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,
},
],
});

將測試分割為專案

我們可以將測試分割為專案並使用篩選器來執行測試的子集合。例如,我們可以建立一個專案,使用篩選器來執行符合特定檔案名稱的所有測試。然後我們可以有另一組忽略特定測試檔案的測試。

以下是定義通用逾時和兩個專案的範例。「Smoke」專案執行少量測試子集合且不重試,而「Default」專案執行所有其他測試並重試。

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

export default defineConfig({
timeout: 60000, // Timeout is shared between all tests.
projects: [
{
name: 'Smoke',
testMatch: /.*smoke.spec.ts/,
retries: 0,
},
{
name: 'Default',
testIgnore: /.*smoke.spec.ts/,
retries: 2,
},
],
});

相依性

相依性是在另一個專案的測試執行之前需要執行的專案清單。它們對於設定全域設定動作非常有用,這樣一個專案就依賴於此專案先執行。使用專案相依性時,測試報告器會顯示設定測試,而追蹤檢視器會記錄設定的追蹤。您可以使用檢查器來檢查設定測試追蹤的 DOM 快照,也可以在設定中使用佈置

在此範例中,chromium、firefox 和 webkit 專案依賴於 setup 專案。

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

export default defineConfig({
projects: [
{
name: 'setup',
testMatch: '**/*.setup.ts',
},
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
dependencies: ['setup'],
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
dependencies: ['setup'],
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
dependencies: ['setup'],
},
],
});

執行序列

當處理具有相依性的測試時,相依性會始終先執行,一旦此專案的所有測試都通過,其他專案將平行執行。

執行順序:

  1. 「setup」專案中的測試執行。一旦此專案的所有測試都通過,相依專案的測試將開始執行。

  2. 「chromium」、「webkit」和「firefox」專案中的測試一起執行。預設情況下,這些專案將平行執行,受最大工作程序限制約束。

chromium, webkit and firefox projects depend on setup project

如果有多個相依性,那麼這些專案相依性將首先執行且平行執行。如果相依性的測試失敗,則依賴此專案的測試將不會執行。

執行順序:

  1. 「Browser Login」和「DataBase」專案中的測試平行執行:
    • 「Browser Login」通過
    • ❌ 「DataBase」失敗!
  2. 「e2e tests」專案不會執行!
Browser login project is blue, database is red and e2e tests relies on both

拆除

您也可以透過在設定專案中新增 testProject.teardown 屬性來拆除您的設定。拆除將在所有相依專案執行後執行。請參閱拆除指南以取得更多資訊。

global setup and teardown

測試篩選

所有測試篩選選項,如 --grep/--grep-invert--shard、在命令列中直接依位置篩選,或使用 test.only(),都會直接選擇要執行的主要測試。如果這些測試屬於具有相依性的專案,那麼來自這些相依性的所有測試也會執行。

您可以傳遞 --no-deps 命令列選項來忽略所有相依性和拆除。只有您直接選擇的專案會執行。

自訂專案參數

專案也可以用於使用您的自訂組態設定來參數化測試 - 請查看這個單獨的指南