Skip to content

Commit ced4609

Browse files
committed
test: fix electron tests
1 parent fac3632 commit ced4609

14 files changed

+76
-85
lines changed

tests/android/androidTest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export const androidTest = baseTest.extend<PageTestFixtures & AndroidTestFixture
5757
await run(Number(browserVersion.split('.')[0]));
5858
}, { scope: 'worker' }],
5959

60+
isBidi: [false, { scope: 'worker' }],
6061
isAndroid: [true, { scope: 'worker' }],
6162
isElectron: [false, { scope: 'worker' }],
6263
electronMajorVersion: [0, { scope: 'worker' }],

tests/config/utils.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type { Frame, Page } from 'playwright-core';
17+
import type { Locator, Frame, Page } from 'playwright-core';
1818
import { ZipFile } from '../../packages/playwright-core/lib/server/utils/zipFile';
1919
import type { TraceLoaderBackend } from '../../packages/playwright-core/src/utils/isomorphic/trace/traceLoader';
2020
import type { StackFrame } from '../../packages/protocol/src/channels';
@@ -25,6 +25,8 @@ import { buildActionTree, TraceModel } from '../../packages/playwright-core/src/
2525
import type { ActionTraceEvent, ConsoleMessageTraceEvent, EventTraceEvent, TraceEvent } from '@trace/trace';
2626
import { renderTitleForCall } from '../../packages/playwright-core/lib/utils/isomorphic/protocolFormatter';
2727

28+
export type BoundingBox = Awaited<ReturnType<Locator['boundingBox']>>;
29+
2830
export async function attachFrame(page: Page, frameId: string, url: string): Promise<Frame> {
2931
const handle = await page.evaluateHandle(async ({ frameId, url }) => {
3032
const frame = document.createElement('iframe');
@@ -211,6 +213,36 @@ export function waitForTestLog<T>(page: Page, prefix: string): Promise<T> {
211213
});
212214
}
213215

216+
export async function rafraf(target: Page | Frame, count = 1) {
217+
for (let i = 0; i < count; i++) {
218+
await target.evaluate(async () => {
219+
await new Promise(f => window.builtins.requestAnimationFrame(() => window.builtins.requestAnimationFrame(f)));
220+
});
221+
}
222+
}
223+
224+
export function roundBox(box: BoundingBox): BoundingBox {
225+
return {
226+
x: Math.round(box.x),
227+
y: Math.round(box.y),
228+
width: Math.round(box.width),
229+
height: Math.round(box.height),
230+
};
231+
}
232+
233+
export function unshift(snapshot: string): string {
234+
const lines = snapshot.split('\n');
235+
let whitespacePrefixLength = 100;
236+
for (const line of lines) {
237+
if (!line.trim())
238+
continue;
239+
const match = line.match(/^(\s*)/);
240+
if (match && match[1].length < whitespacePrefixLength)
241+
whitespacePrefixLength = match[1].length;
242+
}
243+
return lines.filter(t => t.trim()).map(line => line.substring(whitespacePrefixLength)).join('\n');
244+
}
245+
214246
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');
215247
export function stripAnsi(str: string): string {
216248
return str.replace(ansiRegex, '');

tests/electron/electronTest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const electronTest = baseTest.extend<TraceViewerFixtures>(traceViewerFixt
3636
browserVersion: [({}, use) => use(process.env.ELECTRON_CHROMIUM_VERSION), { scope: 'worker' }],
3737
browserMajorVersion: [({}, use) => use(Number(process.env.ELECTRON_CHROMIUM_VERSION.split('.')[0])), { scope: 'worker' }],
3838
electronMajorVersion: [({}, use) => use(parseInt(require('electron/package.json').version.split('.')[0], 10)), { scope: 'worker' }],
39+
isBidi: [false, { scope: 'worker' }],
3940
isAndroid: [false, { scope: 'worker' }],
4041
isElectron: [true, { scope: 'worker' }],
4142
isHeadlessShell: [false, { scope: 'worker' }],

tests/library/browsertype-connect.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import * as path from 'path';
2222
import { getUserAgent, getPlaywrightVersion } from '../../packages/playwright-core/lib/server/utils/userAgent';
2323
import WebSocket from 'ws';
2424
import { expect, playwrightTest } from '../config/browserTest';
25-
import { parseTrace, suppressCertificateWarning } from '../config/utils';
25+
import { parseTrace, suppressCertificateWarning, rafraf } from '../config/utils';
2626
import formidable from 'formidable';
2727
import type { Browser, ConnectOptions } from 'playwright-core';
2828
import { createHttpServer } from '../../packages/playwright-core/lib/server/utils/network';
@@ -497,7 +497,7 @@ for (const kind of ['launchServer', 'run-server'] as const) {
497497
});
498498
const page = await context.newPage();
499499
await page.evaluate(() => document.body.style.backgroundColor = 'red');
500-
await new Promise(r => setTimeout(r, 1000));
500+
await rafraf(page, 100);
501501
await context.close();
502502

503503
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36685' });

tests/library/debug-controller.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { createGuid } from '../../packages/playwright-core/lib/server/utils/cryp
2020
import { Backend } from '../config/debugControllerBackend';
2121
import type { Browser, BrowserContext } from '@playwright/test';
2222
import type * as channels from '@protocol/channels';
23-
import { roundBox } from '../page/pageTest';
23+
import { roundBox } from '../config/utils';
2424

2525
type BrowserWithReuse = Browser & { newContextForReuse: () => Promise<BrowserContext> };
2626
type Fixtures = {

tests/library/inspector/cli-codegen-aria.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { test, expect } from './inspectorTest';
18-
import { roundBox } from '../../page/pageTest';
18+
import { roundBox } from '../../config/utils';
1919

2020
test.describe(() => {
2121
test.skip(({ mode }) => mode !== 'default');

tests/library/inspector/cli-codegen-pick-locator.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { test, expect } from './inspectorTest';
18-
import { roundBox } from '../../page/pageTest';
18+
import { roundBox } from '../../config/utils';
1919

2020
test.describe(() => {
2121
test.skip(({ mode }) => mode !== 'default');

tests/library/inspector/pause.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616

1717
import type { Page } from 'playwright-core';
1818
import { test as it, expect, Recorder } from './inspectorTest';
19-
import { waitForTestLog } from '../../config/utils';
20-
import { roundBox } from '../../page/pageTest';
21-
import type { BoundingBox } from '../../page/pageTest';
19+
import { roundBox, waitForTestLog } from '../../config/utils';
20+
import type { BoundingBox } from '../../config/utils';
2221
import { pauseHelper } from './pause-helper';
2322

2423
it('should resume when closing inspector', async ({ page, recorderPageGetter, closeRecorder, mode }) => {

tests/library/trace-viewer.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import type http from 'http';
2626
import { pathToFileURL } from 'url';
2727
import { expect, playwrightTest } from '../config/browserTest';
2828
import type { FrameLocator } from '@playwright/test';
29-
import { rafraf, roundBox } from '../page/pageTest';
30-
import { parseTrace } from '../config/utils';
29+
import { parseTrace, rafraf, roundBox } from '../config/utils';
3130

3231
const test = playwrightTest.extend<TraceViewerFixtures>(traceViewerFixtures);
3332

tests/library/video.spec.ts

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
import { spawnSync } from 'child_process';
1818
import fs from 'fs';
1919
import path from 'path';
20-
import type { Page } from 'playwright-core';
2120
import { PNG, jpegjs } from 'playwright-core/lib/utilsBundle';
2221
import { registry } from '../../packages/playwright-core/lib/server';
2322
import { expect, browserTest as it } from '../config/browserTest';
24-
import { parseTraceRaw } from '../config/utils';
23+
import { parseTraceRaw, rafraf } from '../config/utils';
2524

2625
export class VideoPlayer {
2726
fileName: string;
@@ -171,7 +170,7 @@ it.describe('screencast', () => {
171170
const page = await context.newPage();
172171

173172
await page.evaluate(() => document.body.style.backgroundColor = 'red');
174-
await waitForRafs(page, 100);
173+
await rafraf(page, 100);
175174
await context.close();
176175

177176
const videoFile = await page.video().path();
@@ -196,7 +195,7 @@ it.describe('screencast', () => {
196195
const page = await context.newPage();
197196

198197
await page.evaluate(() => document.body.style.backgroundColor = 'red');
199-
await waitForRafs(page, 100);
198+
await rafraf(page, 100);
200199
await context.close();
201200

202201
const videoFile = await page.video().path();
@@ -226,7 +225,7 @@ it.describe('screencast', () => {
226225
document.body.textContent = ''; // remove link
227226
document.body.style.backgroundColor = 'red';
228227
});
229-
await waitForRafs(page, 100);
228+
await rafraf(page, 100);
230229
await context.close();
231230

232231
const videoFile = await page.video().path();
@@ -294,7 +293,7 @@ it.describe('screencast', () => {
294293
const page = await context.newPage();
295294
const deletePromise = page.video().delete();
296295
await page.evaluate(() => document.body.style.backgroundColor = 'red');
297-
await waitForRafs(page, 100);
296+
await rafraf(page, 100);
298297
await context.close();
299298

300299
const videoPath = await page.video().path();
@@ -385,9 +384,9 @@ it.describe('screencast', () => {
385384
const page = await context.newPage();
386385

387386
await page.goto(server.PREFIX + '/background-color.html#rgb(0,0,0)');
388-
await waitForRafs(page, 100);
387+
await rafraf(page, 100);
389388
await page.goto(server.CROSS_PROCESS_PREFIX + '/background-color.html#rgb(100,100,100)');
390-
await waitForRafs(page, 100);
389+
await rafraf(page, 100);
391390
await context.close();
392391

393392
const videoFile = await page.video().path();
@@ -423,7 +422,7 @@ it.describe('screencast', () => {
423422
const page = await context.newPage();
424423

425424
await page.goto(server.PREFIX + '/rotate-z.html');
426-
await waitForRafs(page, 100);
425+
await rafraf(page, 100);
427426
await context.close();
428427

429428
const videoFile = await page.video().path();
@@ -457,8 +456,8 @@ it.describe('screencast', () => {
457456
]);
458457
await popup.evaluate(() => document.body.style.backgroundColor = 'red');
459458
await Promise.all([
460-
waitForRafs(page, 100),
461-
waitForRafs(popup, 100),
459+
rafraf(page, 100),
460+
rafraf(popup, 100),
462461
]);
463462
await context.close();
464463

@@ -490,11 +489,11 @@ it.describe('screencast', () => {
490489
await page.$eval('.container', container => {
491490
container.firstElementChild.classList.remove('red');
492491
});
493-
await waitForRafs(page, 100);
492+
await rafraf(page, 100);
494493
await page.$eval('.container', container => {
495494
container.firstElementChild.classList.add('red');
496495
});
497-
await waitForRafs(page, 100);
496+
await rafraf(page, 100);
498497
await context.close();
499498

500499
const videoFile = await page.video().path();
@@ -530,7 +529,7 @@ it.describe('screencast', () => {
530529
});
531530

532531
const page = await context.newPage();
533-
await waitForRafs(page, 100);
532+
await rafraf(page, 100);
534533
await context.close();
535534

536535
const videoFile = await page.video().path();
@@ -547,7 +546,7 @@ it.describe('screencast', () => {
547546
});
548547

549548
const page = await context.newPage();
550-
await waitForRafs(page, 100);
549+
await rafraf(page, 100);
551550
await context.close();
552551

553552
const videoFile = await page.video().path();
@@ -567,7 +566,7 @@ it.describe('screencast', () => {
567566
});
568567

569568
const page = await context.newPage();
570-
await waitForRafs(page, 100);
569+
await rafraf(page, 100);
571570
await context.close();
572571

573572
const videoFile = await page.video().path();
@@ -588,7 +587,7 @@ it.describe('screencast', () => {
588587
});
589588

590589
await page.evaluate(() => document.body.style.backgroundColor = 'red');
591-
await waitForRafs(page, 100);
590+
await rafraf(page, 100);
592591
await context.close();
593592

594593
const videoFile = await page.video().path();
@@ -617,7 +616,7 @@ it.describe('screencast', () => {
617616
});
618617

619618
const page = await context.newPage();
620-
await waitForRafs(page, 100);
619+
await rafraf(page, 100);
621620
await context.close();
622621

623622
const videoFile = await page.video().path();
@@ -638,7 +637,7 @@ it.describe('screencast', () => {
638637
});
639638

640639
const page = await context.newPage();
641-
await waitForRafs(page, 100);
640+
await rafraf(page, 100);
642641
await browser.close();
643642

644643
const file = testInfo.outputPath('saved-video-');
@@ -659,7 +658,7 @@ it.describe('screencast', () => {
659658
});
660659

661660
const page = await context.newPage();
662-
await waitForRafs(page, 100);
661+
await rafraf(page, 100);
663662
await (browser as any)._channel.killForTests();
664663

665664
const file = testInfo.outputPath('saved-video-');
@@ -681,7 +680,7 @@ it.describe('screencast', () => {
681680
});
682681

683682
const page = await context.newPage();
684-
await waitForRafs(page, 100);
683+
await rafraf(page, 100);
685684
await page.close();
686685
await context.close();
687686
await browser.close();
@@ -709,7 +708,7 @@ it.describe('screencast', () => {
709708

710709
const page = await context.newPage();
711710
await page.goto(server.EMPTY_PAGE);
712-
await waitForRafs(page, 100);
711+
await rafraf(page, 100);
713712

714713
const cookies = await context.cookies();
715714
expect(cookies.length).toBe(1);
@@ -738,7 +737,7 @@ it.describe('screencast', () => {
738737

739738
const page = await context.newPage();
740739
await page.setContent(`<div style='margin: 0; background: red; position: fixed; right:0; bottom:0; width: 30; height: 30;'></div>`);
741-
await waitForRafs(page, 100);
740+
await rafraf(page, 100);
742741
await page.close();
743742
await context.close();
744743
await browser.close();
@@ -775,7 +774,7 @@ it.describe('screencast', () => {
775774

776775
const page = await context.newPage();
777776
await page.setContent(`<div style='margin: 0; background: red; position: fixed; right:0; bottom:0; width: 30; height: 30;'></div>`);
778-
await waitForRafs(page, 100);
777+
await rafraf(page, 100);
779778
await page.close();
780779
await context.close();
781780
await browser.close();
@@ -811,7 +810,7 @@ it.describe('screencast', () => {
811810
const page = await context.newPage();
812811

813812
await page.evaluate(() => document.body.style.backgroundColor = 'red');
814-
await waitForRafs(page, 100);
813+
await rafraf(page, 100);
815814
await context.tracing.stop({ path: traceFile });
816815
await context.close();
817816

@@ -849,23 +848,10 @@ it('should saveAs video', async ({ browser }, testInfo) => {
849848
});
850849
const page = await context.newPage();
851850
await page.evaluate(() => document.body.style.backgroundColor = 'red');
852-
await waitForRafs(page, 100);
851+
await rafraf(page, 100);
853852
await context.close();
854853

855854
const saveAsPath = testInfo.outputPath('my-video.webm');
856855
await page.video().saveAs(saveAsPath);
857856
expect(fs.existsSync(saveAsPath)).toBeTruthy();
858857
});
859-
860-
async function waitForRafs(page: Page, count: number): Promise<void> {
861-
await page.evaluate(count => new Promise<void>(resolve => {
862-
const onRaf = () => {
863-
--count;
864-
if (!count)
865-
resolve();
866-
else
867-
window.builtins.requestAnimationFrame(onRaf);
868-
};
869-
window.builtins.requestAnimationFrame(onRaf);
870-
}), count);
871-
}

0 commit comments

Comments
 (0)