Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Make E2E test more robust (#83)

Leendert de Borst8c93fceb3ef7
diff --git a/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs b/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs
index f06f615f0..52417af2e 100644
--- a/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs
+++ b/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs
@@ -52,17 +52,17 @@ public class PlaywrightTest
/// <summary>
/// Gets the Playwright browser instance.
/// </summary>
- protected IBrowser Browser { get; private set; }
+ protected IBrowser Browser { get; private set; } = null!;
/// <summary>
/// Gets the Playwright browser context.
/// </summary>
- protected IBrowserContext Context { get; private set; }
+ protected IBrowserContext Context { get; private set; } = null!;
/// <summary>
/// Gets the Playwright page.
/// </summary>
- protected IPage Page { get; private set; }
+ protected IPage Page { get; private set; } = null!;
/// <summary>
/// Gets the input helper for Playwright tests.
@@ -76,65 +76,30 @@ public class PlaywrightTest
[OneTimeSetUp]
public async Task OneTimeSetUp()
{
- // Set the base port for the test starting at 5600. Increase the port by 2 for each test running
- // in parallel to avoid port conflicts.
- var apiPort = 0;
- var appPort = 0;
- lock (_lock)
- {
- apiPort = Interlocked.Increment(ref _currentPort);
- appPort = Interlocked.Increment(ref _currentPort);
- }
-
- AppBaseUrl = "http://localhost:" + appPort + "/";
-
- // Start WebAPI in-memory.
- _apiFactory.HostUrl = "http://localhost:" + apiPort;
- _apiFactory.CreateDefaultClient();
-
- // Start Blazor WASM app out-of-process.
- _wasmFactory.HostUrl = "http://localhost:" + appPort;
- _wasmFactory.CreateDefaultClient();
-
- // Set Playwright headless mode true if not in debug mode.
- bool isDebugMode = System.Diagnostics.Debugger.IsAttached;
- bool headless = !isDebugMode;
-
- var playwright = await Playwright.CreateAsync();
- Browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = headless });
- Context = await Browser.NewContextAsync();
+ const int maxRetries = 10;
+ int currentRetry = 0;
- // Intercept Blazor WASM app requests to override appsettings.json
- await Context.RouteAsync("**/appsettings.json", async route =>
- {
- var response = new
- {
- ApiUrl = "http://localhost:" + apiPort,
- };
- await route.FulfillAsync(new RouteFulfillOptions
- {
- ContentType = "application/json",
- Body = System.Text.Json.JsonSerializer.Serialize(response),
- });
- });
- await Context.RouteAsync("**/appsettings.Development.json", async route =>
+ while (currentRetry < maxRetries)
{
- var response = new
+ try
{
- ApiUrl = "http://localhost:" + apiPort,
- };
- await route.FulfillAsync(new RouteFulfillOptions
+ await SetupEnvironment();
+ await Register();
+ return;
+ }
+ catch (Exception ex)
{
- ContentType = "application/json",
- Body = System.Text.Json.JsonSerializer.Serialize(response),
- });
- });
-
- Page = await Context.NewPageAsync();
- InputHelper = new(Page);
-
- // Register a new account via the UI
- await Register();
+ currentRetry++;
+ Console.WriteLine($"Attempt {currentRetry} failed: {ex.Message}");
+ if (currentRetry >= maxRetries)
+ {
+ Console.WriteLine($"All {maxRetries} attempts failed. Last exception: {ex}");
+ throw;
+ }
+
+ await Task.Delay(500);
+ }
+ }
}
/// <summary>
@@ -296,4 +261,74 @@ public class PlaywrightTest
// Check if we get redirected to the root URL after registration which means we are logged in.
await WaitForURLAsync(AppBaseUrl);
}
+
+ private async Task SetupEnvironment()
+ {
+ // Set the base port for the test starting at 5600. Increase the port by 2 for each test running
+ // in parallel to avoid port conflicts.
+ var apiPort = 0;
+ var appPort = 0;
+ lock (_lock)
+ {
+ apiPort = Interlocked.Increment(ref _currentPort);
+ appPort = Interlocked.Increment(ref _currentPort);
+ }
+
+ AppBaseUrl = "http://localhost:" + appPort + "/";
+
+ // Start WebAPI in-memory.
+ _apiFactory.HostUrl = "http://localhost:" + apiPort;
+ var apiClient = _apiFactory.CreateDefaultClient();
+
+ // Start Blazor WASM app out-of-process.
+ _wasmFactory.HostUrl = "http://localhost:" + appPort;
+ var wasmClient = _wasmFactory.CreateDefaultClient();
+
+ // Set Playwright headless mode true if not in debug mode.
+ bool isDebugMode = System.Diagnostics.Debugger.IsAttached;
+ bool headless = !isDebugMode;
+
+ var playwright = await Playwright.CreateAsync();
+ Browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = headless });
+ Context = await Browser.NewContextAsync();
+
+ // Intercept Blazor WASM app requests to override appsettings.json
+ await Context.RouteAsync(
+ "**/appsettings.json",
+ async route =>
+ {
+ var response = new
+ {
+ ApiUrl = "http://localhost:" + apiPort,
+ };
+ await route.FulfillAsync(
+ new RouteFulfillOptions
+ {
+ ContentType = "application/json",
+ Body = System.Text.Json.JsonSerializer.Serialize(response),
+ });
+ });
+ await Context.RouteAsync(
+ "**/appsettings.Development.json",
+ async route =>
+ {
+ var response = new
+ {
+ ApiUrl = "http://localhost:" + apiPort,
+ };
+ await route.FulfillAsync(
+ new RouteFulfillOptions
+ {
+ ContentType = "application/json",
+ Body = System.Text.Json.JsonSerializer.Serialize(response),
+ });
+ });
+
+ Page = await Context.NewPageAsync();
+ InputHelper = new(Page);
+
+ // Check that we get redirected to /user/login when accessing the root URL and not authenticated.
+ await Page.GotoAsync(AppBaseUrl);
+ await WaitForURLAsync("**/user/login");
+ }
}
diff --git a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
index d2d87b8a7..a26c9cf06 100644
--- a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
+++ b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
@@ -112,6 +112,10 @@ public class WebApplicationApiFactoryFixture<TEntryPoint> : WebApplicationFactor
var host = builder.Build();
host.Start();
+ // This delay prevents "ERR_CONNECTION_REFUSED" errors
+ // which happened like 1 out of 10 times when running tests.
+ Thread.Sleep(50);
+
return dummyHost;
}
}
diff --git a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationWasmFactoryFixture.cs b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationWasmFactoryFixture.cs
index b1d71dea8..10f271c29 100644
--- a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationWasmFactoryFixture.cs
+++ b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationWasmFactoryFixture.cs
@@ -39,6 +39,10 @@ public class WebApplicationWasmFactoryFixture<TEntryPoint> : WebApplicationFacto
var host = builder.Build();
host.Start();
+ // This delay prevents "ERR_CONNECTION_REFUSED" errors
+ // which happened like 1 out of 10 times when running tests.
+ Thread.Sleep(50);
+
return dummyHost;
}
}
diff --git a/src/Tests/AliasVault.E2ETests/Tests/CredentialTests.cs b/src/Tests/AliasVault.E2ETests/Tests/CredentialTests.cs
index b1fb3e14c..eac0549f5 100644
--- a/src/Tests/AliasVault.E2ETests/Tests/CredentialTests.cs
+++ b/src/Tests/AliasVault.E2ETests/Tests/CredentialTests.cs
@@ -11,6 +11,7 @@ namespace AliasVault.E2ETests.Tests;
/// End-to-end tests for the credential management.
/// </summary>
[TestFixture]
+[Parallelizable(ParallelScope.Self)]
public class CredentialTests : PlaywrightTest
{
private static readonly Random Random = new();