Playwright is a testing tool developed by Microsoft that provides reliable end-to-end cross-browsing testing for modern web applications. At the time of writing, it can be used to automate Chromium, Firefox, and WebKit. It is a similar tool to Selenium, but aims to be more reliable, resulting in less flaky tests. Playwright is also less resource-heavy and more performant than Selenium, resulting in faster test runs.
Playwright runs in a headless mode by default, which means that it does not launch an actual browser instance. This makes test execution fast, but it can make it difficult to debug a failing test. It also only runs on one browser type by default.
By using .runsettings files in .NET, we can control Playwright, allowing us to specify whether to run in headed or headless modes, as well as specifying which browser type to run. To control whether Playwright should run in headless mode or not, you need to set the HEADED environment variable in the runsettings file (This is opposite to headless, so to run headless, set this to 0 for false, and to run headed, set this to 1 for true).
To specify different browsers, create a runsettings file for each browser, changing the BrowserName element in each file.
Below are example files for running Playwright with each of the three supported browser types, and in headed mode. To run in headless mode, set the HEADED element to 0.
Chromium:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<EnvironmentVariables>
<HEADED>1</HEADED>
</EnvironmentVariables>
</RunConfiguration>
<Playwright>
<BrowserName>chromium</BrowserName>
</Playwright>
</RunSettings>
Firefox:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<EnvironmentVariables>
<HEADED>1</HEADED>
</EnvironmentVariables>
</RunConfiguration>
<Playwright>
<BrowserName>firefox</BrowserName>
</Playwright>
</RunSettings>
Webkit:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<EnvironmentVariables>
<HEADED>1</HEADED>
</EnvironmentVariables>
</RunConfiguration>
<Playwright>
<BrowserName>webkit</BrowserName>
</Playwright>
</RunSettings>
A PowerShell script can be used to run all these configurations:
Write-Host 'Running Playwright Tests'
dotnet test --settings:runsettings/chromium.runsettings
dotnet test --settings:runsettings/firefox.runsettings
dotnet test --settings:runsettings/webkit.runsettings
Read-Host "Press any key to quit."
I have a complete solution, including an example test in a NUnit test project that you can use as a reference for your own projects on my GitHub profile: MorneZaayman/How-to-use-Playwright-with-multiple-browsers-in-headless-or-headless-mode. After installing Playwright as per their documentation, execute the RunTests.ps1 file to run the tests. You can also use dotnet test to run the test directly, which will be in headless mode.