Skip to content

Commit

Permalink
add some basic login page tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Jun 30, 2023
1 parent 3d40f41 commit 660a6b1
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
53 changes: 53 additions & 0 deletions backend/Testing/Browser/Base/PageTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Microsoft.Playwright;
using Microsoft.Playwright.TestAdapter;

namespace Testing.Browser.Base;

public class PageTest : IAsyncLifetime
{
private readonly PlaywrightFixture _fixture;
public IPage Page => _fixture.Page;
public IBrowser Browser => _fixture.Browser;
public IBrowserContext Context => _fixture.Context;

public PageTest()
{
_fixture = new PlaywrightFixture();
}

public ILocatorAssertions Expect(ILocator locator) => Assertions.Expect(locator);
public IPageAssertions Expect(IPage page) => Assertions.Expect(page);
public IAPIResponseAssertions Expect(IAPIResponse response) => Assertions.Expect(response);

public async Task InitializeAsync()
{
await _fixture.InitializeAsync();
}

public async Task DisposeAsync()
{
await _fixture.DisposeAsync();
}
}

public class PlaywrightFixture : IAsyncLifetime
{
public async Task InitializeAsync()
{
PlaywrightInstance = await Playwright.CreateAsync();
Browser = await PlaywrightInstance.Chromium.LaunchAsync();
Context = await Browser.NewContextAsync(new BrowserNewContextOptions());
Page = await Context.NewPageAsync();
}

public IPage Page { get; set; }
public IBrowser Browser { get; set; } = null!;
public IBrowserContext Context { get; set; } = null!;
private IPlaywright PlaywrightInstance { get; set; } = null!;

public async Task DisposeAsync()
{
await Browser.DisposeAsync();
PlaywrightInstance.Dispose();
}
}
52 changes: 52 additions & 0 deletions backend/Testing/Browser/LoginPageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Microsoft.Playwright;
using Testing.Browser.Base;

namespace Testing.Browser;

public class LoginPageTests: PageTest
{
[Fact]
public async Task CanLoginClicking()
{
await Page.GotoAsync("https://staging.languagedepot.org/login");

await Page.GetByLabel("Email (or Send/Receive username)").ClickAsync();

await Page.GetByLabel("Email (or Send/Receive username)").FillAsync("KindLion");

await Page.GetByLabel("Password").ClickAsync();

await Page.GetByLabel("Password").FillAsync("pass");

await Page.GetByRole(AriaRole.Button, new() { Name = "Log in" }).ClickAsync();

await Expect(Page).ToHaveURLAsync("https://staging.languagedepot.org/admin");
}

[Fact]
public async Task CanLoginKeyboardNavigation()
{
await Page.GotoAsync("https://staging.languagedepot.org/login");

await Page.GetByLabel("Email (or Send/Receive username)").FillAsync("KindLion");

await Page.GetByLabel("Email (or Send/Receive username)").PressAsync("Tab");

await Page.GetByLabel("Password").FillAsync("pass");

await Page.GetByLabel("Password").PressAsync("Enter");

await Expect(Page).ToHaveURLAsync("https://staging.languagedepot.org/admin");
}

[Fact]
public async Task ShowErrorWithoutUsername()
{
await Page.GotoAsync("https://staging.languagedepot.org/login");

await Page.GetByRole(AriaRole.Button, new() { Name = "Log in" }).ClickAsync();

await Expect(Page).ToHaveURLAsync("https://staging.languagedepot.org/login");
await Expect(Page.GetByText("User info missing")).ToBeVisibleAsync();
}
}
2 changes: 2 additions & 0 deletions backend/Testing/Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Microsoft.Playwright" Version="1.35.0" />
<PackageReference Include="Microsoft.Playwright.TestAdapter" Version="1.35.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="Shouldly" Version="4.1.0" />
<PackageReference Include="SIL.Chorus.LibChorus" Version="5.1.0-beta*" />
Expand Down

0 comments on commit 660a6b1

Please sign in to comment.