Skip to content

.NET version of the Playwright testing and automation library.

License

Notifications You must be signed in to change notification settings

interiv/playwright-sharp

 
 

Repository files navigation

🎭 Playwright for .NET

NuGet version Join Slack

Playwright is a .NET library to automate Chromium, Firefox and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable and fast.

Linux macOS Windows
92.0.4498.0
14.2
89.0b6

Headless execution is supported for all the browsers on all platforms. Check out system requirements for details.

Usage

Capabilities

Playwright is built to automate the broad and growing set of web browser capabilities used by Single Page Apps and Progressive Web Apps.

  • Scenarios that span multiple page, domains and iframes
  • Auto-wait for elements to be ready before executing actions (like click, fill)
  • Intercept network activity for stubbing and mocking network requests
  • Emulate mobile devices, geolocation, permissions
  • Support for web components via shadow-piercing selectors
  • Native input events for mouse and keyboard
  • Upload and download files

Examples

Mobile and geolocation

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");

Evaluate in browser context

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);

Intercept network requests

This code snippet sets up request routing for a Firefox 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");

CLI & Codegen

Playwright conveniently comes with a built-in codegen facility which lets you open an inspector and a browser window, then record the actions you perform, generating the relevant source code as you do.

To use it, you need to install the dotnet tool:

dotnet-install --global microsoft.playwright.cli

Then, you can simply call:

playwrightcli codegen https://www.microsoft.com

Resources

About

.NET version of the Playwright testing and automation library.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • C# 98.4%
  • HTML 1.4%
  • Other 0.2%