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] Set ContextOptions via runsettings file #2555

Open
hughesjs opened this issue Apr 18, 2023 · 8 comments
Open

[Feature] Set ContextOptions via runsettings file #2555

hughesjs opened this issue Apr 18, 2023 · 8 comments

Comments

@hughesjs
Copy link

One of the big benefits of using the nunit package is not having to worry about lots of setup boilerplate.

However, I can't seem to set the BaseUrl with an env var, which seems pretty crucial to me...

Can we get this added please?

@mxschmitt
Copy link
Member

mxschmitt commented Apr 18, 2023

You can do the following, would that work for you?

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

namespace PlaywrightTestsN;

[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class MyTest : PageTest
{
    [Test]
    public async Task TestWithCustomContextOptions()
    {
        // The following Page (and BrowserContext) instance has the custom baseURL set:
        await Page.GotoAsync("/login");
    }

    public override BrowserNewContextOptions ContextOptions()
    {
        var baseURL = Environment.GetEnvironmentVariable("BASE_URL");
        if (string.IsNullOrEmpty(baseURL))
        {
            throw new Exception("BASE_URL environment variable is not set.");
        }
        return new BrowserNewContextOptions()
        {
            BaseURL = baseURL,
        };
    }
}

@hughesjs
Copy link
Author

hughesjs commented Apr 19, 2023

Sure, I can, I've already done this:

public class CustomContextTest: ContextTest
{
    public override BrowserNewContextOptions ContextOptions() =>
        new()
            {
                BaseURL = ContextHelper.TestSettings.Host
            };
}

And have two other classes inheriting from it for my equivalent of a PageTest and a MultiPageTest class I've made.

That being said, I don't think it should be necessary. I think setting a BaseURL for automation testing in a .runsettings file or an env var is just as important as indicating whether it's meant to be run headlessly.

Playwright already supports this, I think it's just a case of adding it to the NUnit setup in the same way as we look for HEADED and DEBUG. If this is something you'd consider, I don't mind raising a PR for it.

@hughesjs
Copy link
Author

Arguably, anything in BrowserNewContextOptions should be settable

@mxschmitt mxschmitt changed the title [Feature] Set BaseUrl from Env Var for PageTest in Playwright.NUnit [Feature] Set ContextOptions via runsettings file Apr 20, 2023
@mxschmitt
Copy link
Member

sounds reasonable! I changed the PR title to reflect this feature request.

@NikkTod
Copy link

NikkTod commented Jun 21, 2023

Hey @mxschmitt, any update on that, it is on the table :)

@Exoow
Copy link
Contributor

Exoow commented Oct 27, 2023

We use MSTest, and I wrote a TestContext extension method to set BaseUrl and such from the runsettings:

BaseURL = TestContext.GetParameter("BaseUrl")

@paule96
Copy link

paule96 commented Mar 5, 2024

@Exoow can you share your TestContext extension in an example? So it's easy to maybe implement the same workaround like you did.

@Exoow
Copy link
Contributor

Exoow commented Mar 5, 2024

See attachment, just rename the file to .cs :)

TestContextExtensions.txt

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

No branches or pull requests

5 participants