Skip to content

Commit 3d44307

Browse files
committed
fix(e2e): handling cookie banner for production tests
- Introduced global setup script for consistent cookie banner handling in production. - Removed redundant cookie banner interaction logic from individual test files. - Updated `.gitignore` to include `test/storage-state.json`.
1 parent 8fc647a commit 3d44307

21 files changed

+75
-70
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ generated
5050
/data/page_views_map.json
5151
test-results
5252

53-
public/data
53+
public/data
54+
test/storage-state.json

playwright.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const timeout = isDevelopment ? 10000 : 5000;
1111
const forbidOnly = !isDevelopment;
1212

1313
export default defineConfig({
14+
globalSetup: require.resolve('./test/global-setup.ts'),
1415
forbidOnly,
1516
retries,
1617
reporter,
@@ -21,7 +22,8 @@ export default defineConfig({
2122
toHaveScreenshot: { maxDiffPixelRatio: MAX_DIFF_PIXEL_RATIO }
2223
},
2324
use: {
24-
baseURL: process.env.BASE_URL || 'http://localhost:9000',
25+
baseURL: process.env.BASE_URL || 'http://localhost:3000',
26+
storageState: 'test/storage-state.json',
2527
trace: 'off',
2628
screenshot: 'only-on-failure',
2729
video: 'retain-on-failure'

test/e2e/teach/courses.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { expect, test } from '@playwright/test';
22
import { CoursesPage } from '../../page/teach/courses-page';
3-
import { closeExternalBanners } from '../utils';
43
import { testSelector } from '../../utils';
54
import { checkTeachCta, checkTeachMap, checkTeachNav } from './utils';
65

76
test.describe('Courses page appearance and functionality', async () => {
87
test.beforeEach(async ({ page, context, baseURL }) => {
98
const coursesPage = new CoursesPage(page);
109
await coursesPage.init();
11-
await closeExternalBanners(context, page, baseURL);
1210
});
1311

1412
test('Should load the courses page correctly', async ({ page }) => {

test/e2e/teach/education.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { expect, test } from '@playwright/test';
22
import { TeachPage } from '../../page/teach/education';
3-
import { closeExternalBanners } from '../utils';
43
import { testSelector } from '../../utils';
54
import { checkTeachCta, checkTeachMap, checkTeachNav, MAILTO_LINK, MATERIALS_LINK, SIGNUP_LINK } from './utils';
65

76
test.describe('Education landing page content and interactions', async () => {
8-
test.beforeEach(async ({ context, page, baseURL }) => {
7+
test.beforeEach(async ({ page }) => {
98
const teachPage = new TeachPage(page);
109
await teachPage.init();
11-
await closeExternalBanners(context, page, baseURL);
1210
});
1311

1412
test('Should load the education page correctly', async ({ page }) => {

test/e2e/teach/why.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { expect, test } from '@playwright/test';
22
import { WhyTeachPage } from '../../page/teach/why-page';
3-
import { closeExternalBanners } from '../utils';
43
import { checkTeachCta, checkTeachNav } from './utils';
54
import { testSelector } from '../../utils';
65

@@ -21,10 +20,9 @@ function toId(label: typeof LIST_OF_SECTION[number][0]) {
2120
}
2221

2322
test.describe('Why Teach Kotlin page appearance and functionality', async () => {
24-
test.beforeEach(async ({ page, context, baseURL }) => {
23+
test.beforeEach(async ({ page }) => {
2524
const whyTeachPage = new WhyTeachPage(page);
2625
await whyTeachPage.init();
27-
await closeExternalBanners(context, page, baseURL);
2826
});
2927

3028
test('Should load the Why Teach Kotlin page correctly', async ({ page }) => {

test/e2e/utils.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { BrowserContext, ElementHandle, expect, Page, test } from '@playwright/test';
2-
import { closeCookiesConsentBanner, isSkipScreenshot } from '../utils';
1+
import { ElementHandle, expect, Page, test } from '@playwright/test';
2+
import { isSkipScreenshot } from '../utils';
33
import { PageAssertionsToHaveScreenshotOptions } from 'playwright/types/test';
44

55
export async function getElementScreenshotWithPadding(page: Page, element: ElementHandle, padding: number): Promise<Buffer | undefined> {
@@ -18,16 +18,6 @@ export async function getElementScreenshotWithPadding(page: Page, element: Eleme
1818
}
1919
}
2020

21-
export async function closeExternalBanners(context: BrowserContext, page: Page, baseUrl: string) {
22-
if (baseUrl.startsWith('https://kotlinlang.org/')) {
23-
await closeCookiesConsentBanner(context, baseUrl);
24-
} else {
25-
await page.frameLocator('#webpack-dev-server-client-overlay')
26-
.locator('[aria-label="Dismiss"]')
27-
.click();
28-
}
29-
}
30-
3121
export function pageWrapperMask(page: Page) {
3222
return [
3323
page.locator('header[data-test="header"]'),

test/global-setup.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { join } from 'node:path';
2+
import { writeFile } from 'node:fs/promises';
3+
import { chromium, FullConfig } from '@playwright/test';
4+
import { isProduction } from './utils';
5+
6+
export default async function globalSetup(config: FullConfig) {
7+
const storageStatePath = join(__dirname, 'storage-state.json');
8+
await writeFile(storageStatePath, '{}', 'utf-8');
9+
10+
const project = config.projects[0];
11+
console.log(`[Global Setup] Processing project ${project.name}`);
12+
const baseURL = project.use?.baseURL;
13+
14+
if (isProduction(baseURL)) {
15+
await closeProductionElements(baseURL, storageStatePath);
16+
}
17+
}
18+
19+
async function closeProductionElements(baseURL: string, storageStatePath: string) {
20+
console.log(`[Global Setup] Starting cookie banner setup for ${baseURL}`);
21+
22+
const browser = await chromium.launch();
23+
const context = await browser.newContext();
24+
const page = await context.newPage();
25+
26+
try {
27+
await page.goto(baseURL, { waitUntil: 'domcontentloaded' });
28+
29+
try {
30+
const acceptButton = page.getByRole('button', { name: 'Accept All' });
31+
await acceptButton.waitFor({ state: 'visible', timeout: 5000 });
32+
await acceptButton.click();
33+
34+
await page.waitForTimeout(1000);
35+
} catch (error) {
36+
console.log('[Global Setup] Cookie banner not found - continuing');
37+
}
38+
39+
const closeBanner = page.locator('#optly-banner_close');
40+
41+
if (await closeBanner.count() > 0) {
42+
console.log('[Global Setup] Closing "purple" banner');
43+
await closeBanner.click();
44+
await page.waitForSelector('#optly-banner_close', { state: 'hidden' });
45+
}
46+
47+
await context.storageState({ path: storageStatePath });
48+
console.log(`[Global Setup] Storage state saved to ${storageStatePath}`);
49+
} catch (error) {
50+
console.error('[Global Setup] Error during setup:', error);
51+
throw error;
52+
} finally {
53+
await context.close();
54+
await browser.close();
55+
}
56+
}

test/production/api-navigation.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { expect, Page, test } from '@playwright/test';
33
test.describe('Api navigation', () => {
44
test.beforeEach(async ({ page }) => {
55
await page.goto('/');
6-
await page.waitForSelector('button.ch2-btn.ch2-btn-primary');
7-
await page.click('button.ch2-btn.ch2-btn-primary');
86
const navbar = page.locator('[data-test="header"]');
97
const apiButton = navbar.getByText('API', { exact: true });
108
await expect(apiButton).toBeVisible();

test/production/community-kotlin-user-groups.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { test, expect } from '@playwright/test';
33
test.describe('Community Kotlin User Groups page', () => {
44
test.beforeEach(async ({ page }) => {
55
await page.goto('/community/user-groups/');
6-
await page.waitForSelector('button.ch2-btn.ch2-btn-primary');
7-
await page.click('button.ch2-btn.ch2-btn-primary');
86
});
97

108
test('Overview in navbar opens the related page', async ({ page }) => {

test/production/community-overview-keep-in-touch-section.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ test.describe('Community page, overview tab, keep in touch section', () => {
77
test.beforeEach(async ({ page }) => {
88
communityPage = new CommunityPage(page);
99
await communityPage.init();
10-
await page.waitForSelector('button.ch2-btn.ch2-btn-primary');
11-
await page.click('button.ch2-btn.ch2-btn-primary');
1210
});
1311

1412
test('Slack button opens the related page', async ({ page, context }) => {

0 commit comments

Comments
 (0)