From ad47fc2d60e0fe97f9246705459c7639e0e6551c Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Wed, 19 Feb 2025 12:50:51 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85(e2e)=20global=20setup=20authenticatio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Because of the parallelism of the tests, the authentication setup was flaky. Sometimes the tests would run before the authentication was complete. We change to a global setup instead of the project dependency setup, it should be more reliable. We improved the waiting states of the authentication setup. --- .../e2e/__tests__/app-impress/auth.setup.ts | 63 ++++++++++++------- src/frontend/apps/e2e/playwright.config.ts | 6 +- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/frontend/apps/e2e/__tests__/app-impress/auth.setup.ts b/src/frontend/apps/e2e/__tests__/app-impress/auth.setup.ts index 31d22b71..57168af9 100644 --- a/src/frontend/apps/e2e/__tests__/app-impress/auth.setup.ts +++ b/src/frontend/apps/e2e/__tests__/app-impress/auth.setup.ts @@ -1,27 +1,46 @@ -import { test as setup } from '@playwright/test'; +import { FullConfig, FullProject, chromium, expect } from '@playwright/test'; import { keyCloakSignIn } from './common'; -setup('authenticate-chromium', async ({ page }) => { - await page.goto('/'); - await keyCloakSignIn(page, 'chromium'); - await page - .context() - .storageState({ path: `playwright/.auth/user-chromium.json` }); -}); +const saveStorageState = async ( + browserConfig: FullProject, +) => { + const browserName = browserConfig?.name || 'chromium'; -setup('authenticate-webkit', async ({ page }) => { - await page.goto('/'); - await keyCloakSignIn(page, 'webkit'); - await page - .context() - .storageState({ path: `playwright/.auth/user-webkit.json` }); -}); + const { storageState, ...useConfig } = browserConfig?.use; + const browser = await chromium.launch(); + const context = await browser.newContext(useConfig); + const page = await context.newPage(); -setup('authenticate-firefox', async ({ page }) => { - await page.goto('/'); - await keyCloakSignIn(page, 'firefox'); - await page - .context() - .storageState({ path: `playwright/.auth/user-firefox.json` }); -}); + await page.goto('/', { waitUntil: 'networkidle' }); + await page.content(); + await expect(page.getByText('Docs').first()).toBeVisible(); + + await keyCloakSignIn(page, browserName); + + await expect( + page.locator('header').first().getByRole('button', { + name: 'Logout', + }), + ).toBeVisible(); + + await page.context().storageState({ + path: storageState as string, + }); + + await browser.close(); +}; + +async function globalSetup(config: FullConfig) { + /* eslint-disable @typescript-eslint/no-non-null-assertion */ + const chromeConfig = config.projects.find((p) => p.name === 'chromium')!; + const firefoxConfig = config.projects.find((p) => p.name === 'firefox')!; + const webkitConfig = config.projects.find((p) => p.name === 'webkit')!; + /* eslint-enable @typescript-eslint/no-non-null-assertion */ + + await saveStorageState(chromeConfig); + await saveStorageState(webkitConfig); + await saveStorageState(firefoxConfig); +} + +export default globalSetup; diff --git a/src/frontend/apps/e2e/playwright.config.ts b/src/frontend/apps/e2e/playwright.config.ts index f6007eb0..cdb6aadc 100644 --- a/src/frontend/apps/e2e/playwright.config.ts +++ b/src/frontend/apps/e2e/playwright.config.ts @@ -38,10 +38,9 @@ export default defineConfig({ timeout: 120 * 1000, reuseExistingServer: true, }, - + globalSetup: require.resolve('./__tests__/app-impress/auth.setup'), /* Configure projects for major browsers */ projects: [ - { name: 'setup', testMatch: /.*\.setup\.ts/ }, { name: 'chromium', use: { @@ -53,7 +52,6 @@ export default defineConfig({ permissions: ['clipboard-read', 'clipboard-write'], }, }, - dependencies: ['setup'], }, { name: 'webkit', @@ -63,7 +61,6 @@ export default defineConfig({ timezoneId: 'Europe/Paris', storageState: 'playwright/.auth/user-webkit.json', }, - dependencies: ['setup'], }, { name: 'firefox', @@ -79,7 +76,6 @@ export default defineConfig({ }, }, }, - dependencies: ['setup'], }, ], });