Skip to content

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

chore: test storage plumbing for workers (#38482)

Pavel Feldmane8b6d726bfcb
diff --git a/packages/playwright/src/common/ipc.ts b/packages/playwright/src/common/ipc.ts
index 4d9ca7f846..ddbfeeb8bd 100644
--- a/packages/playwright/src/common/ipc.ts
+++ b/packages/playwright/src/common/ipc.ts
@@ -91,6 +91,17 @@ export type TestPausedPayload = {
errors: TestInfoErrorImpl[];
};
+export type GetStorageValuePayload = {
+ fileName: string;
+ key: string;
+};
+
+export type SetStorageValuePayload = {
+ fileName: string;
+ key: string;
+ value: any;
+};
+
export type CustomMessageRequestPayload = {
testId: string;
request: any;
diff --git a/packages/playwright/src/common/process.ts b/packages/playwright/src/common/process.ts
index 904290406d..80fddc4a41 100644
--- a/packages/playwright/src/common/process.ts
+++ b/packages/playwright/src/common/process.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import { setTimeOrigin, startProfiling, stopProfiling } from 'playwright-core/lib/utils';
+import { ManualPromise, setTimeOrigin, startProfiling, stopProfiling } from 'playwright-core/lib/utils';
import { serializeError } from '../util';
@@ -41,6 +41,14 @@ export class ProcessRunner {
const response: ProtocolResponse = { method, params };
sendMessageToParent({ method: '__dispatch__', params: response });
}
+
+ protected async sendRequest(method: string, params?: any): Promise<any> {
+ return await sendRequestToParent(method, params);
+ }
+
+ protected async sendMessageNoReply(method: string, params?: any) {
+ void sendRequestToParent(method, params).catch(() => {});
+ }
}
let gracefullyCloseCalled = false;
@@ -84,6 +92,8 @@ process.on('message', async (message: any) => {
sendMessageToParent({ method: '__dispatch__', params: response });
}
}
+ if (message.method === '__response__')
+ handleResponseFromParent(message.params as ProtocolResponse);
});
const kForceExitTimeout = +(process.env.PWTEST_FORCE_EXIT_TIMEOUT || 30000);
@@ -120,3 +130,25 @@ function sendMessageToParent(message: { method: string, params?: any }) {
// Can throw when closing.
}
}
+
+let lastId = 0;
+const requestCallbacks = new Map<number, ManualPromise<any>>();
+
+async function sendRequestToParent(method: string, params?: any): Promise<any> {
+ const id = ++lastId;
+ sendMessageToParent({ method: '__request__', params: { id, method, params } });
+ const promise = new ManualPromise<any>();
+ requestCallbacks.set(id, promise);
+ return promise;
+}
+
+function handleResponseFromParent(response: ProtocolResponse) {
+ const promise = requestCallbacks.get(response.id!);
+ if (!promise)
+ return;
+ requestCallbacks.delete(response.id!);
+ if (response.error)
+ promise.reject(new Error(response.error.message));
+ else
+ promise.resolve(response.result);
+}
diff --git a/packages/playwright/src/runner/dispatcher.ts b/packages/playwright/src/runner/dispatcher.ts
index 358f639ac0..ec62cc47d9 100644
--- a/packages/playwright/src/runner/dispatcher.ts
+++ b/packages/playwright/src/runner/dispatcher.ts
@@ -260,6 +260,12 @@ export class Dispatcher {
const producedEnv = this._producedEnvByProjectId.get(testGroup.projectId) || {};
this._producedEnvByProjectId.set(testGroup.projectId, { ...producedEnv, ...worker.producedEnv() });
});
+ worker.onRequest('setStorageValue', async (params: { fileName: string, key: string, value: string }) => {
+ this._setStorageValue(params.fileName, params.key, params.value);
+ });
+ worker.onRequest('getStorageValue', async (params: { fileName: string, key: string }) => {
+ return this._getStorageValue(params.fileName, params.key);
+ });
return worker;
}
@@ -274,6 +280,13 @@ export class Dispatcher {
await Promise.all(this._workerSlots.map(({ worker }) => worker?.stop()));
this._checkFinished();
}
+
+ private _setStorageValue(fileName: string, key: string, value: string) {
+ }
+
+ private _getStorageValue(fileName: string, key: string) {
+ return {};
+ }
}
class JobDispatcher {
diff --git a/packages/playwright/src/runner/processHost.ts b/packages/playwright/src/runner/processHost.ts
index 7910bfe50f..0e70728535 100644
--- a/packages/playwright/src/runner/processHost.ts
+++ b/packages/playwright/src/runner/processHost.ts
@@ -21,7 +21,7 @@ import { assert, timeOrigin } from 'playwright-core/lib/utils';
import { debug } from 'playwright-core/lib/utilsBundle';
import type { EnvProducedPayload, ProcessInitParams } from '../common/ipc';
-import type { ProtocolResponse } from '../common/process';
+import type { ProtocolRequest, ProtocolResponse } from '../common/process';
export type ProcessExitData = {
unexpectedly: boolean;
@@ -40,6 +40,7 @@ export class ProcessHost extends EventEmitter {
private _processName: string;
private _producedEnv: Record<string, string | undefined> = {};
private _extraEnv: Record<string, string | undefined>;
+ private _requestHandlers = new Map<string, (params?: any) => Promise<any>>();
constructor(runnerScript: string, processName: string, env: Record<string, string | undefined>) {
super();
@@ -94,6 +95,18 @@ export class ProcessHost extends EventEmitter {
} else {
this.emit(method!, params);
}
+ } else if (message.method === '__request__') {
+ const { id, method, params } = message.params as ProtocolRequest;
+ const handler = this._requestHandlers.get(method);
+ if (!handler) {
+ this.send({ method: '__response__', params: { id, error: { message: 'Unknown method' } } });
+ } else {
+ handler(params).then(result => {
+ this.send({ method: '__response__', params: { id, result } });
+ }).catch(error => {
+ this.send({ method: '__response__', params: { id, error: { message: error.message } } });
+ });
+ }
} else {
this.emit(message.method!, message.params);
}
@@ -144,6 +157,10 @@ export class ProcessHost extends EventEmitter {
protected async onExit() {
}
+ onRequest(method: string, handler: (params?: any) => Promise<any>) {
+ this._requestHandlers.set(method, handler);
+ }
+
async stop() {
if (!this._processDidExit && !this._didSendStop) {
this.send({ method: '__stop__' });
diff --git a/packages/playwright/src/worker/testInfo.ts b/packages/playwright/src/worker/testInfo.ts
index c9fd959dd9..7889018f0a 100644
--- a/packages/playwright/src/worker/testInfo.ts
+++ b/packages/playwright/src/worker/testInfo.ts
@@ -29,7 +29,7 @@ import type { RunnableDescription } from './timeoutManager';
import type { FullProject, TestInfo, TestStatus, TestStepInfo, TestAnnotation } from '../../types/test';
import type { FullConfig, Location } from '../../types/testReporter';
import type { FullConfigInternal, FullProjectInternal } from '../common/config';
-import type { AttachmentPayload, StepBeginPayload, StepEndPayload, TestInfoErrorImpl, TestPausedPayload, WorkerInitParams } from '../common/ipc';
+import type * as ipc from '../common/ipc';
import type { TestCase } from '../common/test';
import type { StackFrame } from '@protocol/channels';
@@ -57,7 +57,7 @@ export interface TestStepInternal extends TestStepData {
boxedStack?: StackFrame[];
steps: TestStepInternal[];
endWallTime?: number;
- error?: TestInfoErrorImpl;
+ error?: ipc.TestInfoErrorImpl;
}
type SnapshotNames = {
@@ -65,11 +65,17 @@ type SnapshotNames = {
lastNamedSnapshotIndex: { [key: string]: number };
};
+type TestInfoCallbacks = {
+ onStepBegin?: (payload: ipc.StepBeginPayload) => void;
+ onStepEnd?: (payload: ipc.StepEndPayload) => void;
+ onAttach?: (payload: ipc.AttachmentPayload) => void;
+ onTestPaused?: (payload: ipc.TestPausedPayload) => void;
+ onGetStorageValue?: (payload: ipc.GetStorageValuePayload) => Promise<any>;
+ onSetStorageValue?: (payload: ipc.SetStorageValuePayload) => void;
+};
+
export class TestInfoImpl implements TestInfo {
- private _onStepBegin: (payload: StepBeginPayload) => void;
- private _onStepEnd: (payload: StepEndPayload) => void;
- private _onAttach: (payload: AttachmentPayload) => void;
- private _onTestPaused: (payload: TestPausedPayload) => void;
+ private _callbacks: TestInfoCallbacks;
private _snapshotNames: SnapshotNames = { lastAnonymousSnapshotIndex: 0, lastNamedSnapshotIndex: {} };
private _ariaSnapshotNames: SnapshotNames = { lastAnonymousSnapshotIndex: 0, lastNamedSnapshotIndex: {} };
readonly _timeoutManager: TimeoutManager;
@@ -120,15 +126,15 @@ export class TestInfoImpl implements TestInfo {
snapshotSuffix: string = '';
readonly outputDir: string;
readonly snapshotDir: string;
- errors: TestInfoErrorImpl[] = [];
+ errors: ipc.TestInfoErrorImpl[] = [];
readonly _attachmentsPush: (...items: TestInfo['attachments']) => number;
- private _workerParams: WorkerInitParams;
+ private _workerParams: ipc.WorkerInitParams;
- get error(): TestInfoErrorImpl | undefined {
+ get error(): ipc.TestInfoErrorImpl | undefined {
return this.errors[0];
}
- set error(e: TestInfoErrorImpl | undefined) {
+ set error(e: ipc.TestInfoErrorImpl | undefined) {
if (e === undefined)
throw new Error('Cannot assign testInfo.error undefined value!');
this.errors[0] = e;
@@ -158,19 +164,13 @@ export class TestInfoImpl implements TestInfo {
constructor(
configInternal: FullConfigInternal,
projectInternal: FullProjectInternal,
- workerParams: WorkerInitParams,
+ workerParams: ipc.WorkerInitParams,
test: TestCase | undefined,
retry: number,
- onStepBegin: (payload: StepBeginPayload) => void,
- onStepEnd: (payload: StepEndPayload) => void,
- onAttach: (payload: AttachmentPayload) => void,
- onTestPaused: (payload: TestPausedPayload) => void,
+ callbacks: TestInfoCallbacks
) {
this.testId = test?.id ?? '';
- this._onStepBegin = onStepBegin;
- this._onStepEnd = onStepEnd;
- this._onAttach = onAttach;
- this._onTestPaused = onTestPaused;
+ this._callbacks = callbacks;
this._startTime = monotonicTime();
this._startWallTime = Date.now();
this._requireFile = test?._requireFile ?? '';
@@ -338,7 +338,7 @@ export class TestInfoImpl implements TestInfo {
}
if (!step.group) {
- const payload: StepEndPayload = {
+ const payload: ipc.StepEndPayload = {
testId: this.testId,
stepId,
wallTime: step.endWallTime,
@@ -346,7 +346,7 @@ export class TestInfoImpl implements TestInfo {
suggestedRebaseline: result.suggestedRebaseline,
annotations: step.info.annotations,
};
- this._onStepEnd(payload);
+ this._callbacks.onStepEnd?.(payload);
}
if (step.group !== 'internal') {
const errorForTrace = step.error ? { name: '', message: step.error.message || '', stack: step.error.stack } : undefined;
@@ -360,7 +360,7 @@ export class TestInfoImpl implements TestInfo {
this._stepMap.set(stepId, step);
if (!step.group) {
- const payload: StepBeginPayload = {
+ const payload: ipc.StepBeginPayload = {
testId: this.testId,
stepId,
parentStepId: parentStep ? parentStep.stepId : undefined,
@@ -369,7 +369,7 @@ export class TestInfoImpl implements TestInfo {
wallTime: Date.now(),
location: step.location,
};
- this._onStepBegin(payload);
+ this._callbacks.onStepBegin?.(payload);
}
if (step.group !== 'internal') {
this._tracing.appendBeforeActionForStep({
@@ -464,7 +464,7 @@ export class TestInfoImpl implements TestInfo {
async _didFinishTestFunction() {
const shouldPause = (this._workerParams.pauseAtEnd && !this._isFailure()) || (this._workerParams.pauseOnError && this._isFailure());
if (shouldPause) {
- this._onTestPaused({ testId: this.testId, errors: this._isFailure() ? this.errors : [] });
+ this._callbacks.onTestPaused?.({ testId: this.testId, errors: this._isFailure() ? this.errors : [] });
await this._interruptedPromise;
}
await this._onDidFinishTestFunctionCallback?.();
@@ -499,7 +499,7 @@ export class TestInfoImpl implements TestInfo {
this._tracing.appendAfterActionForStep(stepId, undefined, [attachment]);
}
- this._onAttach({
+ this._callbacks.onAttach?.({
testId: this.testId,
name: attachment.name,
contentType: attachment.contentType,
@@ -635,6 +635,14 @@ export class TestInfoImpl implements TestInfo {
setTimeout(timeout: number) {
this._timeoutManager.setTimeout(timeout);
}
+
+ async _getStorageValue(fileName: string, key: string): Promise<any> {
+ return await this._callbacks.onGetStorageValue?.({ fileName, key }) ?? Promise.resolve(undefined);
+ }
+
+ _setStorageValue(fileName: string, key: string, value: string) {
+ this._callbacks.onSetStorageValue?.({ fileName, key, value });
+ }
}
export class TestStepInfoImpl implements TestStepInfo {
diff --git a/packages/playwright/src/worker/workerMain.ts b/packages/playwright/src/worker/workerMain.ts
index f1a96cbdd8..d19c4076dc 100644
--- a/packages/playwright/src/worker/workerMain.ts
+++ b/packages/playwright/src/worker/workerMain.ts
@@ -33,19 +33,19 @@ import { loadTestFile } from '../common/testLoader';
import type { TimeSlot } from './timeoutManager';
import type { Location } from '../../types/testReporter';
import type { FullConfigInternal, FullProjectInternal } from '../common/config';
-import type { CustomMessageRequestPayload, CustomMessageResponsePayload, DonePayload, RunPayload, TeardownErrorsPayload, TestBeginPayload, TestEndPayload, TestInfoErrorImpl, WorkerInitParams } from '../common/ipc';
+import type * as ipc from '../common/ipc';
import type { Suite, TestCase } from '../common/test';
import type { TestAnnotation } from '../../types/test';
export class WorkerMain extends ProcessRunner {
- private _params: WorkerInitParams;
+ private _params: ipc.WorkerInitParams;
private _config!: FullConfigInternal;
private _project!: FullProjectInternal;
private _poolBuilder!: PoolBuilder;
private _fixtureRunner: FixtureRunner;
// Accumulated fatal errors that cannot be attributed to a test.
- private _fatalErrors: TestInfoErrorImpl[] = [];
+ private _fatalErrors: ipc.TestInfoErrorImpl[] = [];
// Whether we should skip running remaining tests in this suite because
// of a setup error, usually beforeAll hook.
private _skipRemainingTestsInSuite: Suite | undefined;
@@ -66,7 +66,7 @@ export class WorkerMain extends ProcessRunner {
// Contains dynamic annotations originated by modifiers with a callback, e.g. `test.skip(() => true)`.
private _activeSuites = new Map<Suite, TestAnnotation[]>();
- constructor(params: WorkerInitParams) {
+ constructor(params: ipc.WorkerInitParams) {
super();
process.env.TEST_WORKER_INDEX = String(params.workerIndex);
process.env.TEST_PARALLEL_INDEX = String(params.parallelIndex);
@@ -118,7 +118,7 @@ export class WorkerMain extends ProcessRunner {
return;
}
// Ignore top-level errors, they are already inside TestInfo.errors.
- const fakeTestInfo = new TestInfoImpl(this._config, this._project, this._params, undefined, 0, () => {}, () => {}, () => {}, () => {});
+ const fakeTestInfo = new TestInfoImpl(this._config, this._project, this._params, undefined, 0, {});
const runnable = { type: 'teardown' } as const;
// We have to load the project to get the right deadline below.
await fakeTestInfo._runWithTimeout(runnable, () => this._loadIfNeeded()).catch(() => {});
@@ -134,12 +134,12 @@ export class WorkerMain extends ProcessRunner {
if (this._fatalErrors.length) {
this._appendProcessTeardownDiagnostics(this._fatalErrors[this._fatalErrors.length - 1]);
- const payload: TeardownErrorsPayload = { fatalErrors: this._fatalErrors };
+ const payload: ipc.TeardownErrorsPayload = { fatalErrors: this._fatalErrors };
this.dispatchEvent('teardownErrors', payload);
}
}
- private _appendProcessTeardownDiagnostics(error: TestInfoErrorImpl) {
+ private _appendProcessTeardownDiagnostics(error: ipc.TestInfoErrorImpl) {
if (!this._lastRunningTests.length)
return;
const count = this._totalRunningTests === 1 ? '1 test' : `${this._totalRunningTests} tests`;
@@ -213,7 +213,7 @@ export class WorkerMain extends ProcessRunner {
this._poolBuilder = PoolBuilder.createForWorker(this._project);
}
- async runTestGroup(runPayload: RunPayload) {
+ async runTestGroup(runPayload: ipc.RunPayload) {
this._runFinished = new ManualPromise<void>();
const entries = new Map(runPayload.entries.map(e => [e.testId, e]));
let fatalUnknownTestIds: string[] | undefined;
@@ -250,7 +250,7 @@ export class WorkerMain extends ProcessRunner {
this._fatalErrors.push(testInfoError(e));
void this._stop();
} finally {
- const donePayload: DonePayload = {
+ const donePayload: ipc.DonePayload = {
fatalErrors: this._fatalErrors,
skipTestsDueToSetupFailure: [],
fatalUnknownTestIds,
@@ -267,7 +267,7 @@ export class WorkerMain extends ProcessRunner {
}
}
- async customMessage(payload: CustomMessageRequestPayload): Promise<CustomMessageResponsePayload> {
+ async customMessage(payload: ipc.CustomMessageRequestPayload): Promise<ipc.CustomMessageResponsePayload> {
try {
if (this._currentTest?.testId !== payload.testId)
throw new Error('Test has already stopped');
@@ -279,12 +279,14 @@ export class WorkerMain extends ProcessRunner {
}
private async _runTest(test: TestCase, retry: number, nextTest: TestCase | undefined) {
- const testInfo = new TestInfoImpl(this._config, this._project, this._params, test, retry,
- stepBeginPayload => this.dispatchEvent('stepBegin', stepBeginPayload),
- stepEndPayload => this.dispatchEvent('stepEnd', stepEndPayload),
- attachment => this.dispatchEvent('attach', attachment),
- testPausedPayload => this.dispatchEvent('testPaused', testPausedPayload));
-
+ const testInfo = new TestInfoImpl(this._config, this._project, this._params, test, retry, {
+ onStepBegin: payload => this.dispatchEvent('stepBegin', payload),
+ onStepEnd: payload => this.dispatchEvent('stepEnd', payload),
+ onAttach: payload => this.dispatchEvent('attach', payload),
+ onTestPaused: payload => this.dispatchEvent('testPaused', payload),
+ onGetStorageValue: payload => this.sendRequest('getStorageValue', payload),
+ onSetStorageValue: payload => this.sendMessageNoReply('setStorageValue', payload),
+ });
const processAnnotation = (annotation: TestAnnotation) => {
testInfo.annotations.push(annotation);
switch (annotation.type) {
@@ -610,14 +612,14 @@ export class WorkerMain extends ProcessRunner {
}
}
-function buildTestBeginPayload(testInfo: TestInfoImpl): TestBeginPayload {
+function buildTestBeginPayload(testInfo: TestInfoImpl): ipc.TestBeginPayload {
return {
testId: testInfo.testId,
startWallTime: testInfo._startWallTime,
};
}
-function buildTestEndPayload(testInfo: TestInfoImpl): TestEndPayload {
+function buildTestEndPayload(testInfo: TestInfoImpl): ipc.TestEndPayload {
return {
testId: testInfo.testId,
duration: testInfo.duration,
@@ -651,4 +653,4 @@ function calculateMaxTimeout(t1: number, t2: number) {
return (!t1 || !t2) ? 0 : Math.max(t1, t2);
}
-export const create = (params: WorkerInitParams) => new WorkerMain(params);
+export const create = (params: ipc.WorkerInitParams) => new WorkerMain(params);