verify.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var verify_exports = {};
  30. __export(verify_exports, {
  31. default: () => verify_default
  32. });
  33. module.exports = __toCommonJS(verify_exports);
  34. var import_bundle = require("../../sdk/bundle");
  35. var import_tool = require("./tool");
  36. var javascript = __toESM(require("../codegen"));
  37. const verifyElement = (0, import_tool.defineTabTool)({
  38. capability: "testing",
  39. schema: {
  40. name: "browser_verify_element_visible",
  41. title: "Verify element visible",
  42. description: "Verify element is visible on the page",
  43. inputSchema: import_bundle.z.object({
  44. role: import_bundle.z.string().describe('ROLE of the element. Can be found in the snapshot like this: `- {ROLE} "Accessible Name":`'),
  45. accessibleName: import_bundle.z.string().describe('ACCESSIBLE_NAME of the element. Can be found in the snapshot like this: `- role "{ACCESSIBLE_NAME}"`')
  46. }),
  47. type: "assertion"
  48. },
  49. handle: async (tab, params, response) => {
  50. const locator = tab.page.getByRole(params.role, { name: params.accessibleName });
  51. if (await locator.count() === 0) {
  52. response.addError(`Element with role "${params.role}" and accessible name "${params.accessibleName}" not found`);
  53. return;
  54. }
  55. response.addCode(`await expect(page.getByRole(${javascript.escapeWithQuotes(params.role)}, { name: ${javascript.escapeWithQuotes(params.accessibleName)} })).toBeVisible();`);
  56. response.addResult("Done");
  57. }
  58. });
  59. const verifyText = (0, import_tool.defineTabTool)({
  60. capability: "testing",
  61. schema: {
  62. name: "browser_verify_text_visible",
  63. title: "Verify text visible",
  64. description: `Verify text is visible on the page. Prefer ${verifyElement.schema.name} if possible.`,
  65. inputSchema: import_bundle.z.object({
  66. text: import_bundle.z.string().describe('TEXT to verify. Can be found in the snapshot like this: `- role "Accessible Name": {TEXT}` or like this: `- text: {TEXT}`')
  67. }),
  68. type: "assertion"
  69. },
  70. handle: async (tab, params, response) => {
  71. const locator = tab.page.getByText(params.text).filter({ visible: true });
  72. if (await locator.count() === 0) {
  73. response.addError("Text not found");
  74. return;
  75. }
  76. response.addCode(`await expect(page.getByText(${javascript.escapeWithQuotes(params.text)})).toBeVisible();`);
  77. response.addResult("Done");
  78. }
  79. });
  80. const verifyList = (0, import_tool.defineTabTool)({
  81. capability: "testing",
  82. schema: {
  83. name: "browser_verify_list_visible",
  84. title: "Verify list visible",
  85. description: "Verify list is visible on the page",
  86. inputSchema: import_bundle.z.object({
  87. element: import_bundle.z.string().describe("Human-readable list description"),
  88. ref: import_bundle.z.string().describe("Exact target element reference that points to the list"),
  89. items: import_bundle.z.array(import_bundle.z.string()).describe("Items to verify")
  90. }),
  91. type: "assertion"
  92. },
  93. handle: async (tab, params, response) => {
  94. const { locator } = await tab.refLocator({ ref: params.ref, element: params.element });
  95. const itemTexts = [];
  96. for (const item of params.items) {
  97. const itemLocator = locator.getByText(item);
  98. if (await itemLocator.count() === 0) {
  99. response.addError(`Item "${item}" not found`);
  100. return;
  101. }
  102. itemTexts.push(await itemLocator.textContent());
  103. }
  104. const ariaSnapshot = `\`
  105. - list:
  106. ${itemTexts.map((t) => ` - listitem: ${javascript.escapeWithQuotes(t, '"')}`).join("\n")}
  107. \``;
  108. response.addCode(`await expect(page.locator('body')).toMatchAriaSnapshot(${ariaSnapshot});`);
  109. response.addResult("Done");
  110. }
  111. });
  112. const verifyValue = (0, import_tool.defineTabTool)({
  113. capability: "testing",
  114. schema: {
  115. name: "browser_verify_value",
  116. title: "Verify value",
  117. description: "Verify element value",
  118. inputSchema: import_bundle.z.object({
  119. type: import_bundle.z.enum(["textbox", "checkbox", "radio", "combobox", "slider"]).describe("Type of the element"),
  120. element: import_bundle.z.string().describe("Human-readable element description"),
  121. ref: import_bundle.z.string().describe("Exact target element reference that points to the element"),
  122. value: import_bundle.z.string().describe('Value to verify. For checkbox, use "true" or "false".')
  123. }),
  124. type: "assertion"
  125. },
  126. handle: async (tab, params, response) => {
  127. const { locator, resolved } = await tab.refLocator({ ref: params.ref, element: params.element });
  128. const locatorSource = `page.${resolved}`;
  129. if (params.type === "textbox" || params.type === "slider" || params.type === "combobox") {
  130. const value = await locator.inputValue();
  131. if (value !== params.value) {
  132. response.addError(`Expected value "${params.value}", but got "${value}"`);
  133. return;
  134. }
  135. response.addCode(`await expect(${locatorSource}).toHaveValue(${javascript.quote(params.value)});`);
  136. } else if (params.type === "checkbox" || params.type === "radio") {
  137. const value = await locator.isChecked();
  138. if (value !== (params.value === "true")) {
  139. response.addError(`Expected value "${params.value}", but got "${value}"`);
  140. return;
  141. }
  142. const matcher = value ? "toBeChecked" : "not.toBeChecked";
  143. response.addCode(`await expect(${locatorSource}).${matcher}();`);
  144. }
  145. response.addResult("Done");
  146. }
  147. });
  148. var verify_default = [
  149. verifyElement,
  150. verifyText,
  151. verifyList,
  152. verifyValue
  153. ];