π Playwright for .NET
PlaywrightSharp is a .Net library to automate Chromium, Firefox and WebKit browsers with a single API. Playwright delivers automation that is ever-green, capable, reliable and fast. See how Playwright is better.
Linux | macOS | Windows | |
---|---|---|---|
Chromium 90.0.4421.0 | β | β | β |
WebKit 14.0 | β | β | β |
Firefox 86.0b10 | β | β | β |
Headless execution is supported for all browsers on all platforms.
Install PlaywrightSharp package from NuGet in Visual Studio or from the CLI in your project root directory:
dotnet add package PlaywrightSharp
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.bing.com");
await page.ScreenshotAsync(path: outputFile);
Playwright Sharp relies on two external components: The Playwright driver and the browsers.
Playwright drivers will be copied to the bin
folder at build time. Nuget will rely on the RuntimeIdentifier to copy a platform-specific driver, or on the runtime used on dotnet publish.
If the RuntimeIdentifier is not set, all runtimes will be copied inside a runtimes folder. Then, the platform-specific driver will be picked at run-time.
The way browsers are installed will vary depending on the use case scenario.
If you use Playwright in test projects, all required browsers will be installed at build time.
If you use the official Docker images, all the required browsers will be installed in that image. The minor version of the package will tell you which docker image tag to use. For instance, PlaywrightSharp 0.162.0 will work with the tag v1.6.2.
If you run Playwright in a remote server. For instance, as part of a web application, you will need to run .\bin\playwright-cli.exe install
in Windows or ./bin/playwright-cli install
in OSX/Linux, as part of your deploy script.
If none of the scenarios mentioned above cover your scenario, you can install the browsers programmatically using await Playwright.InstallAsync();
This snippet emulates Mobile Safari on a device at a given geolocation, navigates to maps.google.com, performs an action, and takes a screenshot.
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Webkit.LaunchAsync(headless: false);
var contextOptions = playwright.Devices["iPhone 11 Pro"].ToBrowserContextOptions();
contextOptions.Locale = "en-US";
contextOptions.Geolocation = new Geolocation { Longitude = 12.492507m, Latitude = 41.889938m };
contextOptions.Permissions = new[] { ContextPermission.Geolocation };
var context = await browser.NewContextAsync(contextOptions);
var page = await context.NewPageAsync();
await page.GoToAsync("https://www.google.com/maps");
await page.ClickAsync(".ml-button-my-location-fab");
await page.WaitForLoadStateAsync(LifecycleEvent.Networkidle);
if ((await page.QuerySelectorAsync(".ml-promotion-no-thanks")) != null)
{
await page.ClickAsync(".ml-promotion-no-thanks");
}
await page.ScreenshotAsync("colosseum-iphone.png");
This code snippet navigates to example.com in Firefox and executes a script in the page context.
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Firefox.LaunchAsync();
var context = await browser.NewContextAsync();
var page = await context.NewPageAsync();
await page.GoToAsync("https://www.example.com/");
var dimensions = await page.EvaluateAsync<Size>(@"() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
}
}");
Console.WriteLine(dimensions);
This code snippet sets up request routing for a WebKit page to log all network requests.
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Firefox.LaunchAsync();
var context = await browser.NewContextAsync();
var page = await context.NewPageAsync();
// Log and continue all network requests
await page.RouteAsync("**", (route, _) =>
{
Console.WriteLine(route.Request.Url);
route.ContinueAsync();
});
await page.GoToAsync("http://todomvc.com");