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
chore: merge agent cache from workers (#38500)
Pavel Feldman86b465c0c5da
diff --git a/packages/playwright-core/src/server/agent/agent.ts b/packages/playwright-core/src/server/agent/agent.tsindex 4ab64460f8..27866b04cc 100644--- a/packages/playwright-core/src/server/agent/agent.ts+++ b/packages/playwright-core/src/server/agent/agent.ts@@ -80,7 +80,10 @@ ${full}});}-type CachedActions = Record<string, actions.Action[]>;+type CachedActions = Record<string, {+ timestamp: number,+ actions: actions.Action[],+}>;const allCaches = new Map<string, CachedActions>();@@ -90,14 +93,14 @@ async function cachedPerform(context: Context, options: channels.PagePerformParaconst cache = await cachedActions(context.options.cacheFile);const cacheKey = options.key ?? options.task;- const actions = cache[cacheKey];- if (!actions) {+ const entry = cache[cacheKey];+ if (!entry) {if (context.options.cacheMode === 'force')throw new Error(`No cached actions for key "${cacheKey}", but cache mode is set to "force"`);return false;}- for (const action of actions)+ for (const action of entry.actions)await runAction(context.progress, context.page, action, context.options.secrets ?? []);return true;}@@ -108,7 +111,10 @@ async function updateCache(context: Context, options: channels.PagePerformParamsreturn;const cache = await cachedActions(cacheFile);const cacheKey = options.key ?? options.task;- cache[cacheKey] = context.actions;+ cache[cacheKey] = {+ timestamp: Date.now(),+ actions: context.actions,+ };await fs.promises.writeFile(cacheFile, JSON.stringify(cache, undefined, 2));}diff --git a/packages/playwright/src/common/ipc.ts b/packages/playwright/src/common/ipc.tsindex 91a201dbb8..42f5e40d4e 100644--- a/packages/playwright/src/common/ipc.ts+++ b/packages/playwright/src/common/ipc.ts@@ -96,15 +96,12 @@ export type ResumePayload = {action?: 'continue' | 'abort';};-export type GetStorageValuePayload = {- fileName: string;- key: string;+export type CloneStoragePayload = {+ storageFile: string;};-export type SetStorageValuePayload = {- fileName: string;- key: string;- value: any;+export type UpstreamStoragePayload = {+ workerFile: string;};export type CustomMessageRequestPayload = {diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.tsindex 85c763c633..44e9d726ca 100644--- a/packages/playwright/src/index.ts+++ b/packages/playwright/src/index.ts@@ -179,7 +179,7 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({baseURL,contextOptions,serviceWorkers,- }, use) => {+ }, use, testInfo) => {const options: BrowserContextOptions = {};if (acceptDownloads !== undefined)options.acceptDownloads = acceptDownloads;@@ -227,10 +227,22 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({options.baseURL = baseURL;if (serviceWorkers !== undefined)options.serviceWorkers = serviceWorkers;++ const workerFile = agent?.cacheFile && agent.cacheMode !== 'ignore' ? await (testInfo as TestInfoImpl)._cloneStorage(agent.cacheFile) : undefined;+ if (agent && workerFile) {+ options.agent = {+ ...agent,+ cacheFile: workerFile,+ };+ }+await use({...contextOptions,...options,});++ if (workerFile)+ await (testInfo as TestInfoImpl)._upstreamStorage(workerFile);}, { box: true }],_setupContextOptions: [async ({ playwright, _combinedContextOptions, actionTimeout, navigationTimeout, testIdAttribute }, use, testInfo) => {diff --git a/packages/playwright/src/runner/dispatcher.ts b/packages/playwright/src/runner/dispatcher.tsindex 5e5c9a7b85..6b223f0643 100644--- a/packages/playwright/src/runner/dispatcher.ts+++ b/packages/playwright/src/runner/dispatcher.ts@@ -22,11 +22,13 @@ import { WorkerHost } from './workerHost';import { serializeConfig } from '../common/ipc';import { addLocationAndSnippetToError } from '../reporters/internalReporter';import { serializeError } from '../util';+import { Storage } from './storage';import type { FailureTracker } from './failureTracker';import type { ProcessExitData } from './processHost';import type { TestGroup } from './testGroups';import type { TestError, TestResult, TestStep } from '../../types/testReporter';+import type * as ipc from '../common/ipc';import type { FullConfigInternal } from '../common/config';import type { AttachmentPayload, DonePayload, RunPayload, SerializedConfig, StepBeginPayload, StepEndPayload, TeardownErrorsPayload, TestBeginPayload, TestEndPayload, TestOutputPayload, TestPausedPayload } from '../common/ipc';import type { Suite } from '../common/test';@@ -260,11 +262,11 @@ 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('cloneStorage', async (params: ipc.CloneStoragePayload) => {+ return await Storage.clone(params.storageFile, worker.artifactsDir());});- worker.onRequest('getStorageValue', async (params: { fileName: string, key: string }) => {- return this._getStorageValue(params.fileName, params.key);+ worker.onRequest('upstreamStorage', async (params: ipc.UpstreamStoragePayload) => {+ await Storage.upstream(params.workerFile);});return worker;}@@ -280,13 +282,6 @@ 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.tsindex 0e70728535..e3d77633b2 100644--- a/packages/playwright/src/runner/processHost.ts+++ b/packages/playwright/src/runner/processHost.ts@@ -40,7 +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>>();+ private _requestHandlers = new Map<string, (params: any) => Promise<any>>();constructor(runnerScript: string, processName: string, env: Record<string, string | undefined>) {super();diff --git a/packages/playwright/src/runner/storage.ts b/packages/playwright/src/runner/storage.tsnew file mode 100644index 0000000000..46c897a6d2--- /dev/null+++ b/packages/playwright/src/runner/storage.ts@@ -0,0 +1,97 @@+/**+ * Copyright Microsoft Corporation. All rights reserved.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import fs from 'fs';+import path from 'path';++import { createGuid } from 'playwright-core/lib/utils';++type StorageEnties = Record<string, { timestamp: number, value: any }>;++export class Storage {+ private static _storages = new Map<string, Storage>();+ private static _workerFiles = new Map<string, {+ storageFile: string;+ lastModified: number;+ }>();++ private _fileName: string;+ private _entriesPromise: Promise<StorageEnties> | undefined;+ private _writeChain: Promise<void> = Promise.resolve();++ static async clone(storageFile: string, artifactsDir: string): Promise<string> {+ const workerFile = await this._storage(storageFile)._clone(artifactsDir);+ const stat = await fs.promises.stat(workerFile);+ const lastModified = stat.mtime.getTime();+ Storage._workerFiles.set(workerFile, { storageFile, lastModified });+ return workerFile;+ }++ static async upstream(workerFile: string) {+ const entry = Storage._workerFiles.get(workerFile);+ if (!entry)+ return;+ const { storageFile, lastModified } = entry;+ const stat = await fs.promises.stat(workerFile);+ const newLastModified = stat.mtime.getTime();+ if (lastModified !== newLastModified)+ await this._storage(storageFile)._upstream(workerFile);+ Storage._workerFiles.delete(workerFile);+ }++ private static _storage(fileName: string) {+ if (!Storage._storages.has(fileName))+ Storage._storages.set(fileName, new Storage(fileName));+ return Storage._storages.get(fileName)!;+ }++ private constructor(fileName: string) {+ this._fileName = fileName;+ }++ async _clone(artifactsDir: string): Promise<string> {+ const entries = await this._load();+ const workerFile = path.join(artifactsDir, `pw-storage-${createGuid()}.json`);+ await fs.promises.writeFile(workerFile, JSON.stringify(entries, null, 2)).catch(() => {});+ return workerFile;+ }++ async _upstream(workerFile: string) {+ const entries = await this._load();+ const newEntries = await fs.promises.readFile(workerFile, 'utf8').then(JSON.parse).catch(() => ({})) as StorageEnties;+ for (const [key, newValue] of Object.entries(newEntries)) {+ const existing = entries[key];+ if (!existing || existing.timestamp < newValue.timestamp)+ entries[key] = newValue;+ }+ await this._writeFile(entries);+ }++ private async _load(): Promise<StorageEnties> {+ if (!this._entriesPromise)+ this._entriesPromise = fs.promises.readFile(this._fileName, 'utf8').then(JSON.parse).catch(() => ({}));+ return this._entriesPromise;+ }++ private _writeFile(entries: Record<string, any>) {+ this._writeChain = this._writeChain.then(() => fs.promises.writeFile(this._fileName, JSON.stringify(entries, null, 2))).catch(() => {});+ return this._writeChain;+ }++ async flush() {+ await this._writeChain;+ }+}diff --git a/packages/playwright/src/runner/workerHost.ts b/packages/playwright/src/runner/workerHost.tsindex bd8dcc7563..348257b8fc 100644--- a/packages/playwright/src/runner/workerHost.ts+++ b/packages/playwright/src/runner/workerHost.ts@@ -68,6 +68,10 @@ export class WorkerHost extends ProcessHost {};}+ artifactsDir() {+ return this._params.artifactsDir;+ }+async start() {await fs.promises.mkdir(this._params.artifactsDir, { recursive: true });return await this.startRunner(this._params, {diff --git a/packages/playwright/src/worker/testInfo.ts b/packages/playwright/src/worker/testInfo.tsindex 083b14c6eb..b103cce380 100644--- a/packages/playwright/src/worker/testInfo.ts+++ b/packages/playwright/src/worker/testInfo.ts@@ -71,8 +71,8 @@ type TestInfoCallbacks = {onStepEnd?: (payload: ipc.StepEndPayload) => void;onAttach?: (payload: ipc.AttachmentPayload) => void;onTestPaused?: (payload: ipc.TestPausedPayload) => Promise<ipc.ResumePayload>;- onGetStorageValue?: (payload: ipc.GetStorageValuePayload) => Promise<any>;- onSetStorageValue?: (payload: ipc.SetStorageValuePayload) => void;+ onCloneStorage?: (payload: ipc.CloneStoragePayload) => Promise<string>;+ onUpstreamStorage?: (payload: ipc.UpstreamStoragePayload) => Promise<void>;};export class TestInfoImpl implements TestInfo {@@ -660,12 +660,12 @@ export class TestInfoImpl implements TestInfo {this._timeoutManager.setTimeout(timeout);}- async _getStorageValue(fileName: string, key: string): Promise<any> {- return await this._callbacks.onGetStorageValue?.({ fileName, key }) ?? Promise.resolve(undefined);+ async _cloneStorage(storageFile: string): Promise<string | undefined> {+ return await this._callbacks.onCloneStorage?.({ storageFile });}- _setStorageValue(fileName: string, key: string, value: string) {- this._callbacks.onSetStorageValue?.({ fileName, key, value });+ async _upstreamStorage(workerFile: string) {+ await this._callbacks.onUpstreamStorage?.({ workerFile });}}diff --git a/packages/playwright/src/worker/workerMain.ts b/packages/playwright/src/worker/workerMain.tsindex 90f3d55ca3..7bdcf385f9 100644--- a/packages/playwright/src/worker/workerMain.ts+++ b/packages/playwright/src/worker/workerMain.ts@@ -293,8 +293,8 @@ export class WorkerMain extends ProcessRunner {this.dispatchEvent('testPaused', payload);return this._resumePromise;},- onGetStorageValue: payload => this.sendRequest('getStorageValue', payload),- onSetStorageValue: payload => this.sendMessageNoReply('setStorageValue', payload),+ onCloneStorage: async payload => this.sendRequest('cloneStorage', payload),+ onUpstreamStorage: payload => this.sendRequest('upstreamStorage', payload),});const processAnnotation = (annotation: TestAnnotation) => {testInfo.annotations.push(annotation);diff --git a/tests/library/agent-cache.json b/tests/library/agent-cache.jsonindex 3b7fde719e..c1deb2ea11 100644--- a/tests/library/agent-cache.json+++ b/tests/library/agent-cache.json@@ -1,41 +1,47 @@{- "Fill out the form with the following details:\nName: John Smith\nAddress: 1045 La Avenida St, Mountain View, CA 94043\nEmail: [email protected]": [- {- "method": "fill",- "selector": "internal:role=textbox[name=\"Full Name *\"i]",- "text": "John Smith"- },- {- "method": "fill",- "selector": "internal:role=textbox[name=\"Email Address *\"i]",- "text": "[email protected]"- },- {- "method": "fill",- "selector": "internal:role=textbox[name=\"Street Address *\"i]",- "text": "1045 La Avenida St"- },- {- "method": "fill",- "selector": "internal:role=textbox[name=\"City *\"i]",- "text": "Mountain View"- },- {- "method": "fill",- "selector": "internal:role=textbox[name=\"State/Province *\"i]",- "text": "CA"- },- {- "method": "fill",- "selector": "internal:role=textbox[name=\"ZIP/Postal Code *\"i]",- "text": "94043"- }- ],- "Enter x-secret-email into the email field": [- {- "method": "fill",- "selector": "internal:role=textbox[name=\"Email Address\"i]",- "text": "x-secret-email"- }- ]+ "Enter x-secret-email into the email field": {+ "timestamp": 1765318973255,+ "actions": [+ {+ "method": "fill",+ "selector": "internal:role=textbox[name=\"Email Address\"i]",+ "text": "x-secret-email"+ }+ ]+ },+ "Fill out the form with the following details:\nName: John Smith\nAddress: 1045 La Avenida St, Mountain View, CA 94043\nEmail: [email protected]": {+ "timestamp": 1765318976120,+ "actions": [+ {+ "method": "fill",+ "selector": "internal:role=textbox[name=\"Full Name *\"i]",+ "text": "John Smith"+ },+ {+ "method": "fill",+ "selector": "internal:role=textbox[name=\"Email Address *\"i]",+ "text": "[email protected]"+ },+ {+ "method": "fill",+ "selector": "internal:role=textbox[name=\"Street Address *\"i]",+ "text": "1045 La Avenida St"+ },+ {+ "method": "fill",+ "selector": "internal:role=textbox[name=\"City *\"i]",+ "text": "Mountain View"+ },+ {+ "method": "fill",+ "selector": "internal:role=textbox[name=\"State/Province *\"i]",+ "text": "CA"+ },+ {+ "method": "fill",+ "selector": "internal:role=textbox[name=\"ZIP/Postal Code *\"i]",+ "text": "94043"+ }+ ]+ }}\ No newline at end of filediff --git a/tests/library/perform-task.spec.ts b/tests/library/perform-task.spec.tsindex 87f8e82ad2..914941efc2 100644--- a/tests/library/perform-task.spec.ts+++ b/tests/library/perform-task.spec.ts@@ -20,18 +20,15 @@ import z from 'zod';import { browserTest as test, expect } from '../config/browserTest';test.use({- contextOptions: async ({ contextOptions }, use, testInfo) => {+ agent: async ({}, use, testInfo) => {await use({- ...contextOptions,- agent: {- provider: 'github',- model: 'claude-sonnet-4.5',- cacheFile: path.join(testInfo.project.testDir, 'agent-cache.json'),- cacheMode: process.env.CI ? 'force' : 'auto',- secrets: {- 'x-secret-email': '[email protected]',- }- },+ provider: 'github',+ model: 'claude-sonnet-4.5',+ cacheFile: path.join(testInfo.project.testDir, 'agent-cache.json'),+ cacheMode: process.env.CI ? 'force' : 'auto',+ secrets: {+ 'x-secret-email': '[email protected]',+ }});}});