-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a2b74a
commit 9247d0e
Showing
8 changed files
with
988 additions
and
593 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Kiss.Bff.EndToEndTest.Helpers | ||
{ | ||
public static class AcceptAllDialogsExtension | ||
{ | ||
public static IDisposable AcceptAllDialogs(this IPage page) | ||
{ | ||
page.Dialog += Accept; | ||
return new DoOnDispose(() => page.Dialog -= Accept); | ||
} | ||
|
||
private static async void Accept(object? _, IDialog dialog) => await dialog.AcceptAsync(); | ||
|
||
|
||
private sealed class DoOnDispose(Action action) : IDisposable | ||
{ | ||
public void Dispose() => action(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using System.Collections.Concurrent; | ||
using System.Drawing; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
|
||
|
||
namespace Kiss.Bff.EndToEndTest | ||
|
@@ -14,51 +17,90 @@ public class BaseTestInitializer : PageTest | |
.Build(); | ||
|
||
private static readonly UniqueOtpHelper s_uniqueOtpHelper = new(GetRequiredConfig("TestSettings:TEST_TOTP_SECRET")); | ||
private static readonly ConcurrentDictionary<string, string> s_testsHtml = []; | ||
|
||
|
||
private readonly List<string> _steps = []; | ||
|
||
[TestInitialize] | ||
public virtual async Task TestInitialize() | ||
{ | ||
var username = GetRequiredConfig("TestSettings:TEST_USERNAME"); | ||
var password = GetRequiredConfig("TestSettings:TEST_PASSWORD"); | ||
|
||
var loginHelper = new AzureAdLoginHelper(Page, username, password, s_uniqueOtpHelper); | ||
await loginHelper.LoginAsync(); | ||
await Context.StorageStateAsync(new() { Path = StoragePath }); | ||
|
||
await Context.Tracing.StartAsync(new() | ||
{ | ||
Title = $"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}", | ||
Screenshots = true, | ||
Snapshots = true, | ||
Sources = true | ||
Sources = true, | ||
}); | ||
|
||
var loginHelper = new AzureAdLoginHelper(Page, username, password, s_uniqueOtpHelper); | ||
await loginHelper.LoginAsync(); | ||
await Context.StorageStateAsync(new() { Path = StoragePath }); | ||
Console.WriteLine(TestContext.TestName); | ||
} | ||
|
||
protected async Task Step(string description) | ||
{ | ||
await Context.Tracing.GroupEndAsync(); | ||
await Context.Tracing.GroupAsync(description); | ||
Console.WriteLine(description); | ||
_steps.Add(description); | ||
} | ||
|
||
[TestCleanup] | ||
public async Task TestCleanup() | ||
{ | ||
var options = TestContext.CurrentTestOutcome != UnitTestOutcome.Passed | ||
? new TracingStopOptions | ||
{ | ||
Path = Path.Combine( | ||
Environment.CurrentDirectory, | ||
"playwright-traces", | ||
$"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}.zip" | ||
) | ||
} | ||
: null; | ||
|
||
await Context.Tracing.StopAsync(options); | ||
await Context.Tracing.GroupEndAsync(); | ||
var fileName = $"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}.zip"; | ||
var fullPath = Path.Combine(Environment.CurrentDirectory, "playwright-traces", fileName); | ||
|
||
await Context.Tracing.StopAsync(new() | ||
{ | ||
Path = fullPath | ||
}); | ||
|
||
var html = $""" | ||
<div data-outcome="{TestContext.CurrentTestOutcome}"> | ||
<h2>{TestContext.TestName}</h2> | ||
<a target="_blank" href="https://trace.playwright.dev/?trace=https://klantinteractie-servicesysteem.github.io/KISS-frontend/{fileName}">Playwright tracing</a> | ||
<p>Steps:</p> | ||
<ol>{string.Join("", _steps.Select(step => $""" | ||
<li>{step}</li> | ||
"""))} | ||
</ol> | ||
</div> | ||
"""; | ||
|
||
s_testsHtml.TryAdd(TestContext.TestName!, html); | ||
} | ||
|
||
public override BrowserNewContextOptions ContextOptions() | ||
[ClassCleanup(InheritanceBehavior.BeforeEachDerivedClass)] | ||
public static async Task ClassCleanup() | ||
{ | ||
return new(base.ContextOptions()) | ||
{ | ||
BaseURL = GetRequiredConfig("TestSettings:TEST_BASE_URL"), | ||
// save auth state so we don't need to log in in every single test | ||
StorageStatePath = File.Exists(StoragePath) ? StoragePath : null, | ||
}; | ||
var html = $$""" | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src https://unpkg.com/[email protected]/simple.min.css 'sha256-kZS4Ytg58npMuRkQ/J+zM+WLW6n1befhcf5YgPVHdEE=';"> | ||
<title>Playwright traces</title> | ||
<link rel="stylesheet" href="https://unpkg.com/[email protected]/simple.min.css" integrity="sha384-Cxvt41nwdtHMOjpCqr+FaCybNL58LeIc0vPSLR4KlFSCBrHTb095iJbbw+hDTklQ" crossorigin="anonymous"> | ||
<style>[data-outcome=Failed]{color: var(--code)}</style> | ||
</head> | ||
<body> | ||
<main> | ||
{{string.Join("", s_testsHtml.OrderBy(x=> x.Key).Select(x=> x.Value))}} | ||
</main> | ||
</body> | ||
"""; | ||
|
||
using var writer = File.CreateText(Path.Combine(Environment.CurrentDirectory, "playwright-traces", "index.html")); | ||
await writer.WriteLineAsync(html); | ||
} | ||
|
||
private static string GetRequiredConfig(string key) | ||
|
@@ -70,5 +112,15 @@ private static string GetRequiredConfig(string key) | |
} | ||
return value; | ||
} | ||
|
||
public override BrowserNewContextOptions ContextOptions() | ||
{ | ||
return new(base.ContextOptions()) | ||
{ | ||
BaseURL = GetRequiredConfig("TestSettings:TEST_BASE_URL"), | ||
// save auth state so we don't need to log in in every single test | ||
StorageStatePath = File.Exists(StoragePath) ? StoragePath : null, | ||
}; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.