From 9c8fd9e498be3bbc546189cfe165d64e08f7df89 Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Sun, 16 Jun 2024 21:26:15 +0200 Subject: [PATCH] Code style refactor (#11) --- src/Tests/AliasVault.E2ETests/AliasTests.cs | 10 +++------- src/Tests/AliasVault.E2ETests/AuthTests.cs | 4 ++-- .../Common/PlaywrightTest.cs | 16 +++++++++++++--- .../Common/TestDefaults.cs | 19 +++++++++++++++++++ 4 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/Tests/AliasVault.E2ETests/Common/TestDefaults.cs diff --git a/src/Tests/AliasVault.E2ETests/AliasTests.cs b/src/Tests/AliasVault.E2ETests/AliasTests.cs index 233ea7439..a911a953c 100644 --- a/src/Tests/AliasVault.E2ETests/AliasTests.cs +++ b/src/Tests/AliasVault.E2ETests/AliasTests.cs @@ -56,7 +56,7 @@ public class AliasTests : PlaywrightTest public async Task AliasListingCorrect() { await Page.GotoAsync(AppBaseUrl + "aliases"); - await Page.WaitForURLAsync("**/aliases", new PageWaitForURLOptions() { Timeout = 2000 }); + await WaitForURLAsync("**/aliases"); // Wait for the content to load. await Page.WaitForSelectorAsync("text=AliasVault"); @@ -74,7 +74,7 @@ public class AliasTests : PlaywrightTest public async Task CreateAlias() { await Page.GotoAsync(AppBaseUrl + "add-alias"); - await Page.WaitForURLAsync("**/add-alias", new PageWaitForURLOptions() { Timeout = 2000 }); + await WaitForURLAsync("**/add-alias"); // Wait for the content to load. await Page.WaitForSelectorAsync("text=AliasVault"); @@ -89,11 +89,7 @@ public class AliasTests : PlaywrightTest // Press submit button with text "Create Alias" var submitButton = Page.Locator("text=Save Alias").First; await submitButton.ClickAsync(); - await Page.WaitForURLAsync("**/alias/**", new PageWaitForURLOptions() { Timeout = 2000 }); - - // Check if the redirection occurred - var currentUrl = Page.Url; - Assert.That(currentUrl, Does.Contain(AppBaseUrl + "alias/")); + await WaitForURLAsync("**/alias/**"); // Wait for the content to load. await Page.WaitForSelectorAsync("text=Login credentials"); diff --git a/src/Tests/AliasVault.E2ETests/AuthTests.cs b/src/Tests/AliasVault.E2ETests/AuthTests.cs index 2957a2c5f..240d8c3de 100644 --- a/src/Tests/AliasVault.E2ETests/AuthTests.cs +++ b/src/Tests/AliasVault.E2ETests/AuthTests.cs @@ -46,7 +46,7 @@ public class AuthTests : PlaywrightTest // Check that we are on the login page after navigating to the base URL. // We are expecting to not be authenticated and thus to be redirected to the login page. - await Page.WaitForURLAsync("**/user/login", new PageWaitForURLOptions() { Timeout = 2000 }); + await WaitForURLAsync("**/user/login"); // Try to login with test credentials. var emailField = Page.Locator("input[id='email']"); @@ -57,7 +57,7 @@ public class AuthTests : PlaywrightTest // Check if we get redirected when clicking on the login button. var loginButton = Page.Locator("button[type='submit']"); await loginButton.ClickAsync(); - await Page.WaitForURLAsync(AppBaseUrl, new PageWaitForURLOptions() { Timeout = 2000 }); + await WaitForURLAsync(AppBaseUrl); // Check if the login was successful by verifying content. var pageContent = await Page.TextContentAsync("body"); diff --git a/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs b/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs index 781ab7378..37fc2bdb4 100644 --- a/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs +++ b/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs @@ -117,6 +117,16 @@ public class PlaywrightTest _blazorWasmAppManager.StopBlazorWasm(); } + /// + /// Wait for the specified URL to be loaded with a default timeout. + /// + /// The URL to wait for. This may also contains wildcard such as "**/user/login". + /// Async task. + protected async Task WaitForURLAsync(string url) + { + await Page.WaitForURLAsync(url, new PageWaitForURLOptions() { Timeout = TestDefaults.DefaultTimeout }); + } + /// /// Register a new random account. /// @@ -129,12 +139,12 @@ public class PlaywrightTest // Check that we get redirected to /user/login when accessing the root URL and not authenticated. await Page.GotoAsync(AppBaseUrl); - await Page.WaitForURLAsync("**/user/login", new PageWaitForURLOptions() { Timeout = 2000 }); + await WaitForURLAsync("**/user/login"); // Try to register a new account. var registerButton = Page.Locator("a[href='/user/register']"); await registerButton.ClickAsync(); - await Page.WaitForURLAsync("**/user/register", new PageWaitForURLOptions() { Timeout = 2000 }); + await WaitForURLAsync("**/user/register"); // Try to login with test credentials. var emailField = Page.Locator("input[id='email']"); @@ -153,6 +163,6 @@ public class PlaywrightTest await submitButton.ClickAsync(); // Check if we get redirected to the root URL after registration which means we are logged in. - await Page.WaitForURLAsync(AppBaseUrl, new PageWaitForURLOptions() { Timeout = 5000 }); + await WaitForURLAsync(AppBaseUrl); } } diff --git a/src/Tests/AliasVault.E2ETests/Common/TestDefaults.cs b/src/Tests/AliasVault.E2ETests/Common/TestDefaults.cs new file mode 100644 index 000000000..5bf285373 --- /dev/null +++ b/src/Tests/AliasVault.E2ETests/Common/TestDefaults.cs @@ -0,0 +1,19 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) lanedirt. All rights reserved. +// Licensed under the MIT license. See LICENSE.md file in the project root for full license information. +// +//----------------------------------------------------------------------- + +namespace AliasVault.E2ETests.Common; + +/// +/// Default values for tests. +/// +public static class TestDefaults +{ + /// + /// Gets or sets default timeout while waiting for pages to load in milliseconds. + /// + public static int DefaultTimeout { get; set; } = 5000; +}