Skip to content

Instantly share code, notes, and snippets.

@joewashington75
Created February 22, 2022 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joewashington75/4baedd90623c155c4bec3e0befa7d268 to your computer and use it in GitHub Desktop.
Save joewashington75/4baedd90623c155c4bec3e0befa7d268 to your computer and use it in GitHub Desktop.
Basic Authentication automation using Playwright
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