Microsoft Playwright for .NET NUnit, cant launch tests in headed mode

Viewed 633

Im starting to use Playwright to automate my tests for a Blazor WebAssembly application, i created a new NUnit Test project and following the docs on playwright.dev i installed:

dotnet add package Microsoft.Playwright.NUnit

doing so i can inherit the PageTest class and directly use a Page object already configured for me without needing to instantiate the browser and the context

now i need to see what happens during my tests so i wanted to enable headed mode, normally i would launch the browser with this options like this:

await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions
{
    Headless = false,
    SlowMo = 50,
});

but i cant do that here, the official docs suggest to use this commands on the console

set HEADED=1

dotnet test

but nothing happens, the tests are run headless mode regardless.

3 Answers

For the headless mode I first set the following in the terminal "$env:HEADED=1", so that it will open a browser. Keep in mind you have to set it to "0" if you want to run it in headless mode again. From what I understood, after inheriting from PageTest, due to lack of config file, such things (headless/slowMo) should be set runtime from the terminal.

Use this

await using var browser = await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false, SlowMo = 50 });
var context = await browser.NewContextAsync();
var page = await context.NewPageAsync();
await page.GotoAsync("The site you want to go");

It should work

If you set the environment variable PWDEBUG before your tests run, they will run in headed mode.

e.g. by doing something like this in a one time set up.

Environment.SetEnvironmentVariable("PWDEBUG", "console");

More information about Run in Debug Mode

Related