using Microsoft.Playwright; namespace PlaywrightTests.Utilities; public static class Auth { private static LocatorAssertionsToContainTextOptions TenSTextTimeout => new() { Timeout = 10_000 }; private static LocatorAssertionsToBeVisibleOptions TenSLocatorAssertTimeout => new() { Timeout = 10_000 }; public static async Task Login(IPage page, AvailableEmployeeForTests user = AvailableEmployeeForTests.Admin) { await page.GotoAsync("http://localhost:8050/auth"); var (username, password) = GetCredentials(user); await page.GetByPlaceholder("Benutzername").ClickAsync(); await page.GetByPlaceholder("Benutzername").FillAsync(username); await page.GetByPlaceholder("Passwort").ClickAsync(); await page.GetByPlaceholder("Passwort").FillAsync(password); await page.GetByRole(AriaRole.Button, new() { Name = "Anmelden" }).ClickAsync(); await Assertions.Expect(page.GetByRole(AriaRole.Heading)).ToContainTextAsync("Hallo, c-entron Administrator!", TenSTextTimeout); } public static async Task LoginWebAccount(IPage page, AvailableWebAccountForTests user = AvailableWebAccountForTests.Playwright) { await page.GotoAsync("http://localhost:8050/auth"); var (username, password) = GetCredentials(user); await page.GetByPlaceholder("Benutzername").ClickAsync(); await page.GetByPlaceholder("Benutzername").FillAsync(username); await page.GetByPlaceholder("Passwort").ClickAsync(); await page.GetByPlaceholder("Passwort").FillAsync(password); await page.GetByRole(AriaRole.Button, new() { Name = "Anmelden" }).ClickAsync(); await Assertions.Expect(page.Locator("a").Filter(new() { HasText = "Receipts" })).ToBeVisibleAsync(TenSLocatorAssertTimeout); await Assertions.Expect(page.Locator("a").Filter(new() { HasText = "Tickets" })).ToBeVisibleAsync(TenSLocatorAssertTimeout); } public static ValueTuple GetCredentials(AvailableEmployeeForTests employee) => employee switch { AvailableEmployeeForTests.Admin => new ValueTuple("admin", "1"), _ => throw new ArgumentOutOfRangeException(nameof(employee), employee, null) }; private static ValueTuple GetCredentials(AvailableWebAccountForTests webAccount) => webAccount switch { AvailableWebAccountForTests.Playwright => new ValueTuple("Playwright", "playwright"), AvailableWebAccountForTests.Alpha => new ValueTuple("alphauser", "alphapassword"), AvailableWebAccountForTests.Beta => new ValueTuple("betauser", "betapassword"), AvailableWebAccountForTests.Gamma => new ValueTuple("gammauser", "gammapassword"), _ => throw new ArgumentOutOfRangeException(nameof(webAccount), webAccount, null) }; }