Giter Site home page Giter Site logo

playwright-sharp's Introduction

๐ŸŽญ Playwright for .NET NuGet version Join Slack

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 86.0.4238.0 โœ… โœ… โœ…
WebKit 14.0 โœ… โœ… โœ…
Firefox 80.0b8 โœ… โœ… โœ…

Headless execution is supported for all browsers on all platforms.

Usage

Playwright Sharp relies on two external components: The browsers and the Playwright driver.

await Playwright.InstallAsync();
var playwright = await Playwright.CreateAsync();

Playwright.InstallAsync() will download the required browsers. Playwright.CreateAsync() will create and launch the playwright driver.

await Playwright.InstallAsync();
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);

To avoid downloading the browsers in runtime, you can install the playwright-sharp dotnet tool:

dotnet tool install playwright-sharp-tool -g
playwright-sharp install-browsers

By running these two commands, you can avoid having the await Playwright.InstallAsync(); line in your code.

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.

await Playwright.InstallAsync();

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Webkit.LaunchAsync(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://maps.google.com");
page.ClickAsync("text='Your location'"); //
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.

await Playwright.InstallAsync();
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 WebKit page to log all network requests.

await Playwright.InstallAsync();
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");

Monthly reports

Useful links

playwright-sharp's People

Contributors

kblok avatar meir017 avatar slang25 avatar mxschmitt avatar flinde avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.