Convert apiToken/apiRequest to worker-scoped Playwright fixtures

Problem: Each test created a new browser context and performed a full
OIDC login (120 logins in a 40min serial run). By test ~60, Chromium
was bloated and login took 30s+ instead of 3s.

Fix: apiToken and apiRequest are now worker-scoped — login happens
ONCE per Playwright worker, token is reused for all API tests.
liveAuthPage stays test-scoped (UI tests need fresh pages).

Impact: ~120 OIDC logins → 1 per worker. Eliminates auth overhead
as the bottleneck for later tests in the suite.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
master
2026-04-02 13:59:45 +03:00
parent 959afb6d21
commit 5a8c6635fc

View File

@@ -94,28 +94,28 @@ async function loginAndGetToken(page: Page): Promise<string> {
throw new Error('Login failed: could not extract auth token');
}
export const test = base.extend<{
// Test-scoped fixtures (fresh per test)
type TestFixtures = {
liveAuthPage: Page;
};
// Worker-scoped fixtures (shared across all tests in a worker)
type WorkerFixtures = {
apiToken: string;
apiRequest: APIRequestContext;
}>({
liveAuthPage: async ({ browser }, use) => {
const context = await browser.newContext({ ignoreHTTPSErrors: true });
const page = await context.newPage();
await loginAndGetToken(page);
await use(page);
await context.close();
},
};
apiToken: async ({ browser }, use) => {
export const test = base.extend<TestFixtures, WorkerFixtures>({
// Worker-scoped: login ONCE per worker, reuse token for all tests
apiToken: [async ({ browser }, use) => {
const context = await browser.newContext({ ignoreHTTPSErrors: true });
const page = await context.newPage();
const token = await loginAndGetToken(page);
await use(token);
await context.close();
},
}, { scope: 'worker' }],
apiRequest: async ({ playwright, apiToken }, use) => {
apiRequest: [async ({ playwright, apiToken }, use) => {
const ctx = await playwright.request.newContext({
baseURL: BASE_URL,
extraHTTPHeaders: {
@@ -126,6 +126,15 @@ export const test = base.extend<{
});
await use(ctx);
await ctx.dispose();
}, { scope: 'worker' }],
// Test-scoped: each UI test gets a fresh authenticated page
liveAuthPage: async ({ browser }, use) => {
const context = await browser.newContext({ ignoreHTTPSErrors: true });
const page = await context.newPage();
await loginAndGetToken(page);
await use(page);
await context.close();
},
});