This guide provides a step-by-step introduction to setting up Playwright with .NET for browser automation and end-to-end testing.
- .NET SDK installed on your machine
- Familiarity with .NET unit testing frameworks like NUnit or MSTest
First, create a new .NET test project using NUnit:
dotnet new nunit -n PlaywrightTests
cd PlaywrightTests
Choose your preferred test framework and add the corresponding Playwright package:
For NUnit:
dotnet add package Microsoft.Playwright.NUnit
For MSTest:
dotnet add package Microsoft.Playwright.MSTest
dotnet build
To install Playwright's required browsers, use the following PowerShell command:
powershell.exe -File bin\Debug\net8.0\playwright.ps1 install
If pwsh is not available, you have to install PowerShell.
In your test project, add the following sample test to get started with Playwright:
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");
// 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");
// Click the "Get started" link.
await Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }).ClickAsync();
// Expect page to have a heading with the name "Installation."
await Expect(Page.GetByRole(AriaRole.Heading, new() { Name = "Installation" })).ToBeVisibleAsync();
}
}
Run your tests with the following command:
dotnet test
To enable parallel test execution in NUnit, create a .runsettings file in the root directory of your project and configure the settings as shown below:
Create a file named Chrome.runsettings.
Add the following content:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- NUnit adapter -->
<NUnit>
<NumberOfTestWorkers>24</NumberOfTestWorkers>
</NUnit>
<!-- General run configuration -->
<RunConfiguration>
<EnvironmentVariables>
<!-- For debugging selectors, it's recommend to set the following environment variable -->
<DEBUG>pw:api</DEBUG>
</EnvironmentVariables>
</RunConfiguration>
<!-- Playwright -->
<Playwright>
<BrowserName>chromium</BrowserName>
<ExpectTimeout>10000</ExpectTimeout>
<LaunchOptions>
<Headless>false</Headless>
<Channel>msedge</Channel>
</LaunchOptions>
</Playwright>
</RunSettings>
When running tests, use the --settings option to specify the .runsettings file:
dotnet test --settings edge.runsettings
- Add ExtentReports Package Install the ExtentReports package using the following command:
dotnet add package ExtentReports --version 5.0.4
- Add ConfigurationManager Package Install the ConfigurationManager package to handle app settings:
dotnet add package System.Configuration.ConfigurationManager --version 9.0.0
- Run Your Tests Execute the tests using the specified settings file:
dotnet test --settings edge.runsettings
- Locate the HTML Report After the tests run, an HTML report will be generated in the bin\Debug\net8.0 folder. Open the report in a browser to view test results.
For more details on configuring and customizing ExtentReports, visit the Official Documentation
This guide should give you a solid start with Playwright and .NET. For further details, refer to the Playwright documentation.