Playwright can either be used with the MSTest, NUnit, or xUnit base classes or as a Playwright Library (this guide). If you are working on an application that utilizes Playwright capabilities or you are using Playwright with another test runner, read on.
UsageCreate a console project and add the Playwright dependency.
dotnet new console -n PlaywrightDemo
cd PlaywrightDemo
dotnet add package Microsoft.Playwright
dotnet build
pwsh bin/Debug/netX/playwright.ps1 install
dotnet tool update --global PowerShell
Create a Program.cs
that will navigate to https://playwright.dev/dotnet
and take a screenshot in Chromium.
using Microsoft.Playwright;
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
await page.GotoAsync("https://playwright.dev/dotnet");
await page.ScreenshotAsync(new()
{
Path = "screenshot.png"
});
Now run it.
By default, Playwright runs the browsers in headless mode. To see the browser UI, set Headless option to false
. You can also use SlowMo to slow down execution. Learn more in the debugging tools section.
await using var browser = await playwright.Firefox.LaunchAsync(new()
{
Headless = false,
SlowMo = 50,
});
Using Assertions
You can do the following to leverage Playwright's web-first assertions when you are using your own test framework. These will automatically retry until the condition is met, e.g. an element has a certain text or the timeout is reached:
using Microsoft.Playwright;
using static Microsoft.Playwright.Assertions;
SetDefaultExpectTimeout(10_000);
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
await page.GotoAsync("https://playwright.dev/dotnet");
await Expect(page.GetByRole(AriaRole.Link, new() { Name = "Get started" })).ToBeVisibleAsync();
Bundle drivers for different platforms
Playwright by default does bundle only the driver for the .NET publish target runtime. If you want to bundle for additional platforms, you can override this behavior by using either all
, none
or linux
, win
, osx
in your project file.
<PropertyGroup>
<PlaywrightPlatform>all</PlaywrightPlatform>
</PropertyGroup>
or:
<PropertyGroup>
<PlaywrightPlatform>osx;linux</PlaywrightPlatform>
</PropertyGroup>
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4