安裝
簡介
Playwright 的創建 বিশেষভাবে滿足端對端測試的需求。Playwright 支援所有現代渲染引擎,包括 Chromium、WebKit 和 Firefox。在 Windows、Linux 和 macOS 上進行測試,本地或在 CI 上,無頭或有頭模式,並具有原生行動裝置模擬。
您可以選擇使用 MSTest、NUnit 或 xUnit 基底類別,Playwright 提供這些類別來編寫端對端測試。這些類別支援在多個瀏覽器引擎上執行測試、平行化測試、調整啟動/上下文選項,並為每個測試提供一個開箱即用的 Page/BrowserContext 實例。或者,您可以使用 程式庫 手動編寫測試基礎架構。
- 首先使用
dotnet new
建立一個新專案。這將建立包含UnitTest1.cs
檔案的PlaywrightTests
目錄
- MSTest
- NUnit
- xUnit
dotnet new nunit -n PlaywrightTests
cd PlaywrightTests
dotnet new mstest -n PlaywrightTests
cd PlaywrightTests
dotnet new xunit -n PlaywrightTests
cd PlaywrightTests
- 安裝必要的 Playwright 相依性
- MSTest
- NUnit
- xUnit
dotnet add package Microsoft.Playwright.NUnit
dotnet add package Microsoft.Playwright.MSTest
dotnet add package Microsoft.Playwright.Xunit
- 建置專案,使
playwright.ps1
在bin
目錄內可用
dotnet build
- 安裝所需的瀏覽器。此範例使用
net8.0
,如果您使用不同版本的 .NET,您需要調整命令並將net8.0
變更為您的版本。
pwsh bin/Debug/net8.0/playwright.ps1 install
如果 pwsh
不可用,您將必須安裝 PowerShell。
新增範例測試
使用以下程式碼編輯 UnitTest1.cs
檔案以建立範例端對端測試
- MSTest
- NUnit
- xUnit
UnitTest1.cs
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class ExampleTest : PageTest
{
[Test]
public async Task HasTitle()
{
await Page.GotoAsync("https://playwright.dev.org.tw");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[Test]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.dev.org.tw");
// Click the get started link.
await Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }).ClickAsync();
// Expects page to have a heading with the name of Installation.
await Expect(Page.GetByRole(AriaRole.Heading, new() { Name = "Installation" })).ToBeVisibleAsync();
}
}
UnitTest1.cs
using System.Text.RegularExpressions;
using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;
namespace PlaywrightTests;
[TestClass]
public class ExampleTest : PageTest
{
[TestMethod]
public async Task HasTitle()
{
await Page.GotoAsync("https://playwright.dev.org.tw");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[TestMethod]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.dev.org.tw");
// Click the get started link.
await Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }).ClickAsync();
// Expects page to have a heading with the name of Installation.
await Expect(Page.GetByRole(AriaRole.Heading, new() { Name = "Installation" })).ToBeVisibleAsync();
}
}
UnitTest1.cs
using System.Text.RegularExpressions;
using Microsoft.Playwright;
using Microsoft.Playwright.Xunit;
namespace PlaywrightTests;
public class UnitTest1: PageTest
{
[Fact]
public async Task HasTitle()
{
await Page.GotoAsync("https://playwright.dev.org.tw");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[Fact]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.dev.org.tw");
// Click the get started link.
await Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }).ClickAsync();
// Expects page to have a heading with the name of Installation.
await Expect(Page.GetByRole(AriaRole.Heading, new() { Name = "Installation" })).ToBeVisibleAsync();
}
}
執行範例測試
預設情況下,測試將在 Chromium 上執行。這可以透過 BROWSER
環境變數或調整啟動設定選項來設定。測試在無頭模式下執行,這表示執行測試時不會開啟瀏覽器。測試結果和測試日誌將顯示在終端機中。
dotnet test
請參閱我們關於執行與偵錯測試的文件,以了解更多關於在有頭模式下執行測試、執行多個測試、執行特定設定等資訊。
系統需求
- Playwright 以 .NET Standard 2.0 程式庫形式發佈。我們建議使用 .NET 8。
- Windows 10+、Windows Server 2016+ 或 Windows Linux 子系統 (WSL)。
- macOS 13 Ventura 或更高版本。
- Debian 12、Ubuntu 22.04、Ubuntu 24.04,在 x86-64 和 arm64 架構上。