Skip to content

Use test host configuration to set playwright options. #17

@CZEMacLeod

Description

@CZEMacLeod

Discussed in #3

Originally posted by CZEMacLeod August 18, 2023
From #1 there is a possibility to load configuration for PlaywrightWebApplicationFactory from the host under test.

This would allow many options to be set without overriding the respective property get function:

  • BrowserType
  • LaunchOptions
  • ContextOptions
  • PageOptions

It would need to be decided what the configuration section name should be (at least by default) for these. In the proof of concept, I used.

protected virtual IConfiguration TestConfiguration => Services.GetRequiredService<IConfiguration>().GetSection("Playwright");

To separate settings for the host, from the test factory, it might make sense to load additional setting from an appSettings.json file in the test project, and/or from user secret linked to the test project.

The former can be achieved by code like

  /// <summary>
  /// Add the file provided from the test project to the host app configuration
  /// </summary>
  /// <param name="builder">The IHostBuilder</param>
  /// <param name="fileName">The filename or null (defaults to appsettings.Test.json)</param>
  /// <returns>Returns the IHostBuilder to allow chaining</returns>
  public static IHostBuilder AddTestConfiguration(this IHostBuilder builder, string? fileName = null)
  {
    var testDirectory = System.IO.Directory.GetCurrentDirectory();
    builder.ConfigureAppConfiguration(host => host.AddJsonFile(System.IO.Path.Combine(testDirectory, fileName ?? "appsettings.Test.json"), true));
    return builder;
  }

which could be called from CreateHost and the latter by

    builder.ConfigureAppConfiguration(config => config.AddUserSecrets<PlaywrightFixture>());

again in CreateHost.

Technically, we could do something like

builder.ConfigureAppConfiguration(config => config.AddUserSecrets(this.GetType().Assembly));

Which would work if the factory was subclassed as a fixture in the test project.
There is also a GetTestAssemblies function in the base Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory which could perhaps be used.

So, the questions are:

  • Should this feature be added at all?
  • [?] Should it be enabled by default?
  • What section name should it use (by default)?
  • [?] Should appSettings.json (or similar) be added from the test project by default?
  • Should UserSecrets be added from the test project by default?

If should be possible to add predicate properties to allow overriding the default behaviour fairly easily - like

public virtual bool AddMessageSinkProvider => true;
public IMessageSink MessageSink => output;
protected override ILoggingBuilder ConfigureLogging(ILoggingBuilder builder)
{
if (AddMessageSinkProvider) builder.AddXunit(output);
return base.ConfigureLogging(builder);
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestquestionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions