測試執行器
簡介
雖然 Playwright for .NET 並不綁定特定的測試執行器或測試框架,但根據我們的經驗,最簡單的入門方式是使用我們為 MSTest、NUnit、xUnit 或 xUnit v3 提供的基底類別。這些類別支援在多個瀏覽器引擎上執行測試、調整啟動/情境選項,並為每個測試提供 Page/BrowserContext 實例。
Playwright 和 Browser 實例將在測試之間重複使用以獲得更好的效能。我們建議在新的 BrowserContext 中執行每個測試案例,這樣瀏覽器狀態就會在測試之間被隔離。
- MSTest
- NUnit
- xUnit
- xUnit v3
Playwright 透過 Microsoft.Playwright.NUnit
套件提供基底類別來撰寫 NUnit 測試。
Playwright 透過 Microsoft.Playwright.MSTest
套件提供基底類別來撰寫 MSTest 測試。
Playwright 透過 Microsoft.Playwright.Xunit
套件提供基底類別來撰寫 xUnit 測試。
Playwright 透過 Microsoft.Playwright.Xunit.v3
套件提供基底類別來撰寫 xUnit v3 測試。
請查看安裝指南以開始使用。
平行執行測試
- MSTest
- NUnit
- xUnit
- xUnit v3
預設情況下,NUnit 會平行執行所有測試檔案,而在每個檔案內循序執行測試(ParallelScope.Self
)。它會建立與主機系統核心數相同的工作程序。您可以使用 NUnit.NumberOfTestWorkers 參數來調整此行為。僅支援 ParallelScope.Self
。
對於 CPU 密集型測試,我們建議使用系統核心數除以 2 的工作程序數。對於 IO 密集型測試,您可以使用與核心數相同的工作程序數。
dotnet test -- NUnit.NumberOfTestWorkers=5
預設情況下,MSTest 會平行執行所有類別,而在每個類別內循序執行測試(ExecutionScope.ClassLevel
)。它會建立與主機系統核心數相同的工作程序。您可以使用以下 CLI 參數或使用 .runsettings
檔案來調整此行為,請參見下文。不支援在方法層級平行執行測試(ExecutionScope.MethodLevel
)。
dotnet test --settings:.runsettings -- MSTest.Parallelize.Workers=4
By default xUnit will run all classes in parallel, while running tests inside each class sequentially. It will create by default as many processes as there are cores on the system. You can adjust this behavior by using the following CLI parameter or using a .runsettings
file, see below.
dotnet test -- xUnit.MaxParallelThreads=5
We recommend xUnit 2.8+ which uses the conservative
parallelism algorithm by default.
By default xUnit v3 will run all classes in parallel, while running tests inside each class sequentially. It will create by default as many processes as there are cores on the system. You can adjust this behavior by using the following CLI parameter or using a .runsettings
file, see below.
dotnet test -- xUnit.MaxParallelThreads=5
xUnit v3 uses the conservative
parallelism algorithm by default.
Customizing BrowserContext options
- MSTest
- NUnit
- xUnit
- xUnit v3
To customize context options, you can override the ContextOptions
method of your test class derived from Microsoft.Playwright.MSTest.PageTest
or Microsoft.Playwright.MSTest.ContextTest
. See the following example:
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",
};
}
}
To customize context options, you can override the ContextOptions
method of your test class derived from Microsoft.Playwright.MSTest.PageTest
or Microsoft.Playwright.MSTest.ContextTest
. See the following example:
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",
};
}
}
To customize context options, you can override the ContextOptions
method of your test class derived from Microsoft.Playwright.Xunit.PageTest
or Microsoft.Playwright.Xunit.ContextTest
. See the following example:
using Microsoft.Playwright;
using Microsoft.Playwright.Xunit;
namespace PlaywrightTests;
public class UnitTest1 : PageTest
{
[Fact]
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",
};
}
}
To customize context options, you can override the ContextOptions
method of your test class derived from Microsoft.Playwright.Xunit.v3.PageTest
or Microsoft.Playwright.Xunit.v3.ContextTest
. See the following example:
using Microsoft.Playwright;
using Microsoft.Playwright.Xunit.v3;
namespace PlaywrightTests;
public class UnitTest1 : PageTest
{
[Fact]
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",
};
}
}
Customizing Browser/launch options
Browser/launch options can be overridden either using a run settings file or by setting the run settings options directly via the CLI. See the following example:
<?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
Using Verbose API Logs
When you have enabled the verbose API log, via the DEBUG
environment variable, you will see the messages in the standard error stream. Within Visual Studio, that will be the Tests
pane of the Output
window. It will also be displayed in the Test Log
for each test.
Using the .runsettings file
When running tests from Visual Studio, you can take advantage of the .runsettings
file. The following shows a reference of the supported values.
- MSTest
- NUnit
- xUnit
- xUnit v3
For example, to specify the number of workers you can use NUnit.NumberOfTestWorkers
or to enable DEBUG
logs 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>
For example, to specify the number of workers, you can use MSTest.Parallelize.Workers
. You can also enable DEBUG
logs using RunConfiguration.EnvironmentVariables
.
<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>
For example, to specify the number of workers, you can use xUnit.MaxParallelThreads
. You can also enable DEBUG
logs using RunConfiguration.EnvironmentVariables
.
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- See https://xunit.net/docs/runsettings -->
<xUnit>
<MaxParallelThreads>1</MaxParallelThreads>
</xUnit>
<!-- 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>
For example, to specify the number of workers, you can use xUnit.MaxParallelThreads
. You can also enable DEBUG
logs using RunConfiguration.EnvironmentVariables
.
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- See https://xunit.net/docs/runsettings -->
<xUnit>
<MaxParallelThreads>1</MaxParallelThreads>
</xUnit>
<!-- 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>
Base classes for Playwright
- MSTest
- NUnit
- xUnit
- xUnit v3
There are a few base classes available to you in Microsoft.Playwright.NUnit
namespace:
There are a few base classes available to you in Microsoft.Playwright.MSTest
namespace:
There are a few base classes available to you in Microsoft.Playwright.Xunit
namespace:
There are a few base classes available to you in Microsoft.Playwright.Xunit.v3
namespace:
Test | Description |
---|---|
PageTest | Each test gets a fresh copy of a web Page created in its own unique BrowserContext. Extending this class 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. |
ContextTest | Each test will get a fresh copy of a BrowserContext. You can create 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. |
BrowserTest | Each test will get a browser and can create as many contexts as it likes. Each test is responsible for cleaning up all the contexts it created. |
PlaywrightTest | This gives each test a Playwright object so that the test could start and stop as many browsers as it likes. |