Skip to content

Commit

Permalink
Code style refactor (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
lanedirt committed Jun 16, 2024
1 parent 50400c5 commit 9c8fd9e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
10 changes: 3 additions & 7 deletions src/Tests/AliasVault.E2ETests/AliasTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static async Task FillAllInputFields(IPage page)
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");
Expand All @@ -74,7 +74,7 @@ public async Task AliasListingCorrect()
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");
Expand All @@ -89,11 +89,7 @@ public async Task CreateAlias()
// 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");
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/AliasVault.E2ETests/AuthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task Login()

// 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']");
Expand All @@ -57,7 +57,7 @@ public async Task Login()
// 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");
Expand Down
16 changes: 13 additions & 3 deletions src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ public async Task OneTimeTearDown()
_blazorWasmAppManager.StopBlazorWasm();
}

/// <summary>
/// Wait for the specified URL to be loaded with a default timeout.
/// </summary>
/// <param name="url">The URL to wait for. This may also contains wildcard such as "**/user/login".</param>
/// <returns>Async task.</returns>
protected async Task WaitForURLAsync(string url)
{
await Page.WaitForURLAsync(url, new PageWaitForURLOptions() { Timeout = TestDefaults.DefaultTimeout });
}

/// <summary>
/// Register a new random account.
/// </summary>
Expand All @@ -129,12 +139,12 @@ private async Task Register()

// 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']");
Expand All @@ -153,6 +163,6 @@ private async Task Register()
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);
}
}
19 changes: 19 additions & 0 deletions src/Tests/AliasVault.E2ETests/Common/TestDefaults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//-----------------------------------------------------------------------
// <copyright file="TestDefaults.cs" company="lanedirt">
// Copyright (c) lanedirt. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
// </copyright>
//-----------------------------------------------------------------------

namespace AliasVault.E2ETests.Common;

/// <summary>
/// Default values for tests.
/// </summary>
public static class TestDefaults
{
/// <summary>
/// Gets or sets default timeout while waiting for pages to load in milliseconds.
/// </summary>
public static int DefaultTimeout { get; set; } = 5000;
}

0 comments on commit 9c8fd9e

Please sign in to comment.