playwright-dotnet
playwright-dotnet copied to clipboard
[Feature] Set ContextOptions via runsettings file
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?
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,
};
}
}
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.
Arguably, anything in BrowserNewContextOptions should be settable
sounds reasonable! I changed the PR title to reflect this feature request.
Hey @mxschmitt, any update on that, it is on the table :)
We use MSTest, and I wrote a TestContext extension method to set BaseUrl and such from the runsettings:
BaseURL = TestContext.GetParameter("BaseUrl")
@Exoow can you share your TestContext extension in an example? So it's easy to maybe implement the same workaround like you did.