Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
test: fix electron tests (#38654)
Dmitry Gozmane5cc9f36f938
diff --git a/tests/android/androidTest.ts b/tests/android/androidTest.tsindex e2015db451..cb3d16828b 100644--- a/tests/android/androidTest.ts+++ b/tests/android/androidTest.ts@@ -57,6 +57,7 @@ export const androidTest = baseTest.extend<PageTestFixtures & AndroidTestFixtureawait run(Number(browserVersion.split('.')[0]));}, { scope: 'worker' }],+ isBidi: [false, { scope: 'worker' }],isAndroid: [true, { scope: 'worker' }],isElectron: [false, { scope: 'worker' }],electronMajorVersion: [0, { scope: 'worker' }],diff --git a/tests/config/utils.ts b/tests/config/utils.tsindex bba8dc3596..b1ae53f67f 100644--- a/tests/config/utils.ts+++ b/tests/config/utils.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import type { Frame, Page } from 'playwright-core';+import type { Locator, Frame, Page } from 'playwright-core';import { ZipFile } from '../../packages/playwright-core/lib/server/utils/zipFile';import type { TraceLoaderBackend } from '../../packages/playwright-core/src/utils/isomorphic/trace/traceLoader';import type { StackFrame } from '../../packages/protocol/src/channels';@@ -25,6 +25,8 @@ import { buildActionTree, TraceModel } from '../../packages/playwright-core/src/import type { ActionTraceEvent, ConsoleMessageTraceEvent, EventTraceEvent, TraceEvent } from '@trace/trace';import { renderTitleForCall } from '../../packages/playwright-core/lib/utils/isomorphic/protocolFormatter';+export type BoundingBox = Awaited<ReturnType<Locator['boundingBox']>>;+export async function attachFrame(page: Page, frameId: string, url: string): Promise<Frame> {const handle = await page.evaluateHandle(async ({ frameId, url }) => {const frame = document.createElement('iframe');@@ -211,6 +213,36 @@ export function waitForTestLog<T>(page: Page, prefix: string): Promise<T> {});}+export async function rafraf(target: Page | Frame, count = 1) {+ for (let i = 0; i < count; i++) {+ await target.evaluate(async () => {+ await new Promise(f => window.builtins.requestAnimationFrame(() => window.builtins.requestAnimationFrame(f)));+ });+ }+}++export function roundBox(box: BoundingBox): BoundingBox {+ return {+ x: Math.round(box.x),+ y: Math.round(box.y),+ width: Math.round(box.width),+ height: Math.round(box.height),+ };+}++export function unshift(snapshot: string): string {+ const lines = snapshot.split('\n');+ let whitespacePrefixLength = 100;+ for (const line of lines) {+ if (!line.trim())+ continue;+ const match = line.match(/^(\s*)/);+ if (match && match[1].length < whitespacePrefixLength)+ whitespacePrefixLength = match[1].length;+ }+ return lines.filter(t => t.trim()).map(line => line.substring(whitespacePrefixLength)).join('\n');+}+const ansiRegex = new RegExp('[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))', 'g');export function stripAnsi(str: string): string {return str.replace(ansiRegex, '');diff --git a/tests/electron/electronTest.ts b/tests/electron/electronTest.tsindex 516f5c1e5c..e98d47112e 100644--- a/tests/electron/electronTest.ts+++ b/tests/electron/electronTest.ts@@ -36,6 +36,7 @@ export const electronTest = baseTest.extend<TraceViewerFixtures>(traceViewerFixtbrowserVersion: [({}, use) => use(process.env.ELECTRON_CHROMIUM_VERSION), { scope: 'worker' }],browserMajorVersion: [({}, use) => use(Number(process.env.ELECTRON_CHROMIUM_VERSION.split('.')[0])), { scope: 'worker' }],electronMajorVersion: [({}, use) => use(parseInt(require('electron/package.json').version.split('.')[0], 10)), { scope: 'worker' }],+ isBidi: [false, { scope: 'worker' }],isAndroid: [false, { scope: 'worker' }],isElectron: [true, { scope: 'worker' }],isHeadlessShell: [false, { scope: 'worker' }],diff --git a/tests/library/browsertype-connect.spec.ts b/tests/library/browsertype-connect.spec.tsindex acd5367677..55fc1876a2 100644--- a/tests/library/browsertype-connect.spec.ts+++ b/tests/library/browsertype-connect.spec.ts@@ -22,7 +22,7 @@ import * as path from 'path';import { getUserAgent, getPlaywrightVersion } from '../../packages/playwright-core/lib/server/utils/userAgent';import WebSocket from 'ws';import { expect, playwrightTest } from '../config/browserTest';-import { parseTrace, suppressCertificateWarning } from '../config/utils';+import { parseTrace, suppressCertificateWarning, rafraf } from '../config/utils';import formidable from 'formidable';import type { Browser, ConnectOptions } from 'playwright-core';import { createHttpServer } from '../../packages/playwright-core/lib/server/utils/network';@@ -497,7 +497,7 @@ for (const kind of ['launchServer', 'run-server'] as const) {});const page = await context.newPage();await page.evaluate(() => document.body.style.backgroundColor = 'red');- await new Promise(r => setTimeout(r, 1000));+ await rafraf(page, 100);await context.close();test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36685' });diff --git a/tests/library/debug-controller.spec.ts b/tests/library/debug-controller.spec.tsindex 9159eaad66..1ceb9e5048 100644--- a/tests/library/debug-controller.spec.ts+++ b/tests/library/debug-controller.spec.ts@@ -20,7 +20,7 @@ import { createGuid } from '../../packages/playwright-core/lib/server/utils/crypimport { Backend } from '../config/debugControllerBackend';import type { Browser, BrowserContext } from '@playwright/test';import type * as channels from '@protocol/channels';-import { roundBox } from '../page/pageTest';+import { roundBox } from '../config/utils';type BrowserWithReuse = Browser & { newContextForReuse: () => Promise<BrowserContext> };type Fixtures = {diff --git a/tests/library/inspector/cli-codegen-aria.spec.ts b/tests/library/inspector/cli-codegen-aria.spec.tsindex 6db0d6df5b..70e9528263 100644--- a/tests/library/inspector/cli-codegen-aria.spec.ts+++ b/tests/library/inspector/cli-codegen-aria.spec.ts@@ -15,7 +15,7 @@*/import { test, expect } from './inspectorTest';-import { roundBox } from '../../page/pageTest';+import { roundBox } from '../../config/utils';test.describe(() => {test.skip(({ mode }) => mode !== 'default');diff --git a/tests/library/inspector/cli-codegen-pick-locator.spec.ts b/tests/library/inspector/cli-codegen-pick-locator.spec.tsindex bd1a612842..346dbcfaad 100644--- a/tests/library/inspector/cli-codegen-pick-locator.spec.ts+++ b/tests/library/inspector/cli-codegen-pick-locator.spec.ts@@ -15,7 +15,7 @@*/import { test, expect } from './inspectorTest';-import { roundBox } from '../../page/pageTest';+import { roundBox } from '../../config/utils';test.describe(() => {test.skip(({ mode }) => mode !== 'default');diff --git a/tests/library/inspector/pause.spec.ts b/tests/library/inspector/pause.spec.tsindex 9fc1ec752f..dc2362546d 100644--- a/tests/library/inspector/pause.spec.ts+++ b/tests/library/inspector/pause.spec.ts@@ -16,9 +16,8 @@import type { Page } from 'playwright-core';import { test as it, expect, Recorder } from './inspectorTest';-import { waitForTestLog } from '../../config/utils';-import { roundBox } from '../../page/pageTest';-import type { BoundingBox } from '../../page/pageTest';+import { roundBox, waitForTestLog } from '../../config/utils';+import type { BoundingBox } from '../../config/utils';import { pauseHelper } from './pause-helper';it('should resume when closing inspector', async ({ page, recorderPageGetter, closeRecorder, mode }) => {diff --git a/tests/library/trace-viewer.spec.ts b/tests/library/trace-viewer.spec.tsindex d3788bf7d8..84d1d745bb 100644--- a/tests/library/trace-viewer.spec.ts+++ b/tests/library/trace-viewer.spec.ts@@ -26,8 +26,7 @@ import type http from 'http';import { pathToFileURL } from 'url';import { expect, playwrightTest } from '../config/browserTest';import type { FrameLocator } from '@playwright/test';-import { rafraf, roundBox } from '../page/pageTest';-import { parseTrace } from '../config/utils';+import { parseTrace, rafraf, roundBox } from '../config/utils';const test = playwrightTest.extend<TraceViewerFixtures>(traceViewerFixtures);diff --git a/tests/library/video.spec.ts b/tests/library/video.spec.tsindex fc2c89a40c..49b48459eb 100644--- a/tests/library/video.spec.ts+++ b/tests/library/video.spec.ts@@ -17,11 +17,10 @@import { spawnSync } from 'child_process';import fs from 'fs';import path from 'path';-import type { Page } from 'playwright-core';import { PNG, jpegjs } from 'playwright-core/lib/utilsBundle';import { registry } from '../../packages/playwright-core/lib/server';import { expect, browserTest as it } from '../config/browserTest';-import { parseTraceRaw } from '../config/utils';+import { parseTraceRaw, rafraf } from '../config/utils';export class VideoPlayer {fileName: string;@@ -171,7 +170,7 @@ it.describe('screencast', () => {const page = await context.newPage();await page.evaluate(() => document.body.style.backgroundColor = 'red');- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.close();const videoFile = await page.video().path();@@ -196,7 +195,7 @@ it.describe('screencast', () => {const page = await context.newPage();await page.evaluate(() => document.body.style.backgroundColor = 'red');- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.close();const videoFile = await page.video().path();@@ -226,7 +225,7 @@ it.describe('screencast', () => {document.body.textContent = ''; // remove linkdocument.body.style.backgroundColor = 'red';});- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.close();const videoFile = await page.video().path();@@ -294,7 +293,7 @@ it.describe('screencast', () => {const page = await context.newPage();const deletePromise = page.video().delete();await page.evaluate(() => document.body.style.backgroundColor = 'red');- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.close();const videoPath = await page.video().path();@@ -385,9 +384,9 @@ it.describe('screencast', () => {const page = await context.newPage();await page.goto(server.PREFIX + '/background-color.html#rgb(0,0,0)');- await waitForRafs(page, 100);+ await rafraf(page, 100);await page.goto(server.CROSS_PROCESS_PREFIX + '/background-color.html#rgb(100,100,100)');- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.close();const videoFile = await page.video().path();@@ -423,7 +422,7 @@ it.describe('screencast', () => {const page = await context.newPage();await page.goto(server.PREFIX + '/rotate-z.html');- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.close();const videoFile = await page.video().path();@@ -457,8 +456,8 @@ it.describe('screencast', () => {]);await popup.evaluate(() => document.body.style.backgroundColor = 'red');await Promise.all([- waitForRafs(page, 100),- waitForRafs(popup, 100),+ rafraf(page, 100),+ rafraf(popup, 100),]);await context.close();@@ -490,11 +489,11 @@ it.describe('screencast', () => {await page.$eval('.container', container => {container.firstElementChild.classList.remove('red');});- await waitForRafs(page, 100);+ await rafraf(page, 100);await page.$eval('.container', container => {container.firstElementChild.classList.add('red');});- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.close();const videoFile = await page.video().path();@@ -530,7 +529,7 @@ it.describe('screencast', () => {});const page = await context.newPage();- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.close();const videoFile = await page.video().path();@@ -547,7 +546,7 @@ it.describe('screencast', () => {});const page = await context.newPage();- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.close();const videoFile = await page.video().path();@@ -567,7 +566,7 @@ it.describe('screencast', () => {});const page = await context.newPage();- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.close();const videoFile = await page.video().path();@@ -588,7 +587,7 @@ it.describe('screencast', () => {});await page.evaluate(() => document.body.style.backgroundColor = 'red');- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.close();const videoFile = await page.video().path();@@ -617,7 +616,7 @@ it.describe('screencast', () => {});const page = await context.newPage();- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.close();const videoFile = await page.video().path();@@ -638,7 +637,7 @@ it.describe('screencast', () => {});const page = await context.newPage();- await waitForRafs(page, 100);+ await rafraf(page, 100);await browser.close();const file = testInfo.outputPath('saved-video-');@@ -659,7 +658,7 @@ it.describe('screencast', () => {});const page = await context.newPage();- await waitForRafs(page, 100);+ await rafraf(page, 100);await (browser as any)._channel.killForTests();const file = testInfo.outputPath('saved-video-');@@ -681,7 +680,7 @@ it.describe('screencast', () => {});const page = await context.newPage();- await waitForRafs(page, 100);+ await rafraf(page, 100);await page.close();await context.close();await browser.close();@@ -709,7 +708,7 @@ it.describe('screencast', () => {const page = await context.newPage();await page.goto(server.EMPTY_PAGE);- await waitForRafs(page, 100);+ await rafraf(page, 100);const cookies = await context.cookies();expect(cookies.length).toBe(1);@@ -738,7 +737,7 @@ it.describe('screencast', () => {const page = await context.newPage();await page.setContent(`<div style='margin: 0; background: red; position: fixed; right:0; bottom:0; width: 30; height: 30;'></div>`);- await waitForRafs(page, 100);+ await rafraf(page, 100);await page.close();await context.close();await browser.close();@@ -775,7 +774,7 @@ it.describe('screencast', () => {const page = await context.newPage();await page.setContent(`<div style='margin: 0; background: red; position: fixed; right:0; bottom:0; width: 30; height: 30;'></div>`);- await waitForRafs(page, 100);+ await rafraf(page, 100);await page.close();await context.close();await browser.close();@@ -811,7 +810,7 @@ it.describe('screencast', () => {const page = await context.newPage();await page.evaluate(() => document.body.style.backgroundColor = 'red');- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.tracing.stop({ path: traceFile });await context.close();@@ -849,23 +848,10 @@ it('should saveAs video', async ({ browser }, testInfo) => {});const page = await context.newPage();await page.evaluate(() => document.body.style.backgroundColor = 'red');- await waitForRafs(page, 100);+ await rafraf(page, 100);await context.close();const saveAsPath = testInfo.outputPath('my-video.webm');await page.video().saveAs(saveAsPath);expect(fs.existsSync(saveAsPath)).toBeTruthy();});--async function waitForRafs(page: Page, count: number): Promise<void> {- await page.evaluate(count => new Promise<void>(resolve => {- const onRaf = () => {- --count;- if (!count)- resolve();- else- window.builtins.requestAnimationFrame(onRaf);- };- window.builtins.requestAnimationFrame(onRaf);- }), count);-}diff --git a/tests/page/locator-highlight.spec.ts b/tests/page/locator-highlight.spec.tsindex 92f338c71c..4e52ce9c26 100644--- a/tests/page/locator-highlight.spec.ts+++ b/tests/page/locator-highlight.spec.ts@@ -14,7 +14,8 @@* limitations under the License.*/-import { test as it, expect, roundBox } from './pageTest';+import { test as it, expect } from './pageTest';+import { roundBox } from '../config/utils';it.skip(({ mode }) => mode !== 'default', 'Highlight element has a closed shadow-root on != default');diff --git a/tests/page/page-aria-snapshot-ai.spec.ts b/tests/page/page-aria-snapshot-ai.spec.tsindex 98b316536c..4677087d0a 100644--- a/tests/page/page-aria-snapshot-ai.spec.ts+++ b/tests/page/page-aria-snapshot-ai.spec.ts@@ -17,7 +17,8 @@// @ts-ignoreimport { asLocator } from 'playwright-core/lib/utils';-import { test as it, expect, unshift } from './pageTest';+import { test as it, expect } from './pageTest';+import { unshift } from '../config/utils';async function snapshotForAI(page: any, options?: { timeout?: number, mode?: 'full' | 'incremental', track?: string }): Promise<string> {const snapshot = await page._snapshotForAI(options);diff --git a/tests/page/page-aria-snapshot.spec.ts b/tests/page/page-aria-snapshot.spec.tsindex 8157d7787b..b1e7a5ad3a 100644--- a/tests/page/page-aria-snapshot.spec.ts+++ b/tests/page/page-aria-snapshot.spec.ts@@ -15,7 +15,8 @@*/import type { Locator } from '@playwright/test';-import { test as it, expect, unshift } from './pageTest';+import { test as it, expect } from './pageTest';+import { unshift } from '../config/utils';async function checkAndMatchSnapshot(locator: Locator, snapshot: string) {expect.soft(await locator.ariaSnapshot()).toBe(unshift(snapshot));diff --git a/tests/page/pageTest.ts b/tests/page/pageTest.tsindex 92de301080..11b6b74607 100644--- a/tests/page/pageTest.ts+++ b/tests/page/pageTest.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import type { Frame, Page, TestType, Locator } from '@playwright/test';+import type { TestType } from '@playwright/test';import type { PlatformWorkerFixtures } from '../config/platformFixtures';import type { TestModeTestFixtures, TestModeWorkerFixtures, TestModeWorkerOptions } from '../config/testModeFixtures';import { androidTest } from '../android/androidTest';@@ -23,9 +23,9 @@ import { electronTest } from '../electron/electronTest';import type { PageTestFixtures, PageWorkerFixtures } from './pageTestApi';import type { ServerFixtures, ServerWorkerOptions } from '../config/serverFixtures';import { expect as baseExpect } from '@playwright/test';+export { rafraf } from '../config/utils';let impl: TestType<PageTestFixtures & ServerFixtures & TestModeTestFixtures, PageWorkerFixtures & PlatformWorkerFixtures & TestModeWorkerFixtures & TestModeWorkerOptions & ServerWorkerOptions> = browserTest;-export type BoundingBox = Awaited<ReturnType<Locator['boundingBox']>>;if (process.env.PWPAGE_IMPL === 'android')impl = androidTest;@@ -34,36 +34,6 @@ if (process.env.PWPAGE_IMPL === 'electron')export const test = impl;-export async function rafraf(target: Page | Frame, count = 1) {- for (let i = 0; i < count; i++) {- await target.evaluate(async () => {- await new Promise(f => window.builtins.requestAnimationFrame(() => window.builtins.requestAnimationFrame(f)));- });- }-}--export function roundBox(box: BoundingBox): BoundingBox {- return {- x: Math.round(box.x),- y: Math.round(box.y),- width: Math.round(box.width),- height: Math.round(box.height),- };-}--export function unshift(snapshot: string): string {- const lines = snapshot.split('\n');- let whitespacePrefixLength = 100;- for (const line of lines) {- if (!line.trim())- continue;- const match = line.match(/^(\s*)/);- if (match && match[1].length < whitespacePrefixLength)- whitespacePrefixLength = match[1].length;- }- return lines.filter(t => t.trim()).map(line => line.substring(whitespacePrefixLength)).join('\n');-}-export const expect = baseExpect.extend({toContainYaml(received: string, expected: string) {const trimmed = expected.split('\n').filter(a => !!a.trim());