Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

test: fix a few tests (#39840)

Dmitry Gozman1f2d8798b131
diff --git a/packages/playwright-core/src/server/firefox/ffPage.ts b/packages/playwright-core/src/server/firefox/ffPage.ts
index ef3f391190..05749a349c 100644
--- a/packages/playwright-core/src/server/firefox/ffPage.ts
+++ b/packages/playwright-core/src/server/firefox/ffPage.ts
@@ -477,7 +477,7 @@ export class FFPage implements PageDelegate {
}
async startScreencast(options: { width: number, height: number, quality: number }): Promise<void> {
- await this._session.send('Page.startScreencast', options);
+ await this._session.send('Page.startScreencast', { width: options.width, height: options.height, quality: options.quality });
}
async stopScreencast(): Promise<void> {
diff --git a/tests/config/commonFixtures.ts b/tests/config/commonFixtures.ts
index 126759165a..61bf148b20 100644
--- a/tests/config/commonFixtures.ts
+++ b/tests/config/commonFixtures.ts
@@ -19,7 +19,7 @@ import type { ChildProcess } from 'child_process';
import { execSync, spawn } from 'child_process';
import net from 'net';
import fs from 'fs';
-import { stripAnsi } from './utils';
+import { inheritAndCleanEnv, stripAnsi } from './utils';
type TestChildParams = {
command: string[],
@@ -111,10 +111,7 @@ export class TestChildProcess {
const command = params.shell ? params.command.join(' ') : params.command[0];
const args = params.shell ? [] : params.command.slice(1);
this.process = spawn(command, args, {
- env: {
- ...process.env,
- ...params.env,
- },
+ env: inheritAndCleanEnv(params.env),
cwd: params.cwd,
shell: params.shell,
// On non-windows platforms, `detached: true` makes child process a leader of a new
diff --git a/tests/config/remoteServer.ts b/tests/config/remoteServer.ts
index 6d5f0cc452..c2989da4fd 100644
--- a/tests/config/remoteServer.ts
+++ b/tests/config/remoteServer.ts
@@ -15,6 +15,7 @@
*/
import path from 'path';
+import { inheritAndCleanEnv } from './utils';
import type { BrowserType, Browser } from 'playwright-core';
import type { CommonFixtures, TestChildProcess } from './commonFixtures';
@@ -35,10 +36,7 @@ export class RunServer implements PlaywrightServer {
command.push(`--artifacts-dir=${options.artifactsDir}`);
this._process = childProcess({
command,
- env: {
- ...process.env,
- ...options?.env,
- },
+ env: inheritAndCleanEnv(options?.env),
});
let wsEndpointCallback: (value: string) => void;
diff --git a/tests/config/utils.ts b/tests/config/utils.ts
index ffa2a0f0fb..d3a58ab54f 100644
--- a/tests/config/utils.ts
+++ b/tests/config/utils.ts
@@ -229,3 +229,35 @@ const ansiRegex = new RegExp('[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(
export function stripAnsi(str: string): string {
return str.replace(ansiRegex, '');
}
+
+export function inheritAndCleanEnv(env: NodeJS.ProcessEnv | undefined): NodeJS.ProcessEnv {
+ return {
+ ...process.env,
+ // BEGIN: Reserved CI
+ CI: undefined,
+ BUILD_URL: undefined,
+ CI_COMMIT_SHA: undefined,
+ CI_JOB_URL: undefined,
+ CI_PROJECT_URL: undefined,
+ GITHUB_ACTIONS: undefined,
+ GITHUB_REPOSITORY: undefined,
+ GITHUB_RUN_ID: undefined,
+ GITHUB_SERVER_URL: undefined,
+ GITHUB_SHA: undefined,
+ GITHUB_EVENT_PATH: undefined,
+ // END: Reserved CI
+ PW_TEST_HTML_REPORT_OPEN: undefined,
+ PLAYWRIGHT_HTML_OPEN: undefined,
+ PW_TEST_DEBUG_REPORTERS: undefined,
+ PW_TEST_REPORTER: undefined,
+ PW_TEST_REPORTER_WS_ENDPOINT: undefined,
+ PW_TEST_SOURCE_TRANSFORM: undefined,
+ PW_TEST_SOURCE_TRANSFORM_SCOPE: undefined,
+ PWTEST_BOT_NAME: undefined,
+ PWTEST_SHARD_WEIGHTS: undefined,
+ TEST_WORKER_INDEX: undefined,
+ TEST_PARALLEL_INDEX: undefined,
+ NODE_OPTIONS: undefined,
+ ...env,
+ };
+}
diff --git a/tests/electron/electronTest.ts b/tests/electron/electronTest.ts
index b1aa506fd3..edb57bc34c 100644
--- a/tests/electron/electronTest.ts
+++ b/tests/electron/electronTest.ts
@@ -23,6 +23,7 @@ import type { PageTestFixtures, PageWorkerFixtures } from '../page/pageTestApi';
import type { TraceViewerFixtures } from '../config/traceViewerFixtures';
import { traceViewerFixtures } from '../config/traceViewerFixtures';
import { removeFolders } from '../../packages/playwright-core/lib/server/utils/fileUtils';
+import { inheritAndCleanEnv } from '../config/utils';
export { expect } from '@playwright/test';
type ElectronTestFixtures = PageTestFixtures & {
@@ -63,10 +64,7 @@ export const electronTest = baseTest.extend<TraceViewerFixtures>(traceViewerFixt
const app = await playwright._electron.launch({
...options,
args: [path.join(__dirname, appFile), ...args],
- env: {
- ...process.env,
- PWTEST_ELECTRON_USER_DATA_DIR: userDataDir,
- }
+ env: inheritAndCleanEnv({ PWTEST_ELECTRON_USER_DATA_DIR: userDataDir }),
});
apps.push(app);
return app;
diff --git a/tests/library/browsercontext-locale.spec.ts b/tests/library/browsercontext-locale.spec.ts
index 3646d68845..ea0dbb0662 100644
--- a/tests/library/browsercontext-locale.spec.ts
+++ b/tests/library/browsercontext-locale.spec.ts
@@ -171,13 +171,13 @@ it('should propagate locale to workers', async ({ browser, browserName, server }
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
const [msg] = await Promise.all([
- page.waitForEvent('console'),
- page.evaluate(() => new Worker(URL.createObjectURL(new Blob(['console.log(Intl.NumberFormat().resolvedOptions().locale)'], { type: 'application/javascript' })))),
+ page.waitForEvent('console', e => e.text().startsWith('locale:')),
+ page.evaluate(() => new Worker(URL.createObjectURL(new Blob(['console.log("locale:" + Intl.NumberFormat().resolvedOptions().locale)'], { type: 'application/javascript' })))),
]);
if (browserName === 'webkit')
- expect(msg.text()).toContain('ru'); // Webkit on Ubuntu is "ru-RU", and on other platforms is "ru"
+ expect(msg.text()).toContain('locale:ru'); // Webkit on Ubuntu is "ru-RU", and on other platforms is "ru"
else
- expect(msg.text()).toBe('ru-RU');
+ expect(msg.text()).toBe('locale:ru-RU');
await context.close();
});
diff --git a/tests/library/browsercontext-reuse.spec.ts b/tests/library/browsercontext-reuse.spec.ts
index 3b72ce0262..dc92fc41c4 100644
--- a/tests/library/browsercontext-reuse.spec.ts
+++ b/tests/library/browsercontext-reuse.spec.ts
@@ -371,7 +371,7 @@ for (const scenario of ['launch', 'connect'] as const) {
});
server.onMessage(message => ws.send(message));
});
- await page.goto('about:blank');
+ await page.goto(server.EMPTY_PAGE);
await page.evaluate(host => {
window.log = [];
(window as any).ws1 = new WebSocket('ws://' + host + '/ws1');
diff --git a/tests/library/firefox/launcher.spec.ts b/tests/library/firefox/launcher.spec.ts
index 1fe1de3e20..8c081550e6 100644
--- a/tests/library/firefox/launcher.spec.ts
+++ b/tests/library/firefox/launcher.spec.ts
@@ -17,6 +17,7 @@
import fs from 'fs';
import { playwrightTest as it, expect } from '../../config/browserTest';
import { TestServer } from '../../config/testserver';
+import { inheritAndCleanEnv } from '../../config/utils';
it('should pass firefox user preferences', async ({ browserType, mode }) => {
it.skip(mode.startsWith('service'));
@@ -67,7 +68,7 @@ it('should support custom firefox policies', async ({ browserType, mode, asset,
await server.waitUntilReady();
const browser = await browserType.launch({
- env: { ...process.env, 'PLAYWRIGHT_FIREFOX_POLICIES_JSON': policiesPath },
+ env: inheritAndCleanEnv({ 'PLAYWRIGHT_FIREFOX_POLICIES_JSON': policiesPath }),
});
const page = await browser.newPage();
diff --git a/tests/library/launcher.spec.ts b/tests/library/launcher.spec.ts
index 049e9e1834..8c2c37a9ef 100644
--- a/tests/library/launcher.spec.ts
+++ b/tests/library/launcher.spec.ts
@@ -15,6 +15,7 @@
* limitations under the License.
*/
+import { inheritAndCleanEnv } from '../config/utils';
import { playwrightTest as it, expect } from '../config/browserTest';
it('should have an errors object', async ({ playwright }) => {
@@ -49,10 +50,7 @@ it('should throw a friendly error if its headed and there is no xserver on linux
const error: Error = await browserType.launch({
headless: false,
- env: {
- ...process.env,
- DISPLAY: undefined,
- },
+ env: inheritAndCleanEnv({ DISPLAY: undefined }),
}).catch(e => e);
expect(error).toBeInstanceOf(Error);
expect(error.message).toMatch(/Looks like you launched a headed browser without having a XServer running./);
diff --git a/tests/library/route-web-socket.spec.ts b/tests/library/route-web-socket.spec.ts
index dc0d31cd4e..67438cf21a 100644
--- a/tests/library/route-web-socket.spec.ts
+++ b/tests/library/route-web-socket.spec.ts
@@ -258,7 +258,7 @@ test('should pattern match', async ({ page, server }) => {
const wsPromise = server.waitForWebSocket();
- await page.goto('about:blank');
+ await page.goto(server.EMPTY_PAGE);
await page.evaluate(async ({ host }) => {
window.log = [];
(window as any).ws1 = new WebSocket('ws://' + host + '/ws');
@@ -475,7 +475,7 @@ test('should route on context', async ({ page, server }) => {
});
});
- await page.goto('about:blank');
+ await page.goto(server.EMPTY_PAGE);
await page.evaluate(({ host }) => {
window.log = [];
(window as any).ws1 = new WebSocket('ws://' + host + '/ws1');
@@ -542,7 +542,7 @@ test('should work with no trailing slash', async ({ page, server }) => {
});
});
- await page.goto('about:blank');
+ await page.goto(server.EMPTY_PAGE);
await page.evaluate(({ host }) => {
window.log = [];
// No trailing slash!
diff --git a/tests/mcp/cli-fixtures.ts b/tests/mcp/cli-fixtures.ts
index b947ad95fd..14862722bf 100644
--- a/tests/mcp/cli-fixtures.ts
+++ b/tests/mcp/cli-fixtures.ts
@@ -21,6 +21,7 @@ import { chromium } from 'playwright-core';
import { test as baseTest } from './fixtures';
import { killProcessGroup } from '../config/commonFixtures';
+import { inheritAndCleanEnv } from '../config/utils';
import type { Page } from 'playwright-core';
import type { CommonFixtures } from '../config/commonFixtures';
@@ -101,14 +102,12 @@ async function runCli(childProcess: CommonFixtures['childProcess'], args: string
const cli = childProcess({
command: [process.execPath, require.resolve('../../packages/playwright-core/lib/tools/cli-client/cli.js'), ...args],
cwd: cliOptions.cwd ?? testInfo.outputPath(),
- env: {
- ...process.env,
- ...cliOptions.env,
+ env: inheritAndCleanEnv({
...cliEnv(),
PLAYWRIGHT_MCP_BROWSER: options.mcpBrowser,
PLAYWRIGHT_MCP_HEADLESS: String(options.mcpHeadless),
...cliOptions.env,
- },
+ }),
});
await cli.exited.finally(async () => {
await testInfo.attach(stepTitle, { body: cli.output, contentType: 'text/plain' });
diff --git a/tests/mcp/fixtures.ts b/tests/mcp/fixtures.ts
index d4b8edf57f..4b7ad68199 100644
--- a/tests/mcp/fixtures.ts
+++ b/tests/mcp/fixtures.ts
@@ -26,6 +26,7 @@ import { TestServer } from '../config/testserver';
import { serverFixtures } from '../config/serverFixtures';
import { parseResponse } from '../../packages/playwright-core/lib/tools/backend/response';
import { commonFixtures } from '../config/commonFixtures';
+import { inheritAndCleanEnv } from '../config/utils';
import type { CommonFixtures, CommonWorkerFixtures } from '../config/commonFixtures';
import type { Config } from '../../packages/playwright-core/src/tools/mcp/config.d';
@@ -134,11 +135,10 @@ export const test = serverTest.extend<TestFixtures & TestOptions, WorkerFixtures
};
});
}
- const env = {
- ...process.env,
+ const env = inheritAndCleanEnv({
PW_TMPDIR_FOR_TEST: testInfo.outputPath('tmp'),
...options?.env
- };
+ });
const { transport, stderr } = await createTransport(mcpServerType, { args, env, cwd: options?.cwd || test.info().outputPath() });
let stderrBuffer = '';
stderr?.on('data', data => {
diff --git a/tests/mcp/http.spec.ts b/tests/mcp/http.spec.ts
index dae97466b2..6b6cd4fe01 100644
--- a/tests/mcp/http.spec.ts
+++ b/tests/mcp/http.spec.ts
@@ -21,6 +21,7 @@ import { ChildProcess, spawn } from 'child_process';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { test as baseTest, expect, mcpServerPath, formatLog } from './fixtures';
+import { inheritAndCleanEnv } from '../config/utils';
import type { Config } from '../../packages/playwright-core/src/tools/mcp/config.d';
import { ListRootsRequestSchema } from 'playwright-core/lib/mcpBundle';
@@ -41,12 +42,11 @@ const test = baseTest.extend<{ serverEndpoint: (options?: { args?: string[], noP
...(options?.args || []),
], {
stdio: 'pipe',
- env: {
- ...process.env,
+ env: inheritAndCleanEnv({
DEBUG: 'pw:mcp:test',
DEBUG_COLORS: '0',
DEBUG_HIDE_DATE: '1',
- },
+ }),
cwd: testInfo.outputPath(),
});
let stderr = '';
diff --git a/tests/mcp/sse.spec.ts b/tests/mcp/sse.spec.ts
index 6c971c3510..753b233d28 100644
--- a/tests/mcp/sse.spec.ts
+++ b/tests/mcp/sse.spec.ts
@@ -20,6 +20,7 @@ import { ChildProcess, spawn } from 'child_process';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { test as baseTest, expect, mcpServerPath, formatLog } from './fixtures';
+import { inheritAndCleanEnv } from '../config/utils';
import type { Config } from '../../packages/playwright-core/src/tools/mcp/config.d';
@@ -39,12 +40,11 @@ const test = baseTest.extend<{ serverEndpoint: (options?: { args?: string[], noP
...(options?.args || []),
], {
stdio: 'pipe',
- env: {
- ...process.env,
+ env: inheritAndCleanEnv({
DEBUG: 'pw:mcp:test',
DEBUG_COLORS: '0',
DEBUG_HIDE_DATE: '1',
- },
+ }),
cwd: testInfo.outputPath(),
});
let stderr = '';
diff --git a/tests/page/page-navigation.spec.ts b/tests/page/page-navigation.spec.ts
index 01932d6b6f..610db55f97 100644
--- a/tests/page/page-navigation.spec.ts
+++ b/tests/page/page-navigation.spec.ts
@@ -38,19 +38,22 @@ it('should work with _blank target in form', async ({ page, server }) => {
server.setRoute('/done.html?', (req, res) => {
res.end(`Done`);
});
+ server.setRoute('/done.html', (req, res) => {
+ res.end(`Done`);
+ });
await page.goto(server.EMPTY_PAGE);
- void page.setContent(`<form target="_blank" action="done.html" >
- <input type="submit" value="Click me">
- </form>`);
+ await page.setContent(`<form target="_blank" action="done.html" >
+ <input type="submit" value="Click me">
+ </form>`);
await Promise.all([
page.waitForEvent('popup'),
page.click('"Click me"')
]);
- void page.setContent(`<form target="_blank" action="done.html" method="post">
- <input type="submit" value="Click me">
- </form>`);
+ await page.setContent(`<form target="_blank" action="done.html" method="post">
+ <input type="submit" value="Click me">
+ </form>`);
await Promise.all([
page.waitForEvent('popup'),
page.click('"Click me"')
diff --git a/tests/playwright-test/playwright-test-fixtures.ts b/tests/playwright-test/playwright-test-fixtures.ts
index 5410698a50..f2e6826ec7 100644
--- a/tests/playwright-test/playwright-test-fixtures.ts
+++ b/tests/playwright-test/playwright-test-fixtures.ts
@@ -27,6 +27,7 @@ import { serverFixtures } from '../config/serverFixtures';
import type { TestInfo } from './stable-test-runner';
import { expect } from './stable-test-runner';
import { test as base } from './stable-test-runner';
+import { inheritAndCleanEnv } from '../config/utils';
export { countTimes } from '../config/commonFixtures';
type CliRunResult = {
@@ -132,7 +133,7 @@ function startPlaywrightTest(childProcess: CommonFixtures['childProcess'], baseD
function startPlaywrightChildProcess(childProcess: CommonFixtures['childProcess'], baseDir: string, args: string[], env: NodeJS.ProcessEnv, options: RunOptions): TestChildProcess {
return childProcess({
command: ['node', cliEntrypoint, ...args],
- env: cleanEnv(env),
+ env: inheritAndCleanEnv(env),
cwd: options.cwd ? path.resolve(baseDir, options.cwd) : baseDir,
});
}
@@ -207,45 +208,13 @@ async function runPlaywrightTest(childProcess: CommonFixtures['childProcess'], b
async function runPlaywrightCLI(childProcess: CommonFixtures['childProcess'], args: string[], baseDir: string, env: NodeJS.ProcessEnv): Promise<{ output: string, stdout: string, stderr: string, exitCode: number }> {
const testProcess = childProcess({
command: ['node', cliEntrypoint, ...args],
- env: cleanEnv(env),
+ env: inheritAndCleanEnv(env),
cwd: baseDir,
});
const { exitCode } = await testProcess.exited;
return { exitCode, output: testProcess.output, stdout: testProcess.stdout, stderr: testProcess.stderr };
}
-export function cleanEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
- return {
- ...process.env,
- // BEGIN: Reserved CI
- CI: undefined,
- BUILD_URL: undefined,
- CI_COMMIT_SHA: undefined,
- CI_JOB_URL: undefined,
- CI_PROJECT_URL: undefined,
- GITHUB_ACTIONS: undefined,
- GITHUB_REPOSITORY: undefined,
- GITHUB_RUN_ID: undefined,
- GITHUB_SERVER_URL: undefined,
- GITHUB_SHA: undefined,
- GITHUB_EVENT_PATH: undefined,
- // END: Reserved CI
- PW_TEST_HTML_REPORT_OPEN: undefined,
- PLAYWRIGHT_HTML_OPEN: undefined,
- PW_TEST_DEBUG_REPORTERS: undefined,
- PW_TEST_REPORTER: undefined,
- PW_TEST_REPORTER_WS_ENDPOINT: undefined,
- PW_TEST_SOURCE_TRANSFORM: undefined,
- PW_TEST_SOURCE_TRANSFORM_SCOPE: undefined,
- PWTEST_BOT_NAME: undefined,
- PWTEST_SHARD_WEIGHTS: undefined,
- TEST_WORKER_INDEX: undefined,
- TEST_PARALLEL_INDEX: undefined,
- NODE_OPTIONS: undefined,
- ...env,
- };
-}
-
export type RunOptions = {
additionalArgs?: string[];
cwd?: string,
@@ -357,7 +326,7 @@ export const test = base
const cwd = options.cwd ? path.resolve(test.info().outputDir, options.cwd) : test.info().outputDir;
const testProcess = childProcess({
command,
- env: cleanEnv(env),
+ env: inheritAndCleanEnv(env),
cwd,
});
const { exitCode } = await testProcess.exited;
diff --git a/tests/playwright-test/ui-mode-fixtures.ts b/tests/playwright-test/ui-mode-fixtures.ts
index 7fd173e3f5..25f307d093 100644
--- a/tests/playwright-test/ui-mode-fixtures.ts
+++ b/tests/playwright-test/ui-mode-fixtures.ts
@@ -18,11 +18,12 @@ import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import type { TestChildProcess } from '../config/commonFixtures';
-import { cleanEnv, cliEntrypoint, test as base, writeFiles, removeFolders } from './playwright-test-fixtures';
+import { cliEntrypoint, test as base, writeFiles, removeFolders } from './playwright-test-fixtures';
import type { Files, RunOptions } from './playwright-test-fixtures';
import type { Browser, Page, TestInfo } from './stable-test-runner';
import { chromium } from './stable-test-runner';
import { createGuid } from '../../packages/playwright-core/src/server/utils/crypto';
+import { inheritAndCleanEnv } from '../config/utils';
type Latch = {
blockingCode: string;
@@ -97,13 +98,13 @@ export const test = base
const baseDir = await writeFiles(testInfo, files, true);
testProcess = childProcess({
command: ['node', cliEntrypoint, 'test', (options.useWeb ? '--ui-host=127.0.0.1' : '--ui'), '--workers=1', ...(options.additionalArgs || [])],
- env: {
- ...cleanEnv(env),
+ env: inheritAndCleanEnv({
+ ...env,
PWTEST_UNDER_TEST: '1',
PWTEST_CACHE_DIR: cacheDir,
PWTEST_HEADED_FOR_TEST: headless ? '0' : '1',
PWTEST_PRINT_WS_ENDPOINT: '1',
- },
+ }),
cwd: options.cwd ? path.resolve(baseDir, options.cwd) : baseDir,
});
let page: Page;