Skip to main content

測試執行器

簡介

雖然 Playwright for .NET 並不依賴於特定的測試執行器或測試框架,但根據我們的經驗,最簡單的入門方法是使用我們為 MSTestNUnit 提供的基礎類別。這些類別支援在多個瀏覽器引擎上執行測試,調整啟動/上下文選項,並在每個測試中獲取 Page/BrowserContext 物件。

Playwright 和 Browser 實例將在測試之間重複使用,以獲得更好的效能。我們建議在新的 BrowserContext 中執行每個測試案例,這樣瀏覽器狀態將在測試之間隔離。

MSTest

Playwright 提供基礎類別來使用 MSTest 撰寫測試,透過 Microsoft.Playwright.MSTest 套件。

請查看安裝指南以開始使用。

執行 MSTest 測試並行

預設情況下,MSTest 會平行處理所有類別,同時在每個類別內部依序執行測試(ExecutionScope.ClassLevel)。它會建立與主機系統上的核心數量相同的程序。您可以使用以下 CLI 參數或使用 .runsettings 文件來調整此行為,請參見下文。在方法層級(ExecutionScope.MethodLevel)平行處理測試是不支援的。

dotnet test --settings:.runsettings -- MSTest.Parallelize.Workers=4

自訂 BrowserContext 選項

要自訂上下文選項,您可以覆寫從 Microsoft.Playwright.MSTest.PageTestMicrosoft.Playwright.MSTest.ContextTest 派生的測試類別的 ContextOptions 方法。請參閱以下範例:

using System.Threading.Tasks;
using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PlaywrightTests;

[TestClass]
public class ExampleTest : PageTest
{
[TestMethod]
public async Task TestWithCustomContextOptions()
{
// The following Page (and BrowserContext) instance has the custom colorScheme, viewport and baseURL set:
await Page.GotoAsync("/login");
}

public override BrowserNewContextOptions ContextOptions()
{
return new BrowserNewContextOptions()
{
ColorScheme = ColorScheme.Light,
ViewportSize = new()
{
Width = 1920,
Height = 1080
},
BaseURL = "https://github.com",
};
}
}

自訂 Browser/launch options

Browser/launch options 可以透過使用執行設定檔案或直接透過 CLI 設定執行設定選項來覆蓋。請參閱以下範例:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<Playwright>
<BrowserName>chromium</BrowserName>
<LaunchOptions>
<Headless>false</Headless>
<Channel>msedge</Channel>
</LaunchOptions>
</Playwright>
</RunSettings>
dotnet test -- Playwright.BrowserName=chromium Playwright.LaunchOptions.Headless=false Playwright.LaunchOptions.Channel=msedge

使用詳細 API 日誌

當你啟用了 verbose API log, 通過 DEBUG 環境變數,你將在標準錯誤流中看到這些訊息。在 MSTest 中,在 Visual Studio 內,這些訊息將顯示在 Output 視窗的 Tests 窗格中。它也會顯示在每個測試的 Test Log 中。

使用 .runsettings 檔案

當從 Visual Studio 執行測試時,你可以利用 .runsettings 檔案。以下顯示支援值的參考。

例如,要指定工作者的數量,你可以使用 MSTest.Parallelize.Workers。你也可以使用 RunConfiguration.EnvironmentVariables 啟用 DEBUG 日誌。

<RunSettings>
<!-- MSTest adapter -->
<MSTest>
<Parallelize>
<Workers>4</Workers>
<Scope>ClassLevel</Scope>
</Parallelize>
</MSTest>
<!-- General run configuration -->
<RunConfiguration>
<EnvironmentVariables>
<!-- For debugging selectors, it's recommend to set the following environment variable -->
<DEBUG>pw:api</DEBUG>
</EnvironmentVariables>
</RunConfiguration>
<!-- Playwright -->
<Playwright>
<BrowserName>chromium</BrowserName>
<ExpectTimeout>5000</ExpectTimeout>
<LaunchOptions>
<Headless>false</Headless>
<Channel>msedge</Channel>
</LaunchOptions>
</Playwright>
</RunSettings>

Playwright 的基礎 MSTest 類別

Microsoft.Playwright.MSTest 命名空間中,有一些可用的基礎類別:

TestDescription
PageTestEach test gets a fresh copy of a web Page 建立 in its own unique BrowserContext. Extending this 類別 is the simplest way of writing a fully-functional Playwright test。

Note: You can override the ContextOptions method in each test file to control context options, the ones typically passed into the Browser.NewContextAsync method。That way you can specify all kinds of emulation options for your test file individually。
ContextTestEach test will get a fresh copy of a BrowserContext. You can 建立 as many pages in this context as you'd like。Using this test is the easiest way to test multi-page scenarios where you need more than one tab。

Note: You can override the ContextOptions method in each test file to control context options, the ones typically passed into the Browser.NewContextAsync method。That way you can specify all kinds of emulation options for your test file individually。
BrowserTestEach test will get a browser and can 建立 as many contexts as it likes。Each test is responsible for cleaning up all the contexts it 建立。
PlaywrightTestThis gives each test a Playwright 物件 so that the test could start and stop as many browsers as it likes。

NUnit

Playwright 提供基礎類別來使用 NUnit 撰寫測試,透過 Microsoft.Playwright.NUnit 套件。

請查看安裝指南以開始使用。

執行 NUnit 測試在平行處理

根據預設,NUnit 會平行執行所有測試檔案,同時在每個檔案內部順序執行測試(ParallelScope.Self)。它會建立與主機系統上核心數量相同的程序。您可以使用 NUnit.NumberOfTestWorkers 參數來調整此行為。僅支援 ParallelScope.Self

對於 CPU 密集型測試,我們建議使用與系統核心數量相同的工作者數量,除以 2。對於 IO 密集型測試,您可以使用與核心數量相同的工作者數量。

dotnet test -- NUnit.NumberOfTestWorkers=5

自訂 BrowserContext 選項

要自訂上下文選項,您可以覆寫從 Microsoft.Playwright.MSTest.PageTestMicrosoft.Playwright.MSTest.ContextTest 派生的測試類別的 ContextOptions 方法。請參閱以下範例:

using Microsoft.Playwright.NUnit;

namespace PlaywrightTests;

[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class MyTest : PageTest
{
[Test]
public async Task TestWithCustomContextOptions()
{
// The following Page (and BrowserContext) instance has the custom colorScheme, viewport and baseURL set:
await Page.GotoAsync("/login");
}

public override BrowserNewContextOptions ContextOptions()
{
return new BrowserNewContextOptions()
{
ColorScheme = ColorScheme.Light,
ViewportSize = new()
{
Width = 1920,
Height = 1080
},
BaseURL = "https://github.com",
};
}
}

自訂 Browser/launch options

Browser/launch options 可以透過使用執行設定檔案或直接透過 CLI 設定執行設定選項來覆蓋。請參閱以下範例:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<Playwright>
<BrowserName>chromium</BrowserName>
<LaunchOptions>
<Headless>false</Headless>
<Channel>msedge</Channel>
</LaunchOptions>
</Playwright>
</RunSettings>
dotnet test -- Playwright.BrowserName=chromium Playwright.LaunchOptions.Headless=false Playwright.LaunchOptions.Channel=msedge

使用詳細 API 日誌

當你啟用詳細 API 日誌時,通過 DEBUG 環境變數,你將在標準錯誤流中看到這些訊息。在 NUnit 中,在 Visual Studio 內,這些訊息將顯示在 Output 視窗的 Tests 窗格中。它也會顯示在每個測試的 Test Log 中。

使用 .runsettings 檔案

當從 Visual Studio 執行測試時,你可以利用 .runsettings 檔案。以下顯示支援值的參考。

例如,要指定工作人員的數量,你可以使用 NUnit.NumberOfTestWorkers 或啟用 DEBUG 日誌 RunConfiguration.EnvironmentVariables

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- NUnit adapter -->
<NUnit>
<NumberOfTestWorkers>24</NumberOfTestWorkers>
</NUnit>
<!-- General run configuration -->
<RunConfiguration>
<EnvironmentVariables>
<!-- For debugging selectors, it's recommend to set the following environment variable -->
<DEBUG>pw:api</DEBUG>
</EnvironmentVariables>
</RunConfiguration>
<!-- Playwright -->
<Playwright>
<BrowserName>chromium</BrowserName>
<ExpectTimeout>5000</ExpectTimeout>
<LaunchOptions>
<Headless>false</Headless>
<Channel>msedge</Channel>
</LaunchOptions>
</Playwright>
</RunSettings>

Playwright 的基本 NUnit 類別

Microsoft.Playwright.NUnit 命名空間中,有一些可用的基礎類別:

TestDescription
PageTestEach test gets a fresh copy of a web Page 建立 in its own unique BrowserContext. Extending this 類別 is the simplest way of writing a fully-functional Playwright test。

Note: You can override the ContextOptions method in each test file to control context options, the ones typically passed into the Browser.NewContextAsync method。That way you can specify all kinds of emulation options for your test file individually。
ContextTestEach test will get a fresh copy of a BrowserContext. You can 建立 as many pages in this context as you'd like。Using this test is the easiest way to test multi-page scenarios where you need more than one tab。

Note: You can override the ContextOptions method in each test file to control context options, the ones typically passed into the Browser.NewContextAsync method。That way you can specify all kinds of emulation options for your test file individually。
BrowserTestEach test will get a browser and can 建立 as many contexts as it likes。Each test is responsible for cleaning up all the contexts it 建立。
PlaywrightTestThis gives each test a Playwright 物件 so that the test could start and stop as many browsers as it likes。

xUnit 支援

使用 xUnit 也是支援的,但我們不支援平行執行測試。這是一個眾所周知的問題/設計限制,由維護者在幾個 問題中概述。