Last active
September 6, 2022 06:55
-
-
Save Chandankkrr/7aabf4803ab1093cfa1de33a726776f8 to your computer and use it in GitHub Desktop.
Blazor web application E2E Testing using Playwright
This file contains hidden or 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
using System; | |
using System.Threading.Tasks; | |
using FluentAssertions; | |
using Microsoft.Playwright; | |
using Xunit; | |
using Xunit.Abstractions; | |
using Xunit.Sdk; | |
namespace Asp.BlazorClient.Tests.End2End | |
{ | |
public class UserRegistrationTest | |
{ | |
private const string BaseUrl = "https://victorious-tree-06261d500.azurestaticapps.net/"; | |
[Theory] | |
[InlineData("[email protected]", "Test1234#")] | |
public async Task CanRegisterNewUserAndLoginSuccessfully(string email, string password) | |
{ | |
using var playwright = await Playwright.CreateAsync(); | |
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions | |
{ | |
Headless = false | |
}); | |
var context = await browser.NewContextAsync(); | |
// Open new page | |
var page = await context.NewPageAsync(); | |
// Go to https://victorious-tree-06261d500.azurestaticapps.net/register | |
await page.GotoAsync($"{BaseUrl}register"); | |
// Fill input[type="text"] | |
await page.FillAsync("input[type=\"text\"]", email); | |
// Fill input[type="password"] | |
await page.FillAsync("input[type=\"password\"]", password); | |
// Fill text=PasswordRepeat the password >> input[type="password"] | |
await page.FillAsync("text=PasswordRepeat the password >> input[type=\"password\"]", password); | |
// Click button:has-text("Register") | |
await page.RunAndWaitForNavigationAsync( | |
async () => { await page.ClickAsync("button:has-text(\"Register\")"); }, | |
new PageRunAndWaitForNavigationOptions | |
{ | |
UrlString = $"{BaseUrl}login" | |
}); | |
// Fill input[type="text"] | |
await page.FillAsync("input[type=\"text\"]", email); | |
// Fill input[type="password"] | |
await page.FillAsync("input[type=\"password\"]", password); | |
// Click button:has-text("Login") | |
await page.RunAndWaitForNavigationAsync( | |
async () => { await page.ClickAsync("button:has-text(\"Login\")"); }, | |
new PageRunAndWaitForNavigationOptions | |
{ | |
UrlString = BaseUrl | |
}); | |
var welcomeTextContainer = await page.QuerySelectorAsync($"text=Hello {email}"); | |
if (welcomeTextContainer == null) | |
{ | |
await page.ScreenshotAsync( | |
new PageScreenshotOptions | |
{ | |
Path = "screenshot.png" | |
}); | |
// fail test | |
throw new XunitException("Test failed, unable to find welcome text container"); | |
} | |
var welcomeText = await welcomeTextContainer.TextContentAsync(); | |
welcomeText.Should().Be($"Hello {email}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment