Playwright test runsettings not taking effect (Headless = False yet still runs Headless)

39 Views Asked by At

I have a Playwright test solution with a runsettings file, when the test (Test2) is run (via the Test Explorer in VS Code) it is in Headless mode and seems to ignore the runsettings file i've created in the root directory (see below). I have another test (Test1) in the same solution which explicitly sets the BrowserTypeLaunchOptions in the class `rather than utilise the Base PageTest class) and it runs with Chrome Browser as expected.

Test2 class code:

using Microsoft.Playwright;
using Microsoft.Playwright.NUnit;

namespace Playwright_.NET;

public class NUnitPlaywright : PageTest
{
    [SetUp]
    public async Task Setup()
    {
        await Page.GotoAsync(url: "http://www.eaapp.somee.com");
    }

    [Test]
    public async Task Test1()
    {
        await Page.ClickAsync(selector: "text=Login");

        await Page.ScreenshotAsync(new PageScreenshotOptions
        {
            Path = "../../../Screenshots/EAApp.jpg"
        });
        await Page.FillAsync(selector: "#UserName", value: "admin");
        await Page.FillAsync(selector: "#Password", value: "password");
        await Page.ClickAsync(selector: "text=Log in");
        await Page.ScreenshotAsync(new PageScreenshotOptions
        {
            Path = "../../../Screenshots/Login Assertion.jpg"
        });

        await Expect(Page.Locator(selector: "text='Employee Details'")).ToBeVisibleAsync();

    }
}

test.runsettings file

<?xml version="1.0" encoding="utf-8"?>

<RunSettings>
  <!-- Configurations that affect the Test Framework -->
  <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>
      <Channel>chrome</Channel>
    </LaunchOptions>
  </Playwright>

  <!-- NUnit adapter -->  
  <NUnit>
    <NumberOfTestWorkers>24</NumberOfTestWorkers>
  </NUnit>


</RunSettings>     

Test 1 class code

using Microsoft.Playwright;

namespace Playwright_.NET;

public class Tests
{
    [SetUp]
    public void Setup()
    {
    }

    [Test]
    public async Task Test1()
    {
        //Prepare Playwright driver
        using var playwright = await Playwright.CreateAsync();

        //Launch Browser (not in headless mode)
        await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions()
        {
            Headless = false
        });

        //Navigate to page
        var page = await browser.NewPageAsync(); //NewPageAsync - Convenience api. Frameworks should create IBrowser.NewContextAsync(BrowserNewContextOptions)
        await page.GotoAsync(url: "http://www.eaapp.somee.com");
        //Click login
        await page.ClickAsync(selector: "text=Login");
        //Take Screenshot of login page (current view)
        await page.ScreenshotAsync(new PageScreenshotOptions
        {
            Path = "../../../Screenshots/EAApp.jpg"
        });
        //Input login details and click Log in button
        await page.FillAsync(selector: "#UserName", value: "admin");
        await page.FillAsync(selector: "#Password", value: "password");
        await page.ClickAsync(selector: "text=Log in");
        await page.ScreenshotAsync(new PageScreenshotOptions
        {
            Path = "../../../Screenshots/Login Assertion.jpg"
        });
         //Assertion
        var isExist = await page.Locator(selector: "text='Employee Details'").IsVisibleAsync();
        Assert.IsTrue(isExist);

    }
}
1

There are 1 best solutions below

2
Gaurav Khurana On

You have to inform IDE that which file is run setting file and you can do that by

  1. Click on Test
  2. Configure Run setting
  3. Select solution wide run settings file

and select the file which is your run setting file

enter image description here