forked from Nfactor26/pixel-identity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add automation test cases using Playwright for login, register and lo…
…gout pages
- Loading branch information
Showing
16 changed files
with
495 additions
and
11 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
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
12 changes: 12 additions & 0 deletions
12
src/Pixel.Identity.UI.Tests/Helpers/ConfigurationFactory.cs
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,12 @@ | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Pixel.Identity.UI.Tests.Helpers; | ||
|
||
internal class ConfigurationFactory | ||
{ | ||
private static IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json") | ||
.AddEnvironmentVariables().Build(); | ||
|
||
public static IConfiguration Create() => configuration; | ||
|
||
} |
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,35 @@ | ||
using System.Collections.Generic; | ||
using System.Security.Claims; | ||
|
||
namespace Pixel.Identity.UI.Tests.Helpers; | ||
|
||
internal static class UserCollection | ||
{ | ||
private static readonly List<User> users = new(); | ||
|
||
static UserCollection() | ||
{ | ||
users.Add(new User("[email protected]", "tesT-useR-secreT-1")); | ||
users.Add(new User("[email protected]", "tesT-useR-secreT-2")); | ||
} | ||
|
||
public static IEnumerable<User> GetAllUsers() => users; | ||
} | ||
|
||
internal class User | ||
{ | ||
public string Email { get; set; } | ||
|
||
public string Password { get; set; } | ||
|
||
public List<string> Roles { get; private set; } = new (); | ||
|
||
public List<Claim> Claims { get; private set; } = new (); | ||
|
||
public User(string email, string password) | ||
{ | ||
this.Email = email; | ||
this.Password = password; | ||
} | ||
} | ||
|
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,71 @@ | ||
using Microsoft.Playwright; | ||
using System.Threading.Tasks; | ||
|
||
namespace Pixel.Identity.UI.Tests.PageModels; | ||
|
||
internal class LoginPage | ||
{ | ||
private readonly IPage page; | ||
|
||
/// <summary> | ||
/// constructor | ||
/// </summary> | ||
/// <param name="page"></param> | ||
public LoginPage(IPage page) | ||
{ | ||
this.page = page; | ||
} | ||
|
||
/// <summary> | ||
/// Navigate to login page | ||
/// </summary> | ||
/// <param name="baseUrl"></param> | ||
/// <returns></returns> | ||
public async Task GoToAsync(string baseUrl) | ||
{ | ||
await page.GotoAsync($"{baseUrl}/Identity/Account/Login"); | ||
} | ||
|
||
/// <summary> | ||
/// Perform login action on page. Make sure to call AuthorizeRequestedScopesAsync() | ||
/// </summary> | ||
/// <param name="userEmail">UserName for account</param> | ||
/// <param name="password">Password for account</param> | ||
/// <param name="rememberMe">Indicates whether the remember me checkbox should be checked or not</param> | ||
/// <returns></returns> | ||
public async Task LoginAsync(string userEmail, string password, bool rememberMe) | ||
{ | ||
await this.page.FillAsync("#Input_Email",userEmail); | ||
await this.page.FillAsync("#Input_Password", password); | ||
if(rememberMe) | ||
{ | ||
await this.page.CheckAsync("#Input_RememberMe"); | ||
} | ||
//await page.RunAndWaitForNavigationAsync(async () => | ||
//{ | ||
// await this.page.ClickAsync("#login-submit"); | ||
//}); | ||
await this.page.ClickAsync("#login-submit"); | ||
} | ||
|
||
/// <summary> | ||
/// Click on forgot password link | ||
/// </summary> | ||
/// <returns></returns> | ||
public async Task ClickForgotPasswordAsync() | ||
{ | ||
var forgotPassword = this.page.Locator("#forgot-password"); | ||
await forgotPassword.ClickAsync(); | ||
} | ||
|
||
/// <summary> | ||
/// Click on resend email confirmation link | ||
/// </summary> | ||
/// <returns></returns> | ||
public async Task ClickResendEmailConfirmationAsync() | ||
{ | ||
var resentConfirmation = this.page.Locator("#resend-confirmation"); | ||
await resentConfirmation.ClickAsync(); | ||
} | ||
|
||
} |
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,32 @@ | ||
using Microsoft.Playwright; | ||
using System.Threading.Tasks; | ||
|
||
namespace Pixel.Identity.UI.Tests.PageModels; | ||
|
||
internal class LogoutPage | ||
{ | ||
private readonly IPage page; | ||
|
||
/// <summary> | ||
/// constructor | ||
/// </summary> | ||
/// <param name="page"></param> | ||
public LogoutPage(IPage page) | ||
{ | ||
this.page = page; | ||
} | ||
|
||
/// <summary> | ||
/// Logout using the sign out menu item | ||
/// </summary> | ||
/// <returns></returns> | ||
public async Task LogoutAsync() | ||
{ | ||
await this.page.ClickAsync("#signedInMenu"); | ||
await this.page.ClickAsync("#signOutMenuItem"); | ||
await page.RunAndWaitForNavigationAsync(async () => | ||
{ | ||
await this.page.ClickAsync("#confirmLogoutButton"); | ||
}); | ||
} | ||
} |
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,42 @@ | ||
using Microsoft.Playwright; | ||
using System.Threading.Tasks; | ||
|
||
namespace Pixel.Identity.UI.Tests.PageModels | ||
{ | ||
internal class RegisterPage | ||
{ | ||
private readonly IPage page; | ||
|
||
/// <summary> | ||
/// constructor | ||
/// </summary> | ||
/// <param name="page"></param> | ||
public RegisterPage(IPage page) | ||
{ | ||
this.page = page; | ||
} | ||
|
||
/// <summary> | ||
/// Click the register button to navigate to registration page | ||
/// </summary> | ||
/// <returns></returns> | ||
public async Task GoToAsync() | ||
{ | ||
await page.ClickAsync("#registerPageLink"); | ||
} | ||
|
||
/// <summary> | ||
/// Fill out required details and click the register button to complete registration | ||
/// </summary> | ||
/// <param name="userEmail"></param> | ||
/// <param name="password"></param> | ||
/// <returns></returns> | ||
public async Task RegisterAsync(string userEmail, string password) | ||
{ | ||
await page.FillAsync("#Input_Email", userEmail); | ||
await page.FillAsync("#Input_Password", password); | ||
await page.FillAsync("#Input_ConfirmPassword", password); | ||
await page.ClickAsync("#registerSubmit"); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Pixel.Identity.UI.Tests/Pixel.Identity.UI.Tests.csproj
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,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> | ||
<PackageReference Include="Microsoft.Playwright" Version="1.20.2" /> | ||
<PackageReference Include="Microsoft.Playwright.NUnit" Version="1.20.2" /> | ||
<PackageReference Include="NUnit" Version="3.13.2" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" /> | ||
<PackageReference Include="coverlet.collector" Version="3.1.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="appsettings.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
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,63 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Playwright.NUnit; | ||
using NUnit.Framework; | ||
using Pixel.Identity.UI.Tests.Helpers; | ||
using Pixel.Identity.UI.Tests.PageModels; | ||
using System.Threading.Tasks; | ||
|
||
namespace Pixel.Identity.UI.Tests.Tests; | ||
|
||
public class LoginTests : PageTest | ||
{ | ||
private IConfiguration configuration; | ||
private string baseUrl; | ||
|
||
/// <summary> | ||
/// constructor | ||
/// </summary> | ||
public LoginTests() | ||
{ | ||
configuration = ConfigurationFactory.Create(); | ||
baseUrl = configuration["BaseUrl"]; | ||
} | ||
|
||
/// <summary> | ||
/// Validate that email and password fields are required for sign in | ||
/// </summary> | ||
/// <returns></returns> | ||
[Test, Order(1)] | ||
public async Task Validate_That_UserEmail_And_Credentials_Are_Required() | ||
{ | ||
var loginPage = new LoginPage(this.Page); | ||
await loginPage.GoToAsync(this.baseUrl); | ||
await loginPage.LoginAsync(string.Empty, string.Empty, false); | ||
await Expect(Page.Locator("#Input_Email-error")).ToHaveTextAsync("The Email field is required."); | ||
await Expect(Page.Locator("#Input_Password-error")).ToHaveTextAsync("The Password field is required."); | ||
} | ||
|
||
/// <summary> | ||
/// Validate that user can not login with incorrect credentials | ||
/// </summary> | ||
/// <returns></returns> | ||
[Test, Order(2)] | ||
public async Task Validate_That_User_Can_Not_LogIn_With_Incorrect_Credentials() | ||
{ | ||
var loginPage = new LoginPage(this.Page); | ||
await loginPage.GoToAsync(this.baseUrl); | ||
await loginPage.LoginAsync(this.configuration["UserEmail"], "unknown-secret", false); | ||
await Expect(Page.Locator("#account>div.text-danger")).ToHaveTextAsync("Invalid login attempt."); | ||
} | ||
|
||
/// <summary> | ||
/// validate taht user can login with correct email and password combination | ||
/// </summary> | ||
/// <returns></returns> | ||
[Test, Order(3)] | ||
public async Task Validate_That_User_Can_LogIn_With_Correct_Credentials() | ||
{ | ||
var loginPage = new LoginPage(this.Page); | ||
await loginPage.GoToAsync(baseUrl); | ||
await loginPage.LoginAsync(configuration["UserEmail"], configuration["UserSecret"], false); | ||
await Expect(this.Page.Locator("#signedInMenu")).ToBeVisibleAsync(); | ||
} | ||
} |
Oops, something went wrong.