Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
felixcicatt committed Dec 19, 2024
1 parent 483a335 commit 92fa89b
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 241 deletions.
102 changes: 66 additions & 36 deletions Kiss.Bff.EndToEndTest/Infrastructure/BaseTestInitializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Configuration;
using System.Collections.Concurrent;
using Microsoft.Extensions.Configuration;


namespace Kiss.Bff.EndToEndTest
Expand All @@ -14,11 +15,16 @@ 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()
{
Console.WriteLine("!!Scenario: " + TestContext.TestName);
Console.WriteLine("Scenario: " + TestContext.TestName);

var username = GetRequiredConfig("TestSettings:TEST_USERNAME");
var password = GetRequiredConfig("TestSettings:TEST_PASSWORD");

Expand All @@ -30,56 +36,70 @@ await Context.Tracing.StartAsync(new()
Sources = true,
});

await Step("Given user is logged in to KISS DEV environment");

var loginHelper = new AzureAdLoginHelper(Page, username, password, s_uniqueOtpHelper);
await loginHelper.LoginAsync();
await Context.StorageStateAsync(new() { Path = StoragePath });

await Context.Tracing.GroupEndAsync();
}

[TestCleanup]
public async Task TestCleanup()
protected async Task Step(string description)
{
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();
await Context.Tracing.GroupAsync(description);
Console.WriteLine(description);
_steps.Add(description);
}

public override BrowserNewContextOptions ContextOptions()
[TestCleanup]
public async Task TestCleanup()
{
return new(base.ContextOptions())
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()
{
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,
};
}
Path = fullPath
});

protected static void Given(string given)
{
Console.WriteLine("!!Given " + given);
}
var html = $"""
<p>Scenario: {TestContext.TestName}</p>
<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 => $"""
protected static void When(string given)
{
Console.WriteLine("!!When " + given);
}
<li>{step}</li>
"""))}
</ol>
""";

protected static void Then(string given)
{
Console.WriteLine("!!Then " + given);
s_testsHtml.TryAdd(TestContext.TestName!, html);
}

protected static void And(string given)
[ClassCleanup(InheritanceBehavior.BeforeEachDerivedClass)]
public static async Task ClassCleanup()
{
Console.WriteLine("!!And " + given);
var html = $"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Playwright traces</title>
</head>
<body>
<ol>{string.Join("", s_testsHtml.OrderBy(x=> x.Key).Select(x=> $"""
<li>{x.Value}</li>
"""))}
</ol>
</body>
""";

using var writer = File.CreateText(Path.Combine(Environment.CurrentDirectory, "playwright-traces", "index.html"));
await writer.WriteLineAsync(html);
}

private static string GetRequiredConfig(string key)
Expand All @@ -91,5 +111,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,
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public static async Task<IAsyncDisposable> CreateBerichten(this IPage page, IEnu
var berichten = new List<Bericht>();
async ValueTask Dispose()
{
if (berichten.Count == 0) return;
await page.Context.Tracing.GroupAsync("Cleanup");
foreach (var item in berichten)
{
try
Expand All @@ -19,6 +21,7 @@ async ValueTask Dispose()
{
}
}
await page.Context.Tracing.GroupEndAsync();
}
try
{
Expand Down
Loading

0 comments on commit 92fa89b

Please sign in to comment.