diff --git a/src/Playwright.MSTest/WorkerAwareTest.cs b/src/Playwright.MSTest/WorkerAwareTest.cs index 0bea2243c..53482ada2 100644 --- a/src/Playwright.MSTest/WorkerAwareTest.cs +++ b/src/Playwright.MSTest/WorkerAwareTest.cs @@ -34,9 +34,26 @@ public async Task RegisterService(string name, Func> factory) wher return (_currentWorker.Services[name] as T)!; } + protected virtual PlaywrightSettings? PlaywrightSettings() + { + return null; + } + + private void LoadPlaywrightSettings() + { + var settings = PlaywrightSettings(); + if (settings != null) + { + PlaywrightSettingsProvider.Load(settings); + } + } + [TestInitialize] public void WorkerSetup() { + // Must be run before accessing any static PlaywrightSettingsProvider.* properties + LoadPlaywrightSettings(); + if (PlaywrightSettingsProvider.ExpectTimeout.HasValue) { AssertionsBase.SetDefaultTimeout(PlaywrightSettingsProvider.ExpectTimeout.Value); diff --git a/src/Playwright.NUnit/WorkerAwareTest.cs b/src/Playwright.NUnit/WorkerAwareTest.cs index 0cee929b9..5bef9988b 100644 --- a/src/Playwright.NUnit/WorkerAwareTest.cs +++ b/src/Playwright.NUnit/WorkerAwareTest.cs @@ -58,6 +58,20 @@ public async Task RegisterService(string name, Func> factory) wher return (_currentWorker.Services[name] as T)!; } + protected virtual PlaywrightSettings? PlaywrightSettings() + { + return null; + } + + private void LoadPlaywrightSettings() + { + var settings = PlaywrightSettings(); + if (settings != null) + { + PlaywrightSettingsProvider.Load(settings); + } + } + [SetUp] public void WorkerSetup() { @@ -66,6 +80,10 @@ public void WorkerSetup() _currentWorker = new(); } WorkerIndex = _currentWorker.WorkerIndex; + + // Must be run before accessing any static PlaywrightSettingsProvider.* properties + LoadPlaywrightSettings(); + if (PlaywrightSettingsProvider.ExpectTimeout.HasValue) { AssertionsBase.SetDefaultTimeout(PlaywrightSettingsProvider.ExpectTimeout.Value); diff --git a/src/Playwright.TestAdapter/PlaywrightBrowser.cs b/src/Playwright.TestAdapter/PlaywrightBrowser.cs new file mode 100644 index 000000000..744402b4b --- /dev/null +++ b/src/Playwright.TestAdapter/PlaywrightBrowser.cs @@ -0,0 +1,8 @@ +namespace Microsoft.Playwright.TestAdapter; + +public enum PlaywrightBrowser +{ + Chromium, + Firefox, + Webkit, +} diff --git a/src/Playwright.TestAdapter/PlaywrightSettings.cs b/src/Playwright.TestAdapter/PlaywrightSettings.cs new file mode 100644 index 000000000..84e091b12 --- /dev/null +++ b/src/Playwright.TestAdapter/PlaywrightSettings.cs @@ -0,0 +1,10 @@ +using System; + +namespace Microsoft.Playwright.TestAdapter; + +public class PlaywrightSettings +{ + public PlaywrightBrowser Browser { get; set; } = PlaywrightBrowser.Chromium; + public BrowserTypeLaunchOptions? LaunchOptions { get; set; } + public TimeSpan? ExpectTimeout { get; set; } +} diff --git a/src/Playwright.TestAdapter/PlaywrightSettingsProvider.cs b/src/Playwright.TestAdapter/PlaywrightSettingsProvider.cs index 0390535d7..d802a0769 100644 --- a/src/Playwright.TestAdapter/PlaywrightSettingsProvider.cs +++ b/src/Playwright.TestAdapter/PlaywrightSettingsProvider.cs @@ -34,11 +34,18 @@ namespace Microsoft.Playwright.TestAdapter; public class PlaywrightSettingsProvider : ISettingsProvider { private static PlaywrightSettingsXml? _settings = null!; + private static PlaywrightSettings? _playwrightSettings; public static string BrowserName { get { + if (_playwrightSettings != null) + { + var browser = _playwrightSettings.Browser.ToString().ToLowerInvariant(); + ValidateBrowserName(browser, "PlaywrightSettings.Browser property", string.Empty); + return browser; + } var browserFromEnv = Environment.GetEnvironmentVariable("BROWSER")?.ToLowerInvariant(); // GitHub Codespaces sets the BROWSER environment variable, ignore it if its bogus. var ignoreValueFromEnv = Environment.GetEnvironmentVariable("CODESPACES") == "true" && browserFromEnv!.StartsWith("/vscode/"); @@ -61,15 +68,8 @@ public static float? ExpectTimeout { get { - if (_settings == null) - { - return null; - } - if (_settings.ExpectTimeout.HasValue) - { - return _settings.ExpectTimeout.Value; - } - return null; + var expectTimeout = _playwrightSettings?.ExpectTimeout?.TotalMilliseconds; + return expectTimeout.HasValue ? Convert.ToSingle(expectTimeout.Value) : _settings?.ExpectTimeout; } } @@ -77,7 +77,7 @@ public static BrowserTypeLaunchOptions LaunchOptions { get { - var launchOptions = _settings?.LaunchOptions ?? new BrowserTypeLaunchOptions(); + var launchOptions = _playwrightSettings?.LaunchOptions ?? _settings?.LaunchOptions ?? new BrowserTypeLaunchOptions(); if (Environment.GetEnvironmentVariable("HEADED") == "1") { launchOptions.Headless = false; @@ -104,4 +104,7 @@ private static void ValidateBrowserName(string browserName, string fromText, str public void Load(XmlReader reader) => _settings = new PlaywrightSettingsXml(reader); + + public static void Load(PlaywrightSettings settings) + => _playwrightSettings = settings; } diff --git a/src/Playwright.Xunit/WorkerAwareTest.cs b/src/Playwright.Xunit/WorkerAwareTest.cs index 1cce6b65e..7a074d8b4 100644 --- a/src/Playwright.Xunit/WorkerAwareTest.cs +++ b/src/Playwright.Xunit/WorkerAwareTest.cs @@ -57,6 +57,20 @@ public async Task RegisterService(string name, Func> factory) wher return (_currentWorker.Services[name] as T)!; } + protected virtual PlaywrightSettings? PlaywrightSettings() + { + return null; + } + + private void LoadPlaywrightSettings() + { + var settings = PlaywrightSettings(); + if (settings != null) + { + PlaywrightSettingsProvider.Load(settings); + } + } + async public override Task InitializeAsync() { await base.InitializeAsync().ConfigureAwait(false); @@ -65,6 +79,10 @@ async public override Task InitializeAsync() _currentWorker = new(); } WorkerIndex = _currentWorker.WorkerIndex; + + // Must be run before accessing any static PlaywrightSettingsProvider.* properties + LoadPlaywrightSettings(); + if (PlaywrightSettingsProvider.ExpectTimeout.HasValue) { AssertionsBase.SetDefaultTimeout(PlaywrightSettingsProvider.ExpectTimeout.Value);