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(perform): validate cache file (#38794)
Simon Knott11865e85d167
diff --git a/packages/playwright-core/src/server/agent/actions.ts b/packages/playwright-core/src/server/agent/actions.tsindex dd058a5afd..656a1f91f4 100644--- a/packages/playwright-core/src/server/agent/actions.ts+++ b/packages/playwright-core/src/server/agent/actions.ts@@ -14,97 +14,123 @@* limitations under the License.*/-export type NavigateAction = {- method: 'navigate';- url: string;-};--export type ClickAction = {- method: 'click';- selector: string;- button?: 'left' | 'right' | 'middle';- clickCount?: number;- modifiers?: ('Alt' | 'Control' | 'ControlOrMeta' | 'Meta' | 'Shift')[];-};--export type DragAction = {- method: 'drag';- sourceSelector: string;- targetSelector: string;-};--export type HoverAction = {- method: 'hover';- selector: string;- modifiers?: ('Alt' | 'Control' | 'ControlOrMeta' | 'Meta' | 'Shift')[];-};--export type SelectOptionAction = {- method: 'selectOption';- selector: string;- labels: string[];-};--export type PressAction = {- method: 'pressKey';- // Includes modifiers- key: string;-};--export type PressSequentiallyAction = {- method: 'pressSequentially';- selector: string;- text: string;- submit?: boolean;-};--export type FillAction = {- method: 'fill';- selector: string;- text: string;- submit?: boolean;-};--export type SetChecked = {- method: 'setChecked';- selector: string;- checked: boolean;-};--export type ExpectVisible = {- method: 'expectVisible';- selector: string;- isNot?: boolean;-};--export type ExpectValue = {- method: 'expectValue';- selector: string;- type: 'textbox' | 'checkbox' | 'radio' | 'combobox' | 'slider';- value: string;- isNot?: boolean;-};--export type ExpectAria = {- method: 'expectAria';- template: string;- isNot?: boolean;-};--export type Action =- | NavigateAction- | ClickAction- | DragAction- | HoverAction- | SelectOptionAction- | PressAction- | PressSequentiallyAction- | FillAction- | SetChecked- | ExpectVisible- | ExpectValue- | ExpectAria;--export type ActionWithCode = Action & {- code: string;-};+import { zod } from '../../utilsBundle';+import type z from 'zod';++const modifiersSchema = zod.array(+ zod.enum(['Alt', 'Control', 'ControlOrMeta', 'Meta', 'Shift'])+);++const navigateActionSchema = zod.object({+ method: zod.literal('navigate'),+ url: zod.string(),+});+export type NavigateAction = z.infer<typeof navigateActionSchema>;++const clickActionSchema = zod.object({+ method: zod.literal('click'),+ selector: zod.string(),+ button: zod.enum(['left', 'right', 'middle']).optional(),+ clickCount: zod.number().optional(),+ modifiers: modifiersSchema.optional(),+});+export type ClickAction = z.infer<typeof clickActionSchema>;++const dragActionSchema = zod.object({+ method: zod.literal('drag'),+ sourceSelector: zod.string(),+ targetSelector: zod.string(),+});+export type DragAction = z.infer<typeof dragActionSchema>;++const hoverActionSchema = zod.object({+ method: zod.literal('hover'),+ selector: zod.string(),+ modifiers: modifiersSchema.optional(),+});+export type HoverAction = z.infer<typeof hoverActionSchema>;++const selectOptionActionSchema = zod.object({+ method: zod.literal('selectOption'),+ selector: zod.string(),+ labels: zod.array(zod.string()),+});+export type SelectOptionAction = z.infer<typeof selectOptionActionSchema>;++const pressActionSchema = zod.object({+ method: zod.literal('pressKey'),+ key: zod.string(),+});+export type PressAction = z.infer<typeof pressActionSchema>;++const pressSequentiallyActionSchema = zod.object({+ method: zod.literal('pressSequentially'),+ selector: zod.string(),+ text: zod.string(),+ submit: zod.boolean().optional(),+});+export type PressSequentiallyAction = z.infer<typeof pressSequentiallyActionSchema>;++const fillActionSchema = zod.object({+ method: zod.literal('fill'),+ selector: zod.string(),+ text: zod.string(),+ submit: zod.boolean().optional(),+});+export type FillAction = z.infer<typeof fillActionSchema>;++const setCheckedSchema = zod.object({+ method: zod.literal('setChecked'),+ selector: zod.string(),+ checked: zod.boolean(),+});+export type SetChecked = z.infer<typeof setCheckedSchema>;++const expectVisibleSchema = zod.object({+ method: zod.literal('expectVisible'),+ selector: zod.string(),+ isNot: zod.boolean().optional(),+});+export type ExpectVisible = z.infer<typeof expectVisibleSchema>;++const expectValueSchema = zod.object({+ method: zod.literal('expectValue'),+ selector: zod.string(),+ type: zod.enum(['textbox', 'checkbox', 'radio', 'combobox', 'slider']),+ value: zod.string(),+ isNot: zod.boolean().optional(),+});+export type ExpectValue = z.infer<typeof expectValueSchema>;++const expectAriaSchema = zod.object({+ method: zod.literal('expectAria'),+ template: zod.string(),+ isNot: zod.boolean().optional(),+});+export type ExpectAria = z.infer<typeof expectAriaSchema>;++const actionSchema = zod.discriminatedUnion('method', [+ navigateActionSchema,+ clickActionSchema,+ dragActionSchema,+ hoverActionSchema,+ selectOptionActionSchema,+ pressActionSchema,+ pressSequentiallyActionSchema,+ fillActionSchema,+ setCheckedSchema,+ expectVisibleSchema,+ expectValueSchema,+ expectAriaSchema,+]);+export type Action = z.infer<typeof actionSchema>;++const actionWithCodeSchema = actionSchema.and(zod.object({+ code: zod.string(),+}));+export type ActionWithCode = z.infer<typeof actionWithCodeSchema>;++export const cachedActionsSchema = zod.record(zod.object({+ actions: zod.array(actionWithCodeSchema),+}));+export type CachedActions = z.infer<typeof cachedActionsSchema>;diff --git a/packages/playwright-core/src/server/agent/pageAgent.ts b/packages/playwright-core/src/server/agent/pageAgent.tsindex 20571dbe13..ff573aa9e7 100644--- a/packages/playwright-core/src/server/agent/pageAgent.ts+++ b/packages/playwright-core/src/server/agent/pageAgent.ts@@ -25,7 +25,7 @@ import { Context } from './context';import performTools from './performTools';import expectTools from './expectTools';-import type * as actions from './actions';+import * as actions from './actions';import type { ToolDefinition } from './tool';import type * as loopTypes from '@lowire/loop';import type { Progress } from '../progress';@@ -151,10 +151,6 @@ async function runLoop(progress: Progress, context: Context, toolDefinitions: Toreturn { result: resultSchema ? reportedResult() : undefined };}-type CachedActions = Record<string, {- actions: actions.ActionWithCode[],-}>;-async function cachedPerform(progress: Progress, context: Context, cacheKey: string): Promise<actions.ActionWithCode[] | undefined> {if (!context.agentParams?.cacheFile)return;@@ -191,8 +187,8 @@ async function updateCache(context: Context, cacheKey: string) {}type Cache = {- actions: CachedActions;- newActions: CachedActions;+ actions: actions.CachedActions;+ newActions: actions.CachedActions;};const allCaches = new Map<string, Cache>();@@ -200,8 +196,11 @@ const allCaches = new Map<string, Cache>();async function cachedActions(cacheFile: string): Promise<Cache> {let cache = allCaches.get(cacheFile);if (!cache) {- const actions = await fs.promises.readFile(cacheFile, 'utf-8').then(text => JSON.parse(text)).catch(() => ({})) as CachedActions;- cache = { actions, newActions: {} };+ const text = await fs.promises.readFile(cacheFile, 'utf-8').catch(() => '{}');+ const parsed = actions.cachedActionsSchema.safeParse(JSON.parse(text));+ if (parsed.error)+ throw new Error(`Failed to parse cache file ${cacheFile}: ${parsed.error.issues.map(issue => issue.message).join(', ')}`);+ cache = { actions: parsed.data, newActions: {} };allCaches.set(cacheFile, cache);}return cache;diff --git a/tests/library/agent-helpers.ts b/tests/library/agent-helpers.tsindex 42d11e58a0..af0920aeb8 100644--- a/tests/library/agent-helpers.ts+++ b/tests/library/agent-helpers.ts@@ -20,7 +20,7 @@ import path from 'path';import { browserTest as test } from '../config/browserTest';import type { BrowserContext, Page, PageAgent } from '@playwright/test';-function cacheFile() {+export function cacheFile() {return test.info().outputPath('agent-cache.json');}diff --git a/tests/library/agent-perform.spec.ts b/tests/library/agent-perform.spec.tsindex c6020f58b7..b57cbf1219 100644--- a/tests/library/agent-perform.spec.ts+++ b/tests/library/agent-perform.spec.ts@@ -14,10 +14,11 @@* limitations under the License.*/+import fs from 'fs';import { z } from 'zod';import { browserTest as test, expect } from '../config/browserTest';-import { run, generateAgent, cacheObject, runAgent } from './agent-helpers';+import { run, generateAgent, cacheObject, runAgent, cacheFile } from './agent-helpers';// LOWIRE_NO_CACHE=1 to generate api caches// LOWIRE_FORCE_CACHE=1 to force api caches@@ -154,3 +155,17 @@ test('perform run timeout', async ({ context }) => {expect(error.message).toContain(`waiting for getByRole('button', { name: 'Fox' })`);}});++test('invalid cache file throws error', async ({ context }) => {+ await fs.promises.writeFile(cacheFile(), JSON.stringify({+ 'some key': {+ actions: [{+ method: 'invalid-method',+ }],+ },+ }));+ const { agent } = await runAgent(context);+ await expect(() => agent.perform('click the Test button')).rejects.toThrowError(+ /.*Failed to parse cache file .*: Invalid discriminator value*/+ );+});