Playwright for .NET

repository·main·Indexed 25 days ago

https://github.com/microsoft/playwright-dotnet

An official language port of Playwright that enables developers to automate Chromium, Firefox, and WebKit browsers using a single API in .NET applications. The library is available via the Microsoft.Playwright NuGet package and supports Linux, macOS, and Windows.

Tokens
1.3K
Snippets
5
Records
8
Agent score
84%

What's inside Playwright for .NET

  1. Get started with Playwright for .NET

    main

    Playwright for .NET is the official .NET port of Playwright, designed for cross-browser web automation across Chromium, Firefox, and WebKit. It provides a single API to automate modern web applications reliably and fast.

    To use Playwright, you can install the Microsoft.Playwright NuGet package. The library supports Linux, macOS, and Windows.

    using System.Threading.Tasks;
    using Microsoft.Playwright;
    
    using var playwright = await Playwright.CreateAsync();
    await using var browser = await playwright.Chromium.LaunchAsync(new() { Headless = false });
    var page = await browser.NewPageAsync();
    await page.GotoAsync("https://playwright.dev/dotnet");
    await page.ScreenshotAsync(new() { Path = "screenshot.png" });
  2. Roll Playwright-dotnet to a new driver version

    main

    You can automate the process of downloading a new driver, re-generating API and transport channels, and updating the README by using the --roll flag with the build script.

    Steps to Roll:

    1. Checkout the upstream main or release branch of the playwright repository.
    2. Checkout the main or release branch of the playwright-dotnet repository.
    3. Identify the latest driver version from GitHub Actions.
      • For releases, use the format 1.X.Y.
      • For the main branch, use the format 1.X.Y-<timestamp>.
    4. (Optional) If the playwright project is not located at ../playwright, set the PW_SRC_DIR environment variable to the correct path.
    5. Run the build script with the --roll flag followed by the target driver version.
    ./build.sh --roll <driver-version>
  3. Basic browser automation example

    main

    This example demonstrates how to initialize Playwright, launch a Chromium browser in non-headless mode, navigate to a URL, and take a screenshot.

    using System.Threading.Tasks;
    using Microsoft.Playwright;
    
    using var playwright = await Playwright.CreateAsync();
    await using var browser = await playwright.Chromium.LaunchAsync(new() { Headless = false });
    var page = await browser.NewPageAsync();
    await page.GotoAsync("https://playwright.dev/dotnet");
    await page.ScreenshotAsync(new() { Path = "screenshot.png" });
  4. Configure Playwright Test settings via playwright.config.ts

    main

    The playwright.config.ts file allows you to define the execution environment and behavior for Playwright tests. The following configuration options are used in the testing harness:

    • testDir: Specifies the directory where tests are located. In this project, it is set to ./tests.
    • timeout: The maximum time limit for a single test in milliseconds. Here, it is set to 120,000 ms (2 minutes).
    • workers: The number of worker processes to use for running tests. Set to 1 for sequential execution.
    • reporter: Defines the format of the test output. The list reporter provides a list of test results in the console.
    • reportSlowTests: Configures the threshold for reporting slow tests. Setting this to null disables slow test reporting.
    import type { PlaywrightTestConfig } from '@playwright/test';
    
    const config: PlaywrightTestConfig = {
      testDir: './tests',
      timeout: 2 * 60 * 1_000,
      workers: 1,
      reporter: 'list',
      reportSlowTests: null,
    };
    
    export default config;