Created
February 22, 2022 13:44
-
-
Save joewashington75/4baedd90623c155c4bec3e0befa7d268 to your computer and use it in GitHub Desktop.
Basic Authentication automation using Playwright
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
using Cocona; | |
using Microsoft.Playwright; | |
using PlaywightAzureAdBasicAuth; | |
var builder = CoconaApp.CreateBuilder(); | |
var app = builder.Build(); | |
app.AddCommand(async ([Argument(Description = "Username to login with")] string userName, | |
[Argument(Description = "Password to login with")] string password, | |
[Argument(Description = "The url of your application as a starting point where your login button is located")] string applicationUrl, | |
[Argument(Description = "The html id of your login button")] string loginButtonId) => | |
{ | |
using var playwright = await Playwright.CreateAsync(); | |
await using var browser = await playwright.Chromium.LaunchPersistentContextAsync("", new BrowserTypeLaunchPersistentContextOptions() | |
{ | |
Headless = false, | |
Args = new List<string> | |
{ | |
"-incognito" | |
}, | |
HttpCredentials = new HttpCredentials | |
{ | |
Username = userName, | |
Password = password | |
} | |
}); | |
var page = browser.Pages.First(); | |
await page.GoToApplicationAsync(applicationUrl); | |
await page.ClickLogonAsync(loginButtonId); | |
await page.SetAzureAdLoginDetailsAsync(userName); | |
Console.ReadLine(); | |
}); | |
app.Run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment