Installation
Introduction
Playwright was created specifically to accommodate the needs of end-to-end testing. Playwright supports all modern rendering engines including Chromium, WebKit, and Firefox. Test on Windows, Linux, and macOS, locally or on CI, headless or headed with native mobile emulation.
You can choose to use MSTest, NUnit, or xUnit base classes that Playwright provides to write end-to-end tests. These classes support running tests on multiple browser engines, parallelizing tests, adjusting launch/context options and getting a Page/BrowserContext instance per test out of the box. Alternatively you can use the library to manually write the testing infrastructure.
- Start by creating a new project with
dotnet new. This will create thePlaywrightTestsdirectory which includes aUnitTest1.csfile:
- 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 xunit3 -n PlaywrightTests
cd PlaywrightTests
- Install the necessary Playwright dependencies:
- 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
- Build the project so the
playwright.ps1is available inside thebindirectory:
dotnet build
- Install required browsers. This example uses
net8.0, if you are using a different version of .NET you will need to adjust the command and changenet8.0to your version.
pwsh bin/Debug/net8.0/playwright.ps1 install
If pwsh is not available, you will have to install PowerShell.
Add Example Tests
Edit the UnitTest1.cs file with the code below to create an example end-to-end test:
- MSTest
- NUnit
- xUnit
- xUnit v3
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");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[Test]
public async