Skip to content

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

chore(agent): expectURL (#38812)

Simon Knott02b8f5325190
diff --git a/examples/todomvc/tests/perform/filtering-todos/should-filter-active-todos.spec.ts-cache.json b/examples/todomvc/tests/perform/filtering-todos/should-filter-active-todos.spec.ts-cache.json
index ef978f6928..266698b8dc 100644
--- a/examples/todomvc/tests/perform/filtering-todos/should-filter-active-todos.spec.ts-cache.json
+++ b/examples/todomvc/tests/perform/filtering-todos/should-filter-active-todos.spec.ts-cache.json
@@ -111,9 +111,9 @@
"The URL changes to #/active": {
"actions": [
{
- "method": "expectVisible",
- "selector": "internal:role=link[name=\"Active\"i]",
- "code": "await expect(page.getByRole('link', { name: 'Active' })).toBeVisible();"
+ "method": "expectURL",
+ "regex": "/.*#/active$/",
+ "code": "await expect(page).toHaveURL(/.*#\\/active$/);"
}
]
}
diff --git a/examples/todomvc/tests/perform/filtering-todos/should-filter-completed-todos.spec.ts-cache.json b/examples/todomvc/tests/perform/filtering-todos/should-filter-completed-todos.spec.ts-cache.json
index f9f5e770a3..c8fd9fa7b5 100644
--- a/examples/todomvc/tests/perform/filtering-todos/should-filter-completed-todos.spec.ts-cache.json
+++ b/examples/todomvc/tests/perform/filtering-todos/should-filter-completed-todos.spec.ts-cache.json
@@ -96,9 +96,9 @@
"The URL changes to #/completed": {
"actions": [
{
- "method": "expectVisible",
- "selector": "internal:role=link[name=\"Completed\"i]",
- "code": "await expect(page.getByRole('link', { name: 'Completed' })).toBeVisible();"
+ "method": "expectURL",
+ "regex": "/.*#/completed$/",
+ "code": "await expect(page).toHaveURL(/.*#\\/completed$/);"
}
]
},
@@ -106,8 +106,8 @@
"actions": [
{
"method": "expectVisible",
- "selector": "internal:text=\"1\"i",
- "code": "await expect(page.getByText('1')).toBeVisible();"
+ "selector": "internal:role=button[name=\"Clear completed\"i]",
+ "code": "await expect(page.getByRole('button', { name: 'Clear completed' })).toBeVisible();"
}
]
}
diff --git a/examples/todomvc/tests/perform/filtering-todos/should-show-all-todos-with-all-filter.spec.ts-cache.json b/examples/todomvc/tests/perform/filtering-todos/should-show-all-todos-with-all-filter.spec.ts-cache.json
index 1dc14c0620..e31b7d2d24 100644
--- a/examples/todomvc/tests/perform/filtering-todos/should-show-all-todos-with-all-filter.spec.ts-cache.json
+++ b/examples/todomvc/tests/perform/filtering-todos/should-show-all-todos-with-all-filter.spec.ts-cache.json
@@ -86,9 +86,9 @@
"The URL changes to #/": {
"actions": [
{
- "method": "expectVisible",
- "selector": "internal:role=link[name=\"All\"i]",
- "code": "await expect(page.getByRole('link', { name: 'All' })).toBeVisible();"
+ "method": "expectURL",
+ "regex": "/.*#/$/",
+ "code": "await expect(page).toHaveURL(/.*#\\/$/);"
}
]
},
diff --git a/packages/playwright-core/src/server/agent/actionRunner.ts b/packages/playwright-core/src/server/agent/actionRunner.ts
index b54ae40454..151460693b 100644
--- a/packages/playwright-core/src/server/agent/actionRunner.ts
+++ b/packages/playwright-core/src/server/agent/actionRunner.ts
@@ -15,6 +15,8 @@
*/
import { formatMatcherMessage, serializeExpectedTextValues, simpleMatcherUtils } from '../utils/expectUtils';
+import { constructURLBasedOnBaseURL } from '../../utils/isomorphic/urlMatch';
+import { parseRegex } from '../../utils/isomorphic/stringUtils';
import { monotonicTime } from '../../utils/isomorphic/time';
import { createGuid } from '../utils/crypto';
import { parseAriaSnapshotUnsafe } from '../../utils/isomorphic/ariaSnapshot';
@@ -122,10 +124,20 @@ async function innerRunAction(progress: Progress, mode: 'generate' | 'run', page
await runExpect(frame, progress, mode, 'body', { expression: 'to.match.aria', expectedValue, isNot: !!action.isNot }, '\n' + action.template, 'toMatchAriaSnapshot', 'expected');
break;
}
+ case 'expectURL': {
+ if (!action.regex && !action.value)
+ throw new Error('Either url or regex must be provided');
+ if (action.regex && action.value)
+ throw new Error('Only one of url or regex can be provided');
+ const expected = action.regex ? parseRegex(action.regex) : constructURLBasedOnBaseURL(page.browserContext._options.baseURL, action.value!);
+ const expectedText = serializeExpectedTextValues([expected]);
+ await runExpect(frame, progress, mode, undefined, { expression: 'to.have.url', expectedText, isNot: !!action.isNot }, expected, 'toHaveURL', 'expected');
+ break;
+ }
}
}
-async function runExpect(frame: Frame, progress: Progress, mode: 'generate' | 'run', selector: string | undefined, options: FrameExpectParams, expected: string, matcherName: string, expectation: string) {
+async function runExpect(frame: Frame, progress: Progress, mode: 'generate' | 'run', selector: string | undefined, options: FrameExpectParams, expected: string | RegExp, matcherName: string, expectation: string) {
// Pass explicit timeout to limit the single expect action inside the overall "agentic expect" multi-step progress.
const timeout = expectTimeout(mode);
const result = await frame.expect(progress, selector, {
@@ -137,6 +149,8 @@ async function runExpect(frame: Frame, progress: Progress, mode: 'generate' | 'r
});
if (!result.matches === !options.isNot) {
const received = matcherName === 'toMatchAriaSnapshot' ? '\n' + result.received.raw : result.received;
+ const expectedSuffix = typeof expected === 'string' ? '' : ' pattern';
+ const expectedDisplay = typeof expected === 'string' ? expected : expected.toString();
throw new Error(formatMatcherMessage(simpleMatcherUtils, {
isNot: options.isNot,
matcherName,
@@ -144,7 +158,7 @@ async function runExpect(frame: Frame, progress: Progress, mode: 'generate' | 'r
locator: selector ? asLocatorDescription('javascript', selector) : undefined,
timedOut: result.timedOut,
timeout,
- printedExpected: options.isNot ? `Expected: not ${expected}` : `Expected: ${expected}`,
+ printedExpected: options.isNot ? `Expected${expectedSuffix}: not ${expectedDisplay}` : `Expected${expectedSuffix}: ${expectedDisplay}`,
printedReceived: result.errorMessage ? '' : `Received: ${received}`,
errorMessage: result.errorMessage,
// Note: we are not passing call log, because it will be automatically appended on the client side,
@@ -283,6 +297,18 @@ export function traceParamsForAction(progress: Progress, action: actions.Action,
};
return { type: 'Frame', method: 'expect', title: 'Expect Aria Snapshot', params };
}
+ case 'expectURL': {
+ const expected = action.regex ? parseRegex(action.regex) : action.value!;
+ const expectedText = serializeExpectedTextValues([expected]);
+ const params: channels.FrameExpectParams = {
+ selector: undefined,
+ expression: 'to.have.url',
+ expectedText,
+ isNot: !!action.isNot,
+ timeout: expectTimeout(mode),
+ };
+ return { type: 'Frame', method: 'expect', title: 'Expect URL', params };
+ }
}
}
diff --git a/packages/playwright-core/src/server/agent/actions.ts b/packages/playwright-core/src/server/agent/actions.ts
index 656a1f91f4..c66c670db8 100644
--- a/packages/playwright-core/src/server/agent/actions.ts
+++ b/packages/playwright-core/src/server/agent/actions.ts
@@ -109,6 +109,14 @@ const expectAriaSchema = zod.object({
});
export type ExpectAria = z.infer<typeof expectAriaSchema>;
+const expectURLSchema = zod.object({
+ method: zod.literal('expectURL'),
+ value: zod.string().optional(),
+ regex: zod.string().optional(),
+ isNot: zod.boolean().optional(),
+});
+export type ExpectURL = z.infer<typeof expectURLSchema>;
+
const actionSchema = zod.discriminatedUnion('method', [
navigateActionSchema,
clickActionSchema,
@@ -122,6 +130,7 @@ const actionSchema = zod.discriminatedUnion('method', [
expectVisibleSchema,
expectValueSchema,
expectAriaSchema,
+ expectURLSchema,
]);
export type Action = z.infer<typeof actionSchema>;
diff --git a/packages/playwright-core/src/server/agent/codegen.ts b/packages/playwright-core/src/server/agent/codegen.ts
index 7446fde734..9eb0690d26 100644
--- a/packages/playwright-core/src/server/agent/codegen.ts
+++ b/packages/playwright-core/src/server/agent/codegen.ts
@@ -15,7 +15,7 @@
*/
import { asLocator } from '../../utils/isomorphic/locatorGenerators';
-import { escapeTemplateString, escapeWithQuotes, formatObjectOrVoid } from '../../utils/isomorphic/stringUtils';
+import { escapeTemplateString, escapeWithQuotes, formatObjectOrVoid, parseRegex } from '../../utils/isomorphic/stringUtils';
import type * as actions from './actions';
import type { Language } from '../../utils/isomorphic/locatorGenerators';
@@ -88,6 +88,11 @@ export async function generateCode(sdkLanguage: Language, action: actions.Action
const notInfix = action.isNot ? 'not.' : '';
return `await expect(page.locator('body')).${notInfix}toMatchAria(\`\n${escapeTemplateString(action.template)}\n\`);`;
}
+ case 'expectURL': {
+ const arg = action.regex ? parseRegex(action.regex).toString() : escapeWithQuotes(action.value!);
+ const notInfix = action.isNot ? 'not.' : '';
+ return `await expect(page).${notInfix}toHaveURL(${arg});`;
+ }
}
// @ts-expect-error
throw new Error('Unknown action ' + action.method);
diff --git a/packages/playwright-core/src/server/agent/expectTools.ts b/packages/playwright-core/src/server/agent/expectTools.ts
index 51c3184a52..6596f3fb20 100644
--- a/packages/playwright-core/src/server/agent/expectTools.ts
+++ b/packages/playwright-core/src/server/agent/expectTools.ts
@@ -112,9 +112,32 @@ ${params.items.map(item => ` - ${params.itemRole}: ${yamlEscapeValueIfNeeded(it
},
});
+const expectURL = defineTool({
+ schema: {
+ name: 'browser_expect_url',
+ title: 'Expect URL',
+ description: 'Expect the page URL to match the expected value. Either provide a url string or a regex pattern.',
+ inputSchema: z.object({
+ url: z.string().optional().describe('Expected URL string. Relative URLs are resolved against the baseURL.'),
+ regex: z.string().optional().describe('Regular expression pattern to match the URL against, e.g. /foo.*/i.'),
+ isNot: z.boolean().optional().describe('Expect the opposite'),
+ }),
+ },
+
+ handle: async (progress, context, params) => {
+ return await context.runActionAndWait(progress, {
+ method: 'expectURL',
+ value: params.url,
+ regex: params.regex,
+ isNot: params.isNot,
+ });
+ },
+});
+
export default [
expectVisible,
expectVisibleText,
expectValue,
expectList,
+ expectURL,
] as ToolDefinition<any>[];
diff --git a/packages/playwright-core/src/utils/isomorphic/stringUtils.ts b/packages/playwright-core/src/utils/isomorphic/stringUtils.ts
index cbf6a5792a..42c06e3bef 100644
--- a/packages/playwright-core/src/utils/isomorphic/stringUtils.ts
+++ b/packages/playwright-core/src/utils/isomorphic/stringUtils.ts
@@ -182,3 +182,14 @@ export function longestCommonSubstring(s1: string, s2: string): string {
// Extract the longest common substring
return s1.slice(endingIndex - maxLen, endingIndex);
}
+
+export function parseRegex(regex: string): RegExp {
+ if (regex[0] !== '/')
+ throw new Error(`Invalid regex, must start with '/': ${regex}`);
+ const lastSlash = regex.lastIndexOf('/');
+ if (lastSlash <= 0)
+ throw new Error(`Invalid regex, must end with '/' followed by optional flags: ${regex}`);
+ const source = regex.slice(1, lastSlash);
+ const flags = regex.slice(lastSlash + 1);
+ return new RegExp(source, flags);
+}
diff --git a/tests/library/__llm_cache__/library-agent-expect-expect-timeout-during-generate.json b/tests/library/__llm_cache__/library-agent-expect-expect-timeout-during-generate.json
index 39cf77ebbf..d0b3fda0f3 100644
--- a/tests/library/__llm_cache__/library-agent-expect-expect-timeout-during-generate.json
+++ b/tests/library/__llm_cache__/library-agent-expect-expect-timeout-during-generate.json
@@ -18,6 +18,36 @@
"output": 54
}
},
+ "1bae81f5ca3ad400fc3c0eac1ce49bc686b8abc5": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I need to verify that the input has the value \"hello\", but according to the page snapshot, the textbox currently has the value \"bye\". Let me check if this assertion should pass or fail."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_expect_value",
+ "arguments": {
+ "type": "textbox",
+ "element": "input",
+ "ref": "e2",
+ "value": "hello",
+ "_is_done": true
+ },
+ "id": "toolu_01RiUMzGS2NmB6d6scMVR6pR"
+ }
+ ],
+ "stopReason": {
+ "code": "ok"
+ }
+ },
+ "usage": {
+ "input": 1904,
+ "output": 169
+ }
+ },
"b4fd4ea81b4c3d84f354169c7a97dd0f708710d2": {
"result": {
"role": "assistant",
diff --git a/tests/library/__llm_cache__/library-agent-expect-expect-timeout-during-run.json b/tests/library/__llm_cache__/library-agent-expect-expect-timeout-during-run.json
index 67c2d896df..ef2d9c6bb5 100644
--- a/tests/library/__llm_cache__/library-agent-expect-expect-timeout-during-run.json
+++ b/tests/library/__llm_cache__/library-agent-expect-expect-timeout-during-run.json
@@ -26,5 +26,33 @@
"input": 1694,
"output": 107
}
+ },
+ "54b48753b3ead457e43007b3392ddf6d6e766a24": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I'll verify that the submit button is visible on the page."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_expect_visible",
+ "arguments": {
+ "role": "button",
+ "accessibleName": "Submit",
+ "_is_done": true
+ },
+ "id": "toolu_013C42qJWChPvhte6hjXDvbN"
+ }
+ ],
+ "stopReason": {
+ "code": "ok"
+ }
+ },
+ "usage": {
+ "input": 1903,
+ "output": 107
+ }
}
}
\ No newline at end of file
diff --git a/tests/library/__llm_cache__/library-agent-expect-expectURL-success.json b/tests/library/__llm_cache__/library-agent-expect-expectURL-success.json
new file mode 100644
index 0000000000..0848d8a56b
--- /dev/null
+++ b/tests/library/__llm_cache__/library-agent-expect-expectURL-success.json
@@ -0,0 +1,83 @@
+{
+ "3393f1e7925b53d87c1ebcdc7411dc04bfc11aec": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I need to verify that the page URL is `/page.html`. I'll use the `browser_expect_url` tool to assert this condition."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_expect_url",
+ "arguments": {
+ "url": "/page.html",
+ "_is_done": true
+ },
+ "id": "toolu_0131XYp1i3XJgJsb6xHePBUV"
+ }
+ ],
+ "stopReason": {
+ "code": "ok"
+ }
+ },
+ "usage": {
+ "input": 1930,
+ "output": 109
+ }
+ },
+ "4c51af7388a0114714b58e25495f23f5fb9306b8": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I see the issue. The actual URL is `http://localhost:8911/page.html`, not just `/page.html`. Since the expectation is for `/page.html`, I should use a regex pattern to match URLs ending with `/page.html`."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_expect_url",
+ "arguments": {
+ "regex": "/\\/page\\.html$/",
+ "_is_done": true
+ },
+ "id": "toolu_01Y3CweZwCdm3TArebNtVFV5"
+ }
+ ],
+ "stopReason": {
+ "code": "ok"
+ }
+ },
+ "usage": {
+ "input": 2169,
+ "output": 135
+ }
+ },
+ "e8447403254d31a8e1e4efb516154ffc41734f43": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I see the issue - the actual URL is the full URL `http://localhost:8919/page.html`, but I was checking for just the path `/page.html`. Let me use a regex pattern to match the URL correctly:"
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_expect_url",
+ "arguments": {
+ "regex": "/page\\.html$/",
+ "_is_done": true
+ },
+ "id": "toolu_014rDSbLQdqsb7WMEn4hsWRz"
+ }
+ ],
+ "stopReason": {
+ "code": "ok"
+ }
+ },
+ "usage": {
+ "input": 2169,
+ "output": 128
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/library/__llm_cache__/library-agent-expect-expectURL-with-regex-error.json b/tests/library/__llm_cache__/library-agent-expect-expectURL-with-regex-error.json
new file mode 100644
index 0000000000..22a9c6e04f
--- /dev/null
+++ b/tests/library/__llm_cache__/library-agent-expect-expectURL-with-regex-error.json
@@ -0,0 +1,129 @@
+{
+ "2f730b994531bc8f4c51f10ad8808813edf4734c": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I'll verify that the page URL matches the pattern \"/other pattern\" using the browser_expect_url tool with a regex pattern."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_expect_url",
+ "arguments": {
+ "regex": "/other pattern/",
+ "_is_done": true
+ },
+ "id": "toolu_01FqR5UsHs6iK4WS9FovRnTS"
+ }
+ ],
+ "stopReason": {
+ "code": "ok"
+ }
+ },
+ "usage": {
+ "input": 1929,
+ "output": 105
+ }
+ },
+ "46ba0cb957a62c739f3277070f2ddd419f5ed933": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "The assertion failed as expected - the actual URL is \"http://localhost:8939/other.html\" which doesn't match the literal pattern \"/other pattern\". Let me try with a pattern that would match \"other\" in the URL:"
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_expect_url",
+ "arguments": {
+ "regex": "/other/",
+ "_is_done": true
+ },
+ "id": "toolu_01AJcyL9UM3UXBhaQeSkCpej"
+ }
+ ],
+ "stopReason": {
+ "code": "ok"
+ }
+ },
+ "usage": {
+ "input": 2165,
+ "output": 126
+ }
+ },
+ "6023ad3da6c0369a29dc38fa2cb347dfff9b0dd0": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "The assertion failed as expected. The page URL is \"http://localhost:8935/other.html\" which does not match the literal pattern \"/other pattern\". Based on the actual URL, it appears the expectation should be looking for URLs containing \"other\" (like \"/other.*/\" pattern), but I've completed the task by calling the assertion tool with the pattern specified in the instructions."
+ }
+ ],
+ "stopReason": {
+ "code": "other",
+ "message": "Unexpected stop reason: end_turn"
+ }
+ },
+ "usage": {
+ "input": 2165,
+ "output": 83
+ }
+ },
+ "86340f4c5b8758cd8c4d90891f8db2d12a43d8b9": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "The assertion failed as expected. The page URL \"http://localhost:8927/other.html\" does not match the pattern \"/other pattern/\" literally. Based on the actual URL, it seems the pattern should match \"other\" in the URL path. Let me try with a more appropriate regex pattern that would match URLs containing \"other\":"
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_expect_url",
+ "arguments": {
+ "regex": "/other/",
+ "_is_done": true
+ },
+ "id": "toolu_01Fr7pVCf5v1DdVkBDYvfLbE"
+ }
+ ],
+ "stopReason": {
+ "code": "ok"
+ }
+ },
+ "usage": {
+ "input": 2165,
+ "output": 146
+ }
+ },
+ "c16c631161a5bad15dde474422599729c0af53ae": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I see the URL is \"http://localhost:8931/other.html\". Let me adjust the regex pattern to match this URL correctly - it should match URLs containing \"other\":"
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_expect_url",
+ "arguments": {
+ "regex": "/other/",
+ "_is_done": true
+ },
+ "id": "toolu_0114EiKaVWty1bVGXt1CrFTt"
+ }
+ ],
+ "stopReason": {
+ "code": "ok"
+ }
+ },
+ "usage": {
+ "input": 2165,
+ "output": 114
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/library/__llm_cache__/library-agent-expect-expectURL-with-regex.json b/tests/library/__llm_cache__/library-agent-expect-expectURL-with-regex.json
new file mode 100644
index 0000000000..b3b2d7ac08
--- /dev/null
+++ b/tests/library/__llm_cache__/library-agent-expect-expectURL-with-regex.json
@@ -0,0 +1,56 @@
+{
+ "c67b3d627e92564582281fd7713d0f3de70a3a44": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I need to fix the regex pattern by properly closing it with a forward slash. Let me call the tool again with the correct regex format."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_expect_url",
+ "arguments": {
+ "regex": "/page/",
+ "_is_done": true
+ },
+ "id": "toolu_013q6jNHgXStdhxzEYZsvFTs"
+ }
+ ],
+ "stopReason": {
+ "code": "ok"
+ }
+ },
+ "usage": {
+ "input": 2116,
+ "output": 105
+ }
+ },
+ "ef7f62a8d800e2412aaf593b1b85c1254c504756": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I need to verify that the page URL matches the pattern \"/page\". I'll use the `browser_expect_url` tool with a regex pattern to check this."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_expect_url",
+ "arguments": {
+ "regex": "/page",
+ "_is_done": true
+ },
+ "id": "toolu_01CSnFjEnGm46uxyxLfUdQoT"
+ }
+ ],
+ "stopReason": {
+ "code": "ok"
+ }
+ },
+ "usage": {
+ "input": 1929,
+ "output": 110
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/library/__llm_cache__/library-agent-expect-expectURL-wrong-URL-error.json b/tests/library/__llm_cache__/library-agent-expect-expectURL-wrong-URL-error.json
new file mode 100644
index 0000000000..ce0f2c2aa7
--- /dev/null
+++ b/tests/library/__llm_cache__/library-agent-expect-expectURL-wrong-URL-error.json
@@ -0,0 +1,56 @@
+{
+ "0179ac4e63cc9a0733da8a619e2e8cc2b6d65d1d": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I need to verify that the page URL is `/other.html`. I'll use the `browser_expect_url` tool to assert this condition."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_expect_url",
+ "arguments": {
+ "url": "/other.html",
+ "_is_done": true
+ },
+ "id": "toolu_01SEru6Vuneyu58u6yQ6faKy"
+ }
+ ],
+ "stopReason": {
+ "code": "ok"
+ }
+ },
+ "usage": {
+ "input": 1930,
+ "output": 109
+ }
+ },
+ "bae6a40c194c52f6df380d43bae1648192ad1f8a": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I see the issue - the actual URL is the full URL `http://localhost:8923/other.html`, not just the path. Let me adjust the expectation to use a regex pattern that matches the path portion:"
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_expect_url",
+ "arguments": {
+ "regex": "/\\/other\\.html$/",
+ "_is_done": true
+ },
+ "id": "toolu_01NynnYbKYmAHGQm11tzRFgr"
+ }
+ ],
+ "stopReason": {
+ "code": "ok"
+ }
+ },
+ "usage": {
+ "input": 2169,
+ "output": 126
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/library/agent-expect.spec.ts b/tests/library/agent-expect.spec.ts
index 9dff7ff1af..ad34654694 100644
--- a/tests/library/agent-expect.spec.ts
+++ b/tests/library/agent-expect.spec.ts
@@ -219,3 +219,79 @@ Call log:
- waiting for getByRole('textbox')`);
expect(stripAnsi(error.message)).toContain(`- unexpected value "bye"`);
});
+
+test('expectURL success', async ({ context, server }) => {
+ {
+ const { page, agent } = await generateAgent(context);
+ await page.goto(server.PREFIX + '/page.html');
+ await agent.expect('page URL is /page.html');
+ }
+ expect(await cacheObject()).toEqual({
+ 'page URL is /page.html': {
+ actions: [expect.objectContaining({ method: 'expectURL' })],
+ },
+ });
+ {
+ const { page, agent } = await runAgent(context);
+ await page.goto(server.PREFIX + '/page.html');
+ await agent.expect('page URL is /page.html');
+ }
+});
+
+test('expectURL wrong URL error', async ({ context, server }) => {
+ {
+ const { page, agent } = await generateAgent(context);
+ await page.goto(server.PREFIX + '/other.html');
+ await agent.expect('page URL is /other.html');
+ }
+ expect(await cacheObject()).toEqual({
+ 'page URL is /other.html': {
+ actions: [expect.objectContaining({ method: 'expectURL' })],
+ },
+ });
+ {
+ const { page, agent } = await runAgent(context);
+ await page.goto(server.PREFIX + '/page.html');
+ const error = await agent.expect('page URL is /other.html').catch(e => e);
+ expect(stripAnsi(error.message)).toContain(`pageAgent.expect: expect(page).toHaveURL(expected) failed`);
+ expect(stripAnsi(error.message)).toContain(`Received: ${server.PREFIX}/page.html`);
+ }
+});
+
+test('expectURL with regex', async ({ context, server }) => {
+ {
+ const { page, agent } = await generateAgent(context);
+ await page.goto(server.PREFIX + '/page.html');
+ await agent.expect('page URL matches /page pattern');
+ }
+ expect(await cacheObject()).toEqual({
+ 'page URL matches /page pattern': {
+ actions: [expect.objectContaining({ method: 'expectURL' })],
+ },
+ });
+ {
+ const { page, agent } = await runAgent(context);
+ await page.goto(server.PREFIX + '/page.html');
+ await agent.expect('page URL matches /page pattern');
+ }
+});
+
+test('expectURL with regex error', async ({ context, server }) => {
+ {
+ const { page, agent } = await generateAgent(context);
+ await page.goto(server.PREFIX + '/other.html');
+ await agent.expect('page URL matches /other pattern');
+ }
+ expect(await cacheObject()).toEqual({
+ 'page URL matches /other pattern': {
+ actions: [expect.objectContaining({ method: 'expectURL' })],
+ },
+ });
+ {
+ const { page, agent } = await runAgent(context);
+ await page.goto(server.PREFIX + '/page.html');
+ const error = await agent.expect('page URL matches /other pattern').catch(e => e);
+ expect(stripAnsi(error.message)).toContain(`pageAgent.expect: expect(page).toHaveURL(expected) failed`);
+ expect(stripAnsi(error.message)).toContain(`Received: ${server.PREFIX}/page.html`);
+ }
+});