3

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.

5 Answers 5

3

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

This is actually what worked for me.
2

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

1 Comment

This was the programatic way for me, config way did not work in my case.
2

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.

1 Comment

You should remember to blank the HEADED environment variable afterwards. This is done with $env:HEADED="" And you can see its value with echo $env:HEADED All of this should be done within a Powershell window.
1

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>

1 Comment

NUnit3TestAdapter is a dependency within Microsoft.Playwright.NUnit as is Microsoft.Playwright.TestAdapter. So you don't need to add these two packages explicitly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.