|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { z } from 'zod'; |
| 18 | + |
| 19 | +import { defineTabTool } from './tool.js'; |
| 20 | +import * as javascript from '../utils/codegen.js'; |
| 21 | +import { generateLocator } from './utils.js'; |
| 22 | + |
| 23 | +const verifyElement = defineTabTool({ |
| 24 | + capability: 'verify', |
| 25 | + schema: { |
| 26 | + name: 'browser_verify_element_visible', |
| 27 | + title: 'Verify element visible', |
| 28 | + description: 'Verify element is visible on the page', |
| 29 | + inputSchema: z.object({ |
| 30 | + role: z.string().describe('ROLE of the element. Can be found in the snapshot like this: \`- {ROLE} "Accessible Name":\`'), |
| 31 | + accessibleName: z.string().describe('ACCESSIBLE_NAME of the element. Can be found in the snapshot like this: \`- role "{ACCESSIBLE_NAME}"\`'), |
| 32 | + }), |
| 33 | + type: 'readOnly', |
| 34 | + }, |
| 35 | + |
| 36 | + handle: async (tab, params, response) => { |
| 37 | + const locator = tab.page.getByRole(params.role as any, { name: params.accessibleName }); |
| 38 | + if (await locator.count() === 0) { |
| 39 | + response.addError(`Element with role "${params.role}" and accessible name "${params.accessibleName}" not found`); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + response.addCode(`await expect(page.getByRole(${javascript.escapeWithQuotes(params.role)}, { name: ${javascript.escapeWithQuotes(params.accessibleName)} })).toBeVisible();`); |
| 44 | + response.addResult('Done'); |
| 45 | + }, |
| 46 | +}); |
| 47 | + |
| 48 | +const verifyText = defineTabTool({ |
| 49 | + capability: 'verify', |
| 50 | + schema: { |
| 51 | + name: 'browser_verify_text_visible', |
| 52 | + title: 'Verify text visible', |
| 53 | + description: `Verify text is visible on the page. Prefer ${verifyElement.schema.name} if possible.`, |
| 54 | + inputSchema: z.object({ |
| 55 | + text: z.string().describe('TEXT to verify. Can be found in the snapshot like this: \`- role "Accessible Name": {TEXT}\` or like this: \`- text: {TEXT}\`'), |
| 56 | + }), |
| 57 | + type: 'readOnly', |
| 58 | + }, |
| 59 | + |
| 60 | + handle: async (tab, params, response) => { |
| 61 | + const locator = tab.page.getByText(params.text).filter({ visible: true }); |
| 62 | + if (await locator.count() === 0) { |
| 63 | + response.addError('Text not found'); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + response.addCode(`await expect(page.getByText(${javascript.escapeWithQuotes(params.text)})).toBeVisible();`); |
| 68 | + response.addResult('Done'); |
| 69 | + }, |
| 70 | +}); |
| 71 | + |
| 72 | +const verifyList = defineTabTool({ |
| 73 | + capability: 'verify', |
| 74 | + schema: { |
| 75 | + name: 'browser_verify_list_visible', |
| 76 | + title: 'Verify list visible', |
| 77 | + description: 'Verify list is visible on the page', |
| 78 | + inputSchema: z.object({ |
| 79 | + element: z.string().describe('Human-readable list description'), |
| 80 | + ref: z.string().describe('Exact target element reference that points to the list'), |
| 81 | + items: z.array(z.string()).describe('Items to verify'), |
| 82 | + }), |
| 83 | + type: 'readOnly', |
| 84 | + }, |
| 85 | + |
| 86 | + handle: async (tab, params, response) => { |
| 87 | + const locator = await tab.refLocator({ ref: params.ref, element: params.element }); |
| 88 | + const itemTexts: string[] = []; |
| 89 | + for (const item of params.items) { |
| 90 | + const itemLocator = locator.getByText(item); |
| 91 | + if (await itemLocator.count() === 0) { |
| 92 | + response.addError(`Item "${item}" not found`); |
| 93 | + return; |
| 94 | + } |
| 95 | + itemTexts.push((await itemLocator.textContent())!); |
| 96 | + } |
| 97 | + const ariaSnapshot = `\` |
| 98 | +- list: |
| 99 | +${itemTexts.map(t => ` - text: ${javascript.escapeWithQuotes(t, '"')}`).join('\n')} |
| 100 | +\``; |
| 101 | + response.addCode(`await expect(page.locator('body')).toMatchAriaSnapshot(${ariaSnapshot});`); |
| 102 | + response.addResult('Done'); |
| 103 | + }, |
| 104 | +}); |
| 105 | + |
| 106 | +const verifyValue = defineTabTool({ |
| 107 | + capability: 'verify', |
| 108 | + schema: { |
| 109 | + name: 'browser_verify_value', |
| 110 | + title: 'Verify value', |
| 111 | + description: 'Verify element value', |
| 112 | + inputSchema: z.object({ |
| 113 | + type: z.enum(['textbox', 'checkbox', 'radio', 'combobox', 'slider']).describe('Type of the element'), |
| 114 | + element: z.string().describe('Human-readable element description'), |
| 115 | + ref: z.string().describe('Exact target element reference that points to the element'), |
| 116 | + value: z.string().describe('Value to verify. For checkbox, use "true" or "false".'), |
| 117 | + }), |
| 118 | + type: 'readOnly', |
| 119 | + }, |
| 120 | + |
| 121 | + handle: async (tab, params, response) => { |
| 122 | + const locator = await tab.refLocator({ ref: params.ref, element: params.element }); |
| 123 | + const locatorSource = `page.${await generateLocator(locator)}`; |
| 124 | + if (params.type === 'textbox' || params.type === 'slider' || params.type === 'combobox') { |
| 125 | + const value = await locator.inputValue(); |
| 126 | + if (value !== params.value) { |
| 127 | + response.addError(`Expected value "${params.value}", but got "${value}"`); |
| 128 | + return; |
| 129 | + } |
| 130 | + response.addCode(`await expect(${locatorSource}).toHaveValue(${javascript.quote(params.value)});`); |
| 131 | + } else if (params.type === 'checkbox' || params.type === 'radio') { |
| 132 | + const value = await locator.isChecked(); |
| 133 | + if (value !== (params.value === 'true')) { |
| 134 | + response.addError(`Expected value "${params.value}", but got "${value}"`); |
| 135 | + return; |
| 136 | + } |
| 137 | + const matcher = value ? 'toBeChecked' : 'not.toBeChecked'; |
| 138 | + response.addCode(`await expect(${locatorSource}).${matcher}();`); |
| 139 | + } |
| 140 | + response.addResult('Done'); |
| 141 | + }, |
| 142 | +}); |
| 143 | + |
| 144 | +export default [ |
| 145 | + verifyElement, |
| 146 | + verifyText, |
| 147 | + verifyList, |
| 148 | + verifyValue, |
| 149 | +]; |
0 commit comments