gshev / playwright-extra-sharp

MIT License
9 stars 4 forks source link

PlaywrightExtraSharp

NuGet Badge

Based on Puppeteer extra sharp and Node.js Playwright extra

Quickstart

Long way

// Initialization plugin builder
var playwrightExtra = new PlaywrightExtra(BrowserTypeEnum.Chromium); 

// Install browser
playwrightExtra.Install();

// Use stealth plugin
playwrightExtra.Use(new StealthExtraPlugin());

// Launch the puppeteer browser with plugins
await playwrightExtra.LaunchAsync(new ()
{
    Headless = false
});

// Create a new page
var page = await playwrightExtra.NewPageAsync();
await page.GoToAsync("http://google.com");

Compact way

// Initialize, install, use plugin and launch
var playwrightExtra = await new PlaywrightExtra(BrowserTypeEnum.Chromium)
    .Install()
    .Use(new StealthExtraPlugin())
    .LaunchAsync(new ()
    {
        Headless = false
    });

// Create a new page
var page = await playwrightExtra.NewPageAsync();
await page.GoToAsync("http://google.com");

Note

Because of how Playwright behaves with pages, you should only create new pages through PlaywrightExtra wrapper and not IBrowser. Please refer to examples above.

Context Persistence

There are 4 different ways to work with context in PlaywrightExtra:

  1. launch incognito (as default was) with permanent context
.LaunchAsync(new ()
    {
        Headless = false
    },
    persistContext: true)
  1. launch incognito with new context per page (so when page is closed, context is disposed)
.LaunchAsync(new ()
    {
        Headless = false
    },
    persistContext: false)
  1. launch persistent (user data dir is considered) with permanent context
.LaunchPersistentAsync(new ()
    {
        Headless = false
    },
    persistContext: true)
  1. launch persistent with new context per page
.LaunchPersistentAsync(new ()
    {
        Headless = false
    },
    persistContext: false)

User data directory

While running persistent mode you can specify path to user data directory.

When context is persisted, specify directory at launch:

.LaunchPersistentAsync(new ()
    {
        Headless = false
    },
    persistContext: true,
    userDataDir: "/path/to/userdir")

When context is created for each page (persistContext: false), specify directory at page creation:

var page = await _playwrightExtra.NewPageAsync(userDataDir: "/path/to/userdir");

Helpers

For convenience, you can use helper method when performing request:

await page.GotoAndWaitForIdleAsync("http://google.com/",
    idleTime: TimeSpan.FromMilliseconds(1000));

Basically this works like if you perform request with option WaitUntil = WaitUntilState.NetworkIdle

But unlike builtin method, you can specify amount of time to wait after last request has fired.