Created
September 19, 2023 14:43
-
-
Save MarcusFelling/ac5486defbafd734ee23783859658c13 to your computer and use it in GitHub Desktop.
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
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