Skip to content

Instantly share code, notes, and snippets.

@MarcusFelling
Created September 19, 2023 14:43
Show Gist options
  • Save MarcusFelling/ac5486defbafd734ee23783859658c13 to your computer and use it in GitHub Desktop.
Save MarcusFelling/ac5486defbafd734ee23783859658c13 to your computer and use it in GitHub Desktop.
import { test as setup } from '@playwright/test';
const authFile = '.auth/user.json';
setup('authenticate', async ({ page }) => {
// skip setup if environment variables for AAD creds are not set
setup.skip(!process.env.AADUSERNAME || !process.env.AADPASSWORD, 'AADUSERNAME and AADPASSWORD environment variables must be set');
await page.goto('');
// Sign in using creds from env variables
const dialogPromise = page.waitForEvent('popup');
await page.getByText('LOGIN').click();
const dialog = await dialogPromise;
await dialog.getByPlaceholder('Email, phone, or Skype').fill(process.env.AADUSERNAME!);
await dialog.getByRole('button', { name: 'Next' }).click();
await dialog.getByPlaceholder('Password').fill(process.env.AADPASSWORD!);
await dialog.getByRole('button', { name: 'Sign in' }).click();
// Do not stay signed in
await dialog.getByRole('button', { name: 'No' }).click();
// Use try catch block to handle the case where the consent dialog is shown for the first login
try {
await dialog.waitForURL('**/Consent/**');
await dialog.getByRole('button', { name: 'Yes' }).click();
} catch (e) {
// Consent dialog was not shown
}
// Save auth state to file (.gitignore'd)
await page.context().storageState({ path: authFile });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment