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: support cachePathTemplate (#38516)
Pavel Feldman703996071cb3
diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.tsindex 44e9d726ca..51ee0219ba 100644--- a/packages/playwright/src/index.ts+++ b/packages/playwright/src/index.ts@@ -228,20 +228,16 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({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,- };- }+ const workerFile = await agentCacheWorkerFile(agent, testInfo as TestInfoImpl);+ if (agent && workerFile)+ options.agent = { ...agent, cacheFile: workerFile };await use({...contextOptions,...options,});- if (workerFile)+ if (testInfo.status === 'passed' && workerFile)await (testInfo as TestInfoImpl)._upstreamStorage(workerFile);}, { box: true }],@@ -796,6 +792,16 @@ function tracing() {return (test.info() as TestInfoImpl)._tracing;}+async function agentCacheWorkerFile(agent: PlaywrightTestOptions['agent'], testInfo: TestInfoImpl): Promise<string | undefined> {+ if (!agent || agent.cacheMode === 'ignore')+ return undefined;+ if (!agent.cacheFile && !agent.cachePathTemplate)+ return undefined;++ const cacheFile = agent.cacheFile ?? testInfo._applyPathTemplate(agent.cachePathTemplate!, 'cache', '.json');+ return await testInfo._cloneStorage(cacheFile);+}+export const test = _baseTest.extend<TestFixtures, WorkerFixtures>(playwrightFixtures);export { defineConfig } from './common/configLoader';diff --git a/packages/playwright/src/worker/testInfo.ts b/packages/playwright/src/worker/testInfo.tsindex b103cce380..c4525a5750 100644--- a/packages/playwright/src/worker/testInfo.ts+++ b/packages/playwright/src/worker/testInfo.ts@@ -598,11 +598,6 @@ export class TestInfoImpl implements TestInfo {relativeOutputPath = addSuffixToFilePath(relativeOutputPath, `-${index - 1}`);}- const absoluteSnapshotPath = this._applyPathTemplate(kind, subPath, ext);- return { absoluteSnapshotPath, relativeOutputPath };- }-- private _applyPathTemplate(kind: 'snapshot' | 'screenshot' | 'aria', relativePath: string, ext: string) {const legacyTemplate = '{snapshotDir}/{testFileDir}/{testFileName}-snapshots/{arg}{-projectName}{-snapshotSuffix}{ext}';let template: string;if (kind === 'screenshot') {@@ -614,8 +609,12 @@ export class TestInfoImpl implements TestInfo {template = this._projectInternal.snapshotPathTemplate || legacyTemplate;}- const dir = path.dirname(relativePath);- const name = path.basename(relativePath, ext);+ const nameArgument = path.join(path.dirname(subPath), path.basename(subPath, ext));+ const absoluteSnapshotPath = this._applyPathTemplate(template, nameArgument, ext);+ return { absoluteSnapshotPath, relativeOutputPath };+ }++ _applyPathTemplate(template: string, nameArgument: string, ext: string) {const relativeTestFilePath = path.relative(this.project.testDir, this._requireFile);const parsedRelativeTestFilePath = path.parse(relativeTestFilePath);const projectNamePathSegment = sanitizeForFilePath(this.project.name);@@ -630,7 +629,7 @@ export class TestInfoImpl implements TestInfo {.replace(/\{(.)?testName\}/g, '$1' + this._fsSanitizedTestName()).replace(/\{(.)?testFileName\}/g, '$1' + parsedRelativeTestFilePath.base).replace(/\{(.)?testFilePath\}/g, '$1' + relativeTestFilePath)- .replace(/\{(.)?arg\}/g, '$1' + path.join(dir, name))+ .replace(/\{(.)?arg\}/g, '$1' + nameArgument).replace(/\{(.)?ext\}/g, ext ? '$1' + ext : '');return path.normalize(path.resolve(this._configInternal.configDir, snapshotPath));@@ -660,7 +659,8 @@ export class TestInfoImpl implements TestInfo {this._timeoutManager.setTimeout(timeout);}- async _cloneStorage(storageFile: string): Promise<string | undefined> {+ async _cloneStorage(cacheFileTemplate: string): Promise<string | undefined> {+ const storageFile = this._applyPathTemplate(cacheFileTemplate, 'cache', '.json');return await this._callbacks.onCloneStorage?.({ storageFile });}diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.tsindex a6471a8204..aa94fed21e 100644--- a/packages/playwright/types/test.d.ts+++ b/packages/playwright/types/test.d.ts@@ -6644,7 +6644,7 @@ export type Fixtures<T extends {} = {}, W extends {} = {}, PT extends {} = {}, P[K in Exclude<keyof T, keyof PW | keyof PT>]?: TestFixtureValue<T[K], T & W & PT & PW> | [TestFixtureValue<T[K], T & W & PT & PW>, { scope?: 'test', auto?: boolean, option?: boolean, timeout?: number | undefined, title?: string, box?: boolean | 'self' }];};-type Agent = Exclude<BrowserContextOptions['agent'], undefined> | undefined;+type Agent = Exclude<BrowserContextOptions['agent'], undefined> & { cachePathTemplate?: string } | undefined;type BrowserName = 'chromium' | 'firefox' | 'webkit';type BrowserChannel = Exclude<LaunchOptions['channel'], undefined>;type ColorScheme = Exclude<BrowserContextOptions['colorScheme'], undefined>;diff --git a/tests/library/perform-task.spec.ts b/tests/library/perform-task.spec.tsindex 914941efc2..1f59c4c0bf 100644--- a/tests/library/perform-task.spec.ts+++ b/tests/library/perform-task.spec.ts@@ -14,22 +14,19 @@* limitations under the License.*/-import path from 'path';import z from 'zod';import { browserTest as test, expect } from '../config/browserTest';test.use({- agent: async ({}, use, testInfo) => {- await use({- 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]',- }- });+ agent: {+ provider: 'github',+ model: 'claude-sonnet-4.5',+ cachePathTemplate: '{testFilePath}-cache.json',+ cacheMode: process.env.CI ? 'force' : 'auto',+ secrets: {+ 'x-secret-email': '[email protected]',+ }}});diff --git a/tests/library/agent-cache.json b/tests/library/perform-task.spec.ts-cache.jsonsimilarity index 100%rename from tests/library/agent-cache.jsonrename to tests/library/perform-task.spec.ts-cache.jsondiff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.tsindex a314578ba5..4b2bec3390 100644--- a/utils/generate_types/overrides-test.d.ts+++ b/utils/generate_types/overrides-test.d.ts@@ -209,7 +209,7 @@ export type Fixtures<T extends {} = {}, W extends {} = {}, PT extends {} = {}, P[K in Exclude<keyof T, keyof PW | keyof PT>]?: TestFixtureValue<T[K], T & W & PT & PW> | [TestFixtureValue<T[K], T & W & PT & PW>, { scope?: 'test', auto?: boolean, option?: boolean, timeout?: number | undefined, title?: string, box?: boolean | 'self' }];};-type Agent = Exclude<BrowserContextOptions['agent'], undefined> | undefined;+type Agent = Exclude<BrowserContextOptions['agent'], undefined> & { cachePathTemplate?: string } | undefined;type BrowserName = 'chromium' | 'firefox' | 'webkit';type BrowserChannel = Exclude<LaunchOptions['channel'], undefined>;type ColorScheme = Exclude<BrowserContextOptions['colorScheme'], undefined>;