インストール
はじめに
Playwright は、エンドツーエンドテストのニーズに対応するために特別に作成されました。Playwright は、Chromium、WebKit、Firefox など、すべての最新のレンダリングエンジンをサポートしています。Windows、Linux、macOS で、ローカルまたは CI で、ヘッドレスまたはヘッド付きで、ネイティブのモバイルエミュレーションでテストを実行します。
エンドツーエンドテストを作成するために、Playwright が提供する MSTest、NUnit、または xUnit の基本クラスを使用できます。これらのクラスは、複数のブラウザエンジンでのテストの実行、テストの並列化、起動/コンテキストオプションの調整、およびテストごとにPage/BrowserContext インスタンスをすぐに取得することをサポートしています。あるいは、ライブラリを使用して、テストインフラストラクチャを手動で作成することもできます。
- まず、
dotnet newで新しいプロジェクトを作成します。これにより、PlaywrightTestsディレクトリが作成され、その中にUnitTest1.csファイルが含まれます。
- MSTest
- NUnit
- xUnit
- xUnit v3
dotnet new nunit -n PlaywrightTests
cd PlaywrightTests
dotnet new mstest -n PlaywrightTests
cd PlaywrightTests
dotnet new xunit -n PlaywrightTests
cd PlaywrightTests
dotnet new xunit -n PlaywrightTests
cd PlaywrightTests
- 必要な Playwright の依存関係をインストールします。
- MSTest
- NUnit
- xUnit
- xUnit v3
dotnet add package Microsoft.Playwright.NUnit
dotnet add package Microsoft.Playwright.MSTest
dotnet add package Microsoft.Playwright.Xunit
dotnet add package Microsoft.Playwright.Xunit.v3
- プロジェクトをビルドして、
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
- xUnit v3
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.dokyumento.jp");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[Test]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.dokyumento.jp");
// 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.dokyumento.jp");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[TestMethod]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.dokyumento.jp");
// 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.dokyumento.jp");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[Fact]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.dokyumento.jp");
// 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.v3;
namespace PlaywrightTests;
public class UnitTest1: PageTest
{
[Fact]
public async Task HasTitle()
{
await Page.GotoAsync("https://playwright.dokyumento.jp");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[Fact]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.dokyumento.jp");
// 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 Subsystem for Linux (WSL)。
- macOS 14 Ventura 以降。
- Debian 12、Debian 13、Ubuntu 22.04、Ubuntu 24.04、x86-64 および arm64 アーキテクチャ。