Skip to content

Commit

Permalink
feat: Add possibility to load Playwright settings programmatically
Browse files Browse the repository at this point in the history
As an alternative to using a .runsettings XML file.

Fixes #3081
  • Loading branch information
0xced committed Dec 8, 2024
1 parent df59854 commit 595abc1
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 10 deletions.
17 changes: 17 additions & 0 deletions src/Playwright.MSTest/WorkerAwareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,26 @@ public async Task<T> RegisterService<T>(string name, Func<Task<T>> 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);
Expand Down
18 changes: 18 additions & 0 deletions src/Playwright.NUnit/WorkerAwareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ public async Task<T> RegisterService<T>(string name, Func<Task<T>> 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()
{
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/Playwright.TestAdapter/PlaywrightBrowser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Microsoft.Playwright.TestAdapter;

public enum PlaywrightBrowser
{
Chromium,
Firefox,
Webkit,
}
10 changes: 10 additions & 0 deletions src/Playwright.TestAdapter/PlaywrightSettings.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
23 changes: 13 additions & 10 deletions src/Playwright.TestAdapter/PlaywrightSettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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/");
Expand All @@ -61,23 +68,16 @@ 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;
}
}

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;
Expand All @@ -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;
}
18 changes: 18 additions & 0 deletions src/Playwright.Xunit/WorkerAwareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ public async Task<T> RegisterService<T>(string name, Func<Task<T>> 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);
Expand All @@ -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);
Expand Down

0 comments on commit 595abc1

Please sign in to comment.