Skip to content

Commit

Permalink
Add feature to add test settings and apply a custom delay to playwrig…
Browse files Browse the repository at this point in the history
…ht tests
  • Loading branch information
CZEMacLeod committed Aug 14, 2023
1 parent 8ce2502 commit 6781aab
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ jobs:
dotnet-version: 7.0.x
- run: dotnet build
- name: Execute Playwright tests
env:
TestHostStartDelay: 1000
run: dotnet test --no-build
27 changes: 27 additions & 0 deletions src/TagzApp.WebTest/Fixtures/FixtureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,31 @@ public static async Task CleanUpDbFilesAsync(this Guid id, ILogger? logger = nul
}
}
}

/// <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);
}
}
}
7 changes: 2 additions & 5 deletions src/TagzApp.WebTest/Fixtures/PlaywrightFixture.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using C3D.Extensions.Playwright.AspNetCore.Xunit;
using Microsoft.Extensions.DependencyInjection.Extensions;
using TagzApp.Web;
using Xunit.Sdk;

namespace TagzApp.WebTest.Fixtures;

Expand All @@ -25,7 +22,7 @@ public PlaywrightFixture(IMessageSink output) : base(output) { }
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);
Expand All @@ -46,6 +43,6 @@ public async override ValueTask DisposeAsync()
public async override Task InitializeAsync()
{
await base.InitializeAsync();
await Task.Delay(1000);
await Services.ApplyStartUpDelay();
}
}
4 changes: 2 additions & 2 deletions src/TagzApp.WebTest/Tests/ModalWebTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ModalFixture(IMessageSink output) : base(output) { }
protected override IHost CreateHost(IHostBuilder builder)
{
// ServicesExtensions.SocialMediaProviders = new List<IConfigureProvider> { new StartStubSocialMediaProvider() };

builder.AddTestConfiguration();
builder.UseOnlyStubSocialMediaProvider();
builder.UseUniqueDb(_Uniqueid);
return base.CreateHost(builder);
Expand All @@ -26,7 +26,7 @@ protected override IHost CreateHost(IHostBuilder builder)
public async override Task InitializeAsync()
{
await base.InitializeAsync();
await Task.Delay(1000);
await Services.ApplyStartUpDelay();
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA1816:Dispose methods should call SuppressFinalize", Justification = "Base class calls SuppressFinalize")]
Expand Down
3 changes: 3 additions & 0 deletions src/TagzApp.WebTest/appsettings.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"TestHostStartDelay": 0
}

0 comments on commit 6781aab

Please sign in to comment.