-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature playwright tests cleanup (#86)
- Loading branch information
1 parent
a14db9b
commit 79def04
Showing
19 changed files
with
530 additions
and
630 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace TagzApp.WebTest.Fixtures; | ||
|
||
public static class FixtureExtensions | ||
{ | ||
public static IHostBuilder UseUniqueDb(this IHostBuilder builder, Guid id) => | ||
builder.ConfigureAppConfiguration(configuration => | ||
{ | ||
var testConfiguration = new Dictionary<string, string?>() | ||
{ | ||
{"ConnectionStrings:SecurityContextConnection",$"Data Source=TagzApp.Web.{id:N}.db" } | ||
}; | ||
configuration.AddInMemoryCollection(testConfiguration); | ||
}); | ||
|
||
public static async Task CleanUpDbFilesAsync(this Guid id, ILogger? logger = null) | ||
{ | ||
logger ??= Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance; | ||
// The host should have shutdown here so we can delete the test database files | ||
await Task.Delay(50); | ||
var dbFiles = System.IO.Directory.GetFiles(".", $"TagzApp.Web.{id:N}.db*"); | ||
foreach (var dbFile in dbFiles) | ||
{ | ||
try | ||
{ | ||
logger.LogInformation("Removing test database file {File}", dbFile); | ||
System.IO.File.Delete(dbFile); | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.LogWarning("Could not remove test database file {File}: {Reason}", dbFile, e.Message); | ||
} | ||
} | ||
} | ||
|
||
/// <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; | ||
} | ||
|
||
/// <summary> | ||
/// Applies a startup delay based on the configuration parameter TestHostStartDelay. This allows easy adding of a custom delay on build / test servers. | ||
/// </summary> | ||
/// <param name="serviceProvider">The IServiceProvider used to get the IConfiguration</param> | ||
/// <remarks>The default delay if no value is found is 0 and no delay is applied.</remarks> | ||
public static async Task ApplyStartUpDelay(this IServiceProvider serviceProvider) | ||
{ | ||
var config = serviceProvider.GetRequiredService<IConfiguration>(); | ||
if (int.TryParse(config["TestHostStartDelay"] ?? "0", out var delay) && delay != 0) | ||
{ | ||
await Task.Delay(delay); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using C3D.Extensions.Playwright.AspNetCore.Xunit; | ||
|
||
namespace TagzApp.WebTest.Fixtures; | ||
|
||
/// <summary> | ||
/// WebApplicationFactory that wraps the TestHost in a Kestrel server and provides Playwright and HttpClient testing. | ||
/// This also logs output from the Host under test to Xunit. | ||
/// | ||
/// <p> | ||
/// Credit to <a href="https://github.com/CZEMacLeod">https://github.com/CZEMacLeod</a> for writing this. | ||
/// Functionality is now wrapped in the nuget package C3D.Extensions.Playwright.AspNetCore.Xunit | ||
/// </p> | ||
/// </summary> | ||
public class PlaywrightFixture : PlaywrightFixture<Web.Program> | ||
{ | ||
public override string? Environment { get; } = "Development"; | ||
|
||
public PlaywrightFixture(IMessageSink output) : base(output) { } | ||
|
||
private readonly Guid _Uniqueid = Guid.NewGuid(); | ||
|
||
protected override IHost CreateHost(IHostBuilder builder) | ||
{ | ||
//ServicesExtensions.SocialMediaProviders = new List<IConfigureProvider> { new StartStubSocialMediaProvider() }; | ||
builder.AddTestConfiguration(); | ||
builder.UseOnlyStubSocialMediaProvider(); | ||
builder.UseUniqueDb(_Uniqueid); | ||
var host = base.CreateHost(builder); | ||
|
||
return host; | ||
} | ||
|
||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA1816:Dispose methods should call SuppressFinalize", Justification = "Base class calls SuppressFinalize")] | ||
public async override ValueTask DisposeAsync() | ||
{ | ||
await base.DisposeAsync(); | ||
|
||
var logger = this.MessageSink.CreateLogger<PlaywrightFixture>(); | ||
await _Uniqueid.CleanUpDbFilesAsync(logger); | ||
} | ||
|
||
// Temp hack to see if it is a timing issue in github actions | ||
public async override Task InitializeAsync() | ||
{ | ||
await base.InitializeAsync(); | ||
await Services.ApplyStartUpDelay(); | ||
} | ||
} |
Oops, something went wrong.