Skip to content

Commit

Permalink
test(KTL-1189); fix "Page closed" for github_mac
Browse files Browse the repository at this point in the history
  • Loading branch information
zoobestik committed Aug 22, 2023
1 parent df42131 commit 37cc4b1
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 36 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,5 @@ jobs:
path: |
test-results/
playwright-report/
tests/**/__screenshots__
!tests/**/__screenshots__/dev
tests/**/__screenshots__/github_*
retention-days: 5
31 changes: 16 additions & 15 deletions playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@ import { defineConfig, devices } from '@playwright/test';
dotenv({ path: `.env.local`, override: true });

const PROJECTS_LIST = {
DEV: [ 'Desktop Chrome' ],
GITHUB_MAC: [ 'Desktop Safari' ],
GITHUB_LINUX: [ 'Desktop Chrome', 'Desktop Firefox' ],
DEV: ['Desktop Chrome'],
GITHUB_MAC: ['Desktop Safari'],
GITHUB_LINUX: ['Desktop Chrome', 'Desktop Firefox'],
};

const envMode = (env.TEST_PROJECT_LIST || 'DEV').toUpperCase();
const mode = (env.TEST_PROJECT_LIST || 'DEV').toUpperCase();

if (!(envMode && isKeyOfObject(envMode, PROJECTS_LIST))) {
if (!(mode && isKeyOfObject(mode, PROJECTS_LIST))) {
const list = Object.keys(PROJECTS_LIST)
.map((s) => `'${s.toLowerCase()}'`)
.join(' or ');
.map((s) => `'${s.toLowerCase()}'`)
.join(' or ');

throw Error(`TEST_PROJECT_LIST should be ${list}`);
}

const mode = envMode;
const isDevMode = Boolean(mode === 'DEV');

export default defineConfig({
testDir: './tests',
testMatch: /.*\.e2e\.tsx?$/,
snapshotPathTemplate: `{testDir}/__screenshots__/${mode.toLowerCase()}/{projectName}/{testFilePath}-{arg}{ext}`,

timeout: 60000,
timeout: 30000,
forbidOnly: !isDevMode,
reporter: 'list',
retries: isDevMode ? 0 : 2,
Expand All @@ -42,7 +41,9 @@ export default defineConfig({

use: {
testIdAttribute: 'data-test',
headless: (value => value ? value === 'true' : !isDevMode)(env.TEST_HEADLESS_MODE),
headless: ((value) => (value ? value === 'true' : !isDevMode))(
env.TEST_HEADLESS_MODE,
),
ignoreHTTPSErrors: true,
screenshot: {
fullPage: true,
Expand All @@ -52,15 +53,15 @@ export default defineConfig({
video: isDevMode ? 'on-first-retry' : 'on',
},

projects: PROJECTS_LIST[mode].map(project => ({
projects: PROJECTS_LIST[mode].map((project) => ({
name: project,
use: {...devices[project]},
}))
use: { ...devices[project] },
})),
});

export function isKeyOfObject<T extends object>(
key: string | number | symbol,
obj: T,
key: string | number | symbol,
obj: T,
): key is keyof T {
return key in obj;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion tests/basics.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ import { prepareNetwork, RouteFulfill, toPostData } from './utlis';
import { mockRunRequest, waitRunRequest } from './utlis/mocks/compiler';

test.describe('basics', () => {
let unMockNetwork: CallableFunction;

test.beforeEach(async ({ context, baseURL }) => {
await prepareNetwork(context, baseURL); // offline mode
unMockNetwork = await prepareNetwork(context, baseURL); // offline mode
});

test.afterEach(async () => {
if (unMockNetwork) await unMockNetwork();
});

test('simple usage', async ({ page }) => {
Expand Down
28 changes: 18 additions & 10 deletions tests/utlis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@ import { mockVersions } from './mocks/compiler';

export type RouteFulfill = Parameters<Route['fulfill']>[0];

export function refuseExternalUrls(context: BrowserContext, baseURL: string) {
export async function refuseExternalUrls(
context: BrowserContext,
baseURL: string,
) {
const host = new URL(baseURL).host;
return context.route(
(url) => url.host !== host,
(route) => route.abort('connectionrefused'),
);

const checkUrl = (url: URL) => url.host && url.host !== host;
const onMatch = (route: Route) => route.abort('connectionrefused');

await context.route(checkUrl, onMatch);
return () => context.unroute(checkUrl, onMatch);
}

export function prepareNetwork(context: BrowserContext, baseURL: string) {
return Promise.all([
refuseExternalUrls(context, baseURL),
mockVersions(context),
]);
export async function prepareNetwork(context: BrowserContext, baseURL: string) {
const unRefuse = await refuseExternalUrls(context, baseURL);
const unVersions = await mockVersions(context);

return async () => {
await unVersions();
await unRefuse();
};
}

export function toPostData(code: string) {
Expand Down
17 changes: 9 additions & 8 deletions tests/utlis/mocks/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ export const API_HOST = 'api.kotlinlang.org';

function defaultVersions(route: Route, req: Request) {
if (req.method() !== 'GET') {
route.fallback();
return;
return route.continue();
}

return route.fulfill({ path: join(__dirname, 'versions.json') });
}

export function mockVersions(
export async function mockVersions(
context: BrowserContext,
resp?: Parameters<BrowserContext['route']>[1],
) {
return context.route(
(url) =>
url.host === API_HOST && url.pathname.match(/^\/?\/versions$/) !== null,
resp || defaultVersions,
);
const checkUrl = (url: URL) =>
url.host === API_HOST && url.pathname.match(/^\/?\/versions$/) !== null;
const onMatch = resp || defaultVersions;

await context.route(checkUrl, onMatch);

return () => context.unroute(checkUrl, onMatch);
}

function isRunRequest(url: URL | string) {
Expand Down

0 comments on commit 37cc4b1

Please sign in to comment.