インストール
はじめに
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
- プロジェクトをビルドして、`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.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以降、またはLinux用Windowsサブシステム (WSL)。
- macOS 14 Ventura、またはそれ以降。
- Debian 12、Ubuntu 22.04、Ubuntu 24.04、x86-64およびarm64アーキテクチャ。