インストール
はじめに
Playwright は、エンドツーエンドテストのニーズに対応するために特別に作成されました。Playwright は、Chromium、WebKit、Firefox を含むすべての最新レンダリングエンジンをサポートしています。Windows、Linux、macOS で、ローカルまたは CI 上で、ヘッドレスまたはネイティブモバイルエミュレーションによるヘッド付きでテストします。
Playwright が提供する MSTest、NUnit、または xUnit 基本クラスを使用して、エンドツーエンドテストを作成できます。これらのクラスは、複数のブラウザエンジンでのテストの実行、テストの並列化、起動/コンテキストオプションの調整、およびテストごとに 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
bin
ディレクトリ内でplaywright.ps1
が利用可能になるようにプロジェクトをビルドします
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.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();
}
}
サンプルテストの実行
デフォルトでは、テストは Chromium で実行されます。これは、BROWSER
環境変数を使用するか、起動構成オプションを調整することで構成できます。テストはヘッドレスモードで実行されるため、テストの実行時にブラウザは開きません。テストの結果とテストログは、ターミナルに表示されます。
dotnet test
ヘッド付きモードでのテストの実行、複数のテストの実行、特定の構成の実行などについて詳しくは、テストの実行とデバッグに関するドキュメントをご覧ください。
システム要件
- Playwright は .NET Standard 2.0 ライブラリとして配布されています。 .NET 8 を推奨します。
- Windows 10 以降、Windows Server 2016 以降、または Windows Subsystem for Linux (WSL)。
- macOS 13 Ventura 以降。
- Debian 12、Ubuntu 22.04、Ubuntu 24.04 (x86-64 および arm64 アーキテクチャ上)。