Ivan Ambla
Ivan Ambla

Reputation: 743

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

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.

Upvotes: 3

Views: 5438

Answers (5)

seangwright
seangwright

Reputation: 18225

The SlowMo and Headless options can be configured with a .runsettings file which you can specify when executing dotnet test

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

Create a headed-e2e.runsettings file:

<!-- headed-e2e.runsettings -->
<RunSettings>
    <!-- NUnit adapter -->
    <NUnit>
        <!-- ... -->
    </NUnit>
    <!-- General run configuration -->
    <RunConfiguration>
        <EnvironmentVariables>
            <!-- For debugging selectors, it's recommend to set the following environment variable -->
            <!-- <DEBUG>pw:api</DEBUG> -->
        </EnvironmentVariables>
    </RunConfiguration>
    <!-- Playwright -->
    <Playwright>
        <BrowserName>chromium</BrowserName>
        <ExpectTimeout>5000</ExpectTimeout>
        <LaunchOptions>
            <Headless>false</Headless>
            <SlowMo>1000</SlowMo>
            <Channel>chrome</Channel>
        </LaunchOptions>
    </Playwright>
</RunSettings>

Then specify that file when executing your tests:

dotnet test -s headed-e2e.runsettings

You could have other .runsettings files for other test use cases.

Note that the .runsettings file can be used for more than just Playwright

Upvotes: 3

Shahar
Shahar

Reputation: 11

The playwright libary also provided test Runners frameworks like: mstest, NUnit, and Xunit. With the testing runner framework - NUnit, you can use customizing Browser/launch options by using the run settings file - .runsettings. See the docs: https://playwright.dev/dotnet/docs/test-runners See it under - "customizing Browser/launch options under Nunit/mstest"

Also about .runsettings file https://learn.microsoft.com/en-us/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file?view=vs-2022 for that to work with Nunit you need NUnit3TestAdapter. So your .csproj file contains:

<ItemGroup>
  <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
  <PackageReference Include="Microsoft.Playwright.NUnit" Version="1.28.0" />
  <PackageReference Include="Microsoft.Playwright.TestAdapter" Version="1.28.0" />
  <PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
</ItemGroup>

Upvotes: 1

Shoejep
Shoejep

Reputation: 4849

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

Upvotes: 2

Svetoslav Ts.
Svetoslav Ts.

Reputation: 21

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.

Upvotes: 2

Nalin
Nalin

Reputation: 87

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

Upvotes: 2

Related Questions