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

feat: Add possibility to load Playwright settings programmatically in test runners #3082

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading