To manage SSO authentication in a CI/CD pipeline using @playwright/test, follow these steps:
auth.setup.ts
Prepare authenticated browser state for all tests.
import { test } from '@playwright/test';
test('authenticate', async ({ browser }) => {
// Your authentication logic here
});
parallelIndex
as a unique identifierEach worker gets a unique parallelIndex
.
const parallelIndex = test.workerIndex;
Use a unique account or a list of precreated accounts for testing.
const account = accounts[parallelIndex];
Fill in the username/password fields and submit the form.
await page.fill('#username', account.username);
await page.fill('#password', account.password);
await page.click('#submit');
Wait until the final URL is reached or a specific element is visible.
await page.waitForSelector('#loggedInElement');
storageState
Save the authenticated state to a file.
const authFile = `playwright/.auth/state-${parallelIndex}.json`;
await context.storageState({ path: authFile });
Declare the setup project as a dependency for all testing projects in your playwright.config.ts
.
import { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
projects: [
{
name: 'auth-setup',
testMatch: 'auth.setup.ts',
},
{
name: 'tests',
testMatch: '*.spec.ts',
dependsOn: ['auth-setup'],
},
],
};
export default config;
Use the saved storageState
in your tests.
import { test } from '@playwright/test';
test.use({
storageState: `playwright/.auth/state-${test.workerIndex}.json`,
});
test('example test', async ({ page }) => {
// Your test logic here
});
By following these steps, you can efficiently handle SSO authentication in your CI/CD pipeline using @playwright/test.
If you still have questions, please ask a question and I will try to answer it.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].