Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Load Playwright settings programmatically as an alternative to a .runsettings file #3081

Open
0xced opened this issue Dec 8, 2024 · 1 comment · May be fixed by #3082
Open

[Feature]: Load Playwright settings programmatically as an alternative to a .runsettings file #3081

0xced opened this issue Dec 8, 2024 · 1 comment · May be fixed by #3082

Comments

@0xced
Copy link
Contributor

0xced commented Dec 8, 2024

🚀 Feature Request

Currently, setting the BrowserTypeLaunchOptions when running tests with the Microsoft.Playwright.MSTest, Microsoft.Playwright.NUnit or Microsoft.Playwright.Xunit pacakge is controlled through the .runsettings configuration file.

It would be nice to be able to control the launch options programmatically, just like BrowserNewContextOptions can be controlled programmatically by overriding the ContextOptions() method.

Example

using System;
using System.Threading.Tasks;
using Microsoft.Playwright;
using Microsoft.Playwright.TestAdapter;
using Microsoft.Playwright.Xunit;
using Xunit;

public class IntegrationTests(WebAppFixture webAppFixture) : PageTest, IClassFixture<WebAppFixture>
{
    // 👇 Proposed new virtual method where browser, launch options and more can be controlled programmatically
    protected override PlaywrightSettings PlaywrightSettings()
    {
        return new PlaywrightSettings
        {
            Browser = PlaywrightBrowser.Firefox,
            LaunchOptions = new BrowserTypeLaunchOptions
            {
                Headless = false,
                SlowMo = 100,
                Timeout = 20_000,
            },
            ExpectTimeout = TimeSpan.FromSeconds(10),
        };
    }

    [Fact]
    public async Task TestSearchPage()
    {
        await Page.GotoAsync(webAppFixture.Url("search"));
        await Expect(Page).ToHaveTitleAsync("Search");
    }
}

This example is given with Xunit but would work the same with MSTest or NUnit.

Motivation

Controlling options programatically opens up new possibilities that are not achievable with a .runsettings file. For example, one could decide to run tests on Firefox based on some runtime conditions or change the value of the Timeout based on what hardware the tests are running on. This would give a lot of power to Playwright users.

Also, having a new PlaywrightSettings type is great for discoverability. Typing properties with autocompletion support from your IDE is a much better experience than writing XML elements in a .runsettings file without any autocompletion.

Finally, .runsettings support in Rider is pretty broken, I was not able to get the Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter.ISettingsProvider.Load method to be called, rendring Playwright .runsettings unusable with Rider. See also TestCaseFilter in .runsettings ignored.

0xced added a commit to 0xced/playwright-dotnet that referenced this issue Dec 8, 2024
As an alternative to using a .runsettings XML file.

Fixes microsoft#3081
0xced added a commit to 0xced/playwright-dotnet that referenced this issue Dec 8, 2024
As an alternative to using a .runsettings XML file.

Fixes microsoft#3081
0xced added a commit to 0xced/playwright-dotnet that referenced this issue Dec 9, 2024
As an alternative to using a .runsettings XML file.

Fixes microsoft#3081
@mxschmitt
Copy link
Member

We intentionally had these settings configured on a global level before - this would require us to diverge from what we do in Node.js or Python. Usually for end-to-end tests, you write a test once and run them on all the browsers. For that you run dotnet test 3 times. I recommend to reach out to Jetbrains to fix the issue with runsettings - in the CLI it seems to work all expected, so its an issue with the IDE. Do you mind elaborating why you want to run some test files only in a specific browser? We recommend having multiple runsettings files if you want to specify custom timeouts etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants