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: reduce flakiness in a few tests (#40200)
Dmitry Gozman0aae124cd012
diff --git a/tests/library/capabilities.spec.ts b/tests/library/capabilities.spec.tsindex ed1c47e121..84d061fadf 100644--- a/tests/library/capabilities.spec.ts+++ b/tests/library/capabilities.spec.ts@@ -97,9 +97,9 @@ it('should play audio @smoke', async ({ page, server, browserName, platform }) =await page.goto(server.EMPTY_PAGE);await page.setContent(`<audio src="${server.PREFIX}/example.mp3"></audio>`);await page.$eval('audio', e => e.play());- await page.waitForTimeout(1000);+ await page.waitForTimeout(3000);await page.$eval('audio', e => e.pause());- expect(await page.$eval('audio', e => e.currentTime)).toBeGreaterThan(0.2);+ expect(await page.$eval('audio', e => e.currentTime)).toBeGreaterThan(0.1);});it('should support webgl @smoke', async ({ page }) => {diff --git a/tests/library/chromium/extensions.spec.ts b/tests/library/chromium/extensions.spec.tsindex f1015763ed..c27fd1cdfe 100644--- a/tests/library/chromium/extensions.spec.ts+++ b/tests/library/chromium/extensions.spec.ts@@ -56,28 +56,32 @@ it.describe('MV3', () => {const page = await context.newPage();const cdp = await context.newCDPSession(page);- let versionId: string | undefined;- let scopeURL: string | undefined;- let runningStatus: string | undefined;- cdp.on('ServiceWorker.workerVersionUpdated', ({ versions }: any) => {- const v = versions[0];- if (!v)- return;- versionId = v.versionId;- runningStatus = v.runningStatus;- });- cdp.on('ServiceWorker.workerRegistrationUpdated', ({ registrations }: any) => {- if (registrations.length)- scopeURL = registrations[0].scopeURL;- });+ const waitForCdpEvent = <T>(event: string, predicate: (params: any) => T | undefined): Promise<T> => {+ return new Promise<T>(resolve => {+ const handler = (params: any) => {+ const result = predicate(params);+ if (result !== undefined) {+ cdp.off(event as any, handler);+ resolve(result);+ }+ };+ cdp.on(event as any, handler);+ });+ };++ const versionPromise = waitForCdpEvent('ServiceWorker.workerVersionUpdated', ({ versions }: any) => versions[0]?.versionId as string | undefined);+ const scopePromise = waitForCdpEvent('ServiceWorker.workerRegistrationUpdated', ({ registrations }: any) => registrations[0]?.scopeURL as string | undefined);await cdp.send('ServiceWorker.enable');- await expect.poll(() => versionId && scopeURL, { timeout: 5000 }).toBeTruthy();+ const versionId = await versionPromise;+ const scopeURL = await scopePromise;+ const stoppedPromise = waitForCdpEvent('ServiceWorker.workerVersionUpdated', ({ versions }: any) => versions[0]?.runningStatus === 'stopped' ? true : undefined);await cdp.send('ServiceWorker.stopWorker', { versionId });- // Wait for full stop before triggering restart.- await expect.poll(() => runningStatus, { timeout: 5000 }).toBe('stopped');+ await stoppedPromise;++ const runningPromise = waitForCdpEvent('ServiceWorker.workerVersionUpdated', ({ versions }: any) => versions[0]?.runningStatus === 'running' ? true : undefined);await cdp.send('ServiceWorker.startWorker', { scopeURL });- await expect.poll(() => runningStatus, { timeout: 5000 }).toBe('running');+ await runningPromise;const startTime2 = await sw1.evaluate(() => (globalThis as any).startTime);expect(startTime2).toBeGreaterThan(startTime1);diff --git a/tests/playwright-test/ui-mode-test-network-tab.spec.ts b/tests/playwright-test/ui-mode-test-network-tab.spec.tsindex 3a3cd2fad6..7c1d8cce12 100644--- a/tests/playwright-test/ui-mode-test-network-tab.spec.ts+++ b/tests/playwright-test/ui-mode-test-network-tab.spec.ts@@ -456,30 +456,6 @@ test('should copy network request', async ({ runUITest, server }) => {});-test('should not preserve selection across test runs', async ({ runUITest, server }) => {- const { page } = await runUITest({- 'network-tab.test.ts': `- import { test, expect } from '@playwright/test';- test('network tab test', async ({ page }) => {- await page.goto('${server.PREFIX}/network-tab/network.html');- });- `,- });-- await page.getByRole('treeitem', { name: 'network tab test' }).dblclick();- await expect(page.getByTestId('workbench-run-status')).toContainText('Passed');-- await page.getByRole('tab', { name: 'Network' }).click();- await page.getByRole('listitem').filter({ hasText: 'network.html' }).click();- const headersPanel = page.getByRole('tabpanel', { name: 'Headers' });- await expect(headersPanel).toBeVisible();-- await page.getByRole('treeitem', { name: 'network tab test' }).dblclick();- await expect(headersPanel).toBeHidden();- await expect(page.getByTestId('workbench-run-status')).toContainText('Passed');- await expect(headersPanel).toBeHidden();-});-test('should preserve selection during test run', async ({ runUITest, server }, testInfo) => {const { page } = await runUITest({'network-tab.test.ts': `