Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/outofprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { Playwright } from './client/playwright';
import * as childProcess from 'child_process';
import * as path from 'path';

export async function start() {
const client = new PlaywrightClient();
export async function start(env: any = {}) {
const client = new PlaywrightClient(env);
const playwright = await client._playwright;
(playwright as any).stop = () => client.stop();
(playwright as any).driverProcess = client._driverProcess;
Expand All @@ -34,14 +34,18 @@ class PlaywrightClient {
private _closePromise: Promise<void>;
private _onExit: (exitCode: number | null, signal: string | null) => {};

constructor() {
constructor(env: any) {
this._onExit = (exitCode: number | null, signal: string | null) => {
throw new Error(`Server closed with exitCode=${exitCode} signal=${signal}`);
};

this._driverProcess = childProcess.fork(path.join(__dirname, 'cli', 'cli.js'), ['run-driver'], {
stdio: 'pipe',
detached: true,
env: {
...process.env,
...env
},
});
this._driverProcess.unref();
this._driverProcess.on('exit', this._onExit);
Expand Down
4 changes: 3 additions & 1 deletion src/server/chromium/chromium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
import { CRBrowser } from './crBrowser';
import { Env } from '../../utils/processLauncher';
import { Env, gracefullyCloseSet } from '../../utils/processLauncher';
import { kBrowserCloseMessageId } from './crConnection';
import { rewriteErrorMessage } from '../../utils/stackTrace';
import { BrowserType } from '../browserType';
Expand Down Expand Up @@ -169,7 +169,9 @@ export class Chromium extends BrowserType {
method: 'DELETE',
}).catch(error => progress.log(`<error disconnecting from selenium>: ${error}`));
progress.log(`<disconnected from selenium> sessionId=${sessionId}`);
gracefullyCloseSet.delete(disconnectFromSelenium);
};
gracefullyCloseSet.add(disconnectFromSelenium);

try {
const capabilities = value.capabilities;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/processLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type LaunchResult = {
kill: () => Promise<void>,
};

const gracefullyCloseSet = new Set<() => Promise<void>>();
export const gracefullyCloseSet = new Set<() => Promise<void>>();

export async function gracefullyCloseAll() {
await Promise.all(Array.from(gracefullyCloseSet).map(gracefullyClose => gracefullyClose().catch(e => {})));
Expand Down
30 changes: 30 additions & 0 deletions tests/browsertype-launch-selenium.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { playwrightTest as test, expect } from './config/browserTest';
import type { TestInfo } from '../types/test';
import path from 'path';
import fs from 'fs';
import { start } from '../lib/outofprocess';

const chromeDriver = require('chromedriver').path;
const brokenDriver = path.join(__dirname, 'assets', 'selenium-grid', 'broken-selenium-driver.js');
Expand All @@ -34,6 +35,7 @@ function writeSeleniumConfig(testInfo: TestInfo, port: number) {

test.skip(({ mode }) => mode !== 'default', 'Using test hooks');
test.skip(() => !!process.env.INSIDE_DOCKER, 'Docker image does not have Java');
test.slow();

test('selenium grid 3.141.59 standalone chromium', async ({ browserOptions, browserName, childProcess, waitForPort, browserType }, testInfo) => {
test.skip(browserName !== 'chromium');
Expand Down Expand Up @@ -105,3 +107,31 @@ test('selenium grid 3.141.59 standalone non-chromium', async ({ browserName, bro
const error = await browserType.launch({ __testHookSeleniumRemoteURL } as any).catch(e => e);
expect(error.message).toContain('Connecting to SELENIUM_REMOTE_URL is only supported by Chromium');
});

test('selenium grid 3.141.59 standalone chromium through driver', async ({ browserOptions, browserName, childProcess, waitForPort }, testInfo) => {
test.skip(browserName !== 'chromium');

const port = testInfo.workerIndex + 15123;
const grid = childProcess({
command: ['java', `-Dwebdriver.chrome.driver=${chromeDriver}`, '-jar', standalone_3_141_59, '-config', writeSeleniumConfig(testInfo, port)],
cwd: __dirname,
});
await waitForPort(port);

const pw = await start({
SELENIUM_REMOTE_URL: `http://localhost:${port}/wd/hub`,
});
const browser = await pw.chromium.launch(browserOptions);
const page = await browser.newPage();
await page.setContent('<title>Hello world</title><div>Get Started</div>');
await page.click('text=Get Started');
await expect(page).toHaveTitle('Hello world');
// Note: it is important to stop the driver without explicitly closing the browser.
// It should terminate selenium session in this case.
await pw.stop();

expect(grid.output).toContain('Starting ChromeDriver');
expect(grid.output).toContain('Started new session');
// It is important that selenium session is terminated.
await grid.waitForOutput('Removing session');
});
2 changes: 1 addition & 1 deletion tests/config/commonFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const commonFixtures: Fixtures<CommonFixtures, {}> = {
return process;
});
await Promise.all(processes.map(child => child.close()));
if (testInfo.status !== 'passed' && !process.env.PW_RUNNER_DEBUG) {
if (testInfo.status !== 'passed' && !process.env.PWTEST_DEBUG) {
for (const process of processes) {
console.log('====== ' + process.params.command.join(' '));
console.log(process.output);
Expand Down