@probolabs/playwright 0.4.21 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +227 -136
- package/dist/fixtures.d.ts +28 -0
- package/dist/fixtures.js +52 -0
- package/dist/fixtures.js.map +1 -0
- package/dist/index.d.ts +91 -20
- package/dist/index.js +413 -610
- package/dist/index.js.map +1 -1
- package/dist/types/actions.d.ts.map +1 -1
- package/dist/types/fixtures.d.ts.map +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/nav-tracker.d.ts.map +1 -0
- package/dist/types/replay-utils.d.ts.map +1 -1
- package/package.json +7 -2
package/dist/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
const highlighterCode = "(function (global, factory) {\n typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :\n typeof define === 'function' && define.amd ? define(['exports'], factory) :\n (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ProboLabs = {}));\n})(this, (function (exports) { 'use strict';\n\n const ElementTag = {\r\n CLICKABLE: \"CLICKABLE\", // button, link, toggle switch, checkbox, radio, dropdowns, clickable divs\r\n FILLABLE: \"FILLABLE\", // input, textarea content_editable, date picker??\r\n SELECTABLE: \"SELECTABLE\", // select\r\n NON_INTERACTIVE_ELEMENT: 'NON_INTERACTIVE_ELEMENT',\r\n };\r\n\r\n class ElementInfo {\r\n constructor(element, index, {tag, type, text, html, xpath, css_selector, bounding_box, iframe_selector, short_css_selector, short_iframe_selector}) {\r\n this.index = index.toString();\r\n this.tag = tag;\r\n this.type = type;\r\n this.text = text;\r\n this.html = html;\r\n this.xpath = xpath;\r\n this.css_selector = css_selector;\r\n this.bounding_box = bounding_box;\r\n this.iframe_selector = iframe_selector;\r\n this.element = element;\r\n this.depth = -1;\r\n this.short_css_selector = short_css_selector;\r\n this.short_iframe_selector = short_iframe_selector;\r\n }\r\n\r\n getSelector() {\r\n return this.xpath ? this.xpath : this.css_selector;\r\n }\r\n\r\n getDepth() {\r\n if (this.depth >= 0) {\r\n return this.depth;\r\n }\r\n \r\n this.depth = 0;\r\n let currentElement = this.element;\r\n \r\n while (currentElement.nodeType === Node.ELEMENT_NODE) { \r\n this.depth++;\r\n if (currentElement.assignedSlot) {\r\n currentElement = currentElement.assignedSlot;\r\n }\r\n else {\r\n currentElement = currentElement.parentNode;\r\n // Check if we're at a shadow root\r\n if (currentElement && currentElement.nodeType !== Node.ELEMENT_NODE && currentElement.getRootNode() instanceof ShadowRoot) {\r\n // Get the shadow root's host element\r\n currentElement = currentElement.getRootNode().host; \r\n }\r\n }\r\n }\r\n \r\n return this.depth;\r\n }\r\n }\n\n // License: MIT\n // Author: Anton Medvedev <anton@medv.io>\n // Source: https://github.com/antonmedv/finder\n const acceptedAttrNames = new Set(['role', 'name', 'aria-label', 'rel', 'href']);\n /** Check if attribute name and value are word-like. */\n function attr(name, value) {\n let nameIsOk = acceptedAttrNames.has(name);\n nameIsOk ||= name.startsWith('data-') && wordLike(name);\n let valueIsOk = wordLike(value) && value.length < 100;\n valueIsOk ||= value.startsWith('#') && wordLike(value.slice(1));\n return nameIsOk && valueIsOk;\n }\n /** Check if id name is word-like. */\n function idName(name) {\n return wordLike(name);\n }\n /** Check if class name is word-like. */\n function className(name) {\n return wordLike(name);\n }\n /** Check if tag name is word-like. */\n function tagName(name) {\n return true;\n }\n /** Finds unique CSS selectors for the given element. */\n function finder(input, options) {\n if (input.nodeType !== Node.ELEMENT_NODE) {\n throw new Error(`Can't generate CSS selector for non-element node type.`);\n }\n if (input.tagName.toLowerCase() === 'html') {\n return 'html';\n }\n const defaults = {\n root: document.body,\n idName: idName,\n className: className,\n tagName: tagName,\n attr: attr,\n timeoutMs: 1000,\n seedMinLength: 3,\n optimizedMinLength: 2,\n maxNumberOfPathChecks: Infinity,\n };\n const startTime = new Date();\n const config = { ...defaults, ...options };\n const rootDocument = findRootDocument(config.root, defaults);\n let foundPath;\n let count = 0;\n for (const candidate of search(input, config, rootDocument)) {\n const elapsedTimeMs = new Date().getTime() - startTime.getTime();\n if (elapsedTimeMs > config.timeoutMs ||\n count >= config.maxNumberOfPathChecks) {\n const fPath = fallback(input, rootDocument);\n if (!fPath) {\n throw new Error(`Timeout: Can't find a unique selector after ${config.timeoutMs}ms`);\n }\n return selector(fPath);\n }\n count++;\n if (unique(candidate, rootDocument)) {\n foundPath = candidate;\n break;\n }\n }\n if (!foundPath) {\n throw new Error(`Selector was not found.`);\n }\n const optimized = [\n ...optimize(foundPath, input, config, rootDocument, startTime),\n ];\n optimized.sort(byPenalty);\n if (optimized.length > 0) {\n return selector(optimized[0]);\n }\n return selector(foundPath);\n }\n function* search(input, config, rootDocument) {\n const stack = [];\n let paths = [];\n let current = input;\n let i = 0;\n while (current && current !== rootDocument) {\n const level = tie(current, config);\n for (const node of level) {\n node.level = i;\n }\n stack.push(level);\n current = current.parentElement;\n i++;\n paths.push(...combinations(stack));\n if (i >= config.seedMinLength) {\n paths.sort(byPenalty);\n for (const candidate of paths) {\n yield candidate;\n }\n paths = [];\n }\n }\n paths.sort(byPenalty);\n for (const candidate of paths) {\n yield candidate;\n }\n }\n function wordLike(name) {\n if (/^[a-z\\-]{3,}$/i.test(name)) {\n const words = name.split(/-|[A-Z]/);\n for (const word of words) {\n if (word.length <= 2) {\n return false;\n }\n if (/[^aeiou]{4,}/i.test(word)) {\n return false;\n }\n }\n return true;\n }\n return false;\n }\n function tie(element, config) {\n const level = [];\n const elementId = element.getAttribute('id');\n if (elementId && config.idName(elementId)) {\n level.push({\n name: '#' + CSS.escape(elementId),\n penalty: 0,\n });\n }\n for (let i = 0; i < element.classList.length; i++) {\n const name = element.classList[i];\n if (config.className(name)) {\n level.push({\n name: '.' + CSS.escape(name),\n penalty: 1,\n });\n }\n }\n for (let i = 0; i < element.attributes.length; i++) {\n const attr = element.attributes[i];\n if (config.attr(attr.name, attr.value)) {\n level.push({\n name: `[${CSS.escape(attr.name)}=\"${CSS.escape(attr.value)}\"]`,\n penalty: 2,\n });\n }\n }\n const tagName = element.tagName.toLowerCase();\n if (config.tagName(tagName)) {\n level.push({\n name: tagName,\n penalty: 5,\n });\n const index = indexOf(element, tagName);\n if (index !== undefined) {\n level.push({\n name: nthOfType(tagName, index),\n penalty: 10,\n });\n }\n }\n const nth = indexOf(element);\n if (nth !== undefined) {\n level.push({\n name: nthChild(tagName, nth),\n penalty: 50,\n });\n }\n return level;\n }\n function selector(path) {\n let node = path[0];\n let query = node.name;\n for (let i = 1; i < path.length; i++) {\n const level = path[i].level || 0;\n if (node.level === level - 1) {\n query = `${path[i].name} > ${query}`;\n }\n else {\n query = `${path[i].name} ${query}`;\n }\n node = path[i];\n }\n return query;\n }\n function penalty(path) {\n return path.map((node) => node.penalty).reduce((acc, i) => acc + i, 0);\n }\n function byPenalty(a, b) {\n return penalty(a) - penalty(b);\n }\n function indexOf(input, tagName) {\n const parent = input.parentNode;\n if (!parent) {\n return undefined;\n }\n let child = parent.firstChild;\n if (!child) {\n return undefined;\n }\n let i = 0;\n while (child) {\n if (child.nodeType === Node.ELEMENT_NODE &&\n (tagName === undefined ||\n child.tagName.toLowerCase() === tagName)) {\n i++;\n }\n if (child === input) {\n break;\n }\n child = child.nextSibling;\n }\n return i;\n }\n function fallback(input, rootDocument) {\n let i = 0;\n let current = input;\n const path = [];\n while (current && current !== rootDocument) {\n const tagName = current.tagName.toLowerCase();\n const index = indexOf(current, tagName);\n if (index === undefined) {\n return;\n }\n path.push({\n name: nthOfType(tagName, index),\n penalty: NaN,\n level: i,\n });\n current = current.parentElement;\n i++;\n }\n if (unique(path, rootDocument)) {\n return path;\n }\n }\n function nthChild(tagName, index) {\n if (tagName === 'html') {\n return 'html';\n }\n return `${tagName}:nth-child(${index})`;\n }\n function nthOfType(tagName, index) {\n if (tagName === 'html') {\n return 'html';\n }\n return `${tagName}:nth-of-type(${index})`;\n }\n function* combinations(stack, path = []) {\n if (stack.length > 0) {\n for (let node of stack[0]) {\n yield* combinations(stack.slice(1, stack.length), path.concat(node));\n }\n }\n else {\n yield path;\n }\n }\n function findRootDocument(rootNode, defaults) {\n if (rootNode.nodeType === Node.DOCUMENT_NODE) {\n return rootNode;\n }\n if (rootNode === defaults.root) {\n return rootNode.ownerDocument;\n }\n return rootNode;\n }\n function unique(path, rootDocument) {\n const css = selector(path);\n switch (rootDocument.querySelectorAll(css).length) {\n case 0:\n throw new Error(`Can't select any node with this selector: ${css}`);\n case 1:\n return true;\n default:\n return false;\n }\n }\n function* optimize(path, input, config, rootDocument, startTime) {\n if (path.length > 2 && path.length > config.optimizedMinLength) {\n for (let i = 1; i < path.length - 1; i++) {\n const elapsedTimeMs = new Date().getTime() - startTime.getTime();\n if (elapsedTimeMs > config.timeoutMs) {\n return;\n }\n const newPath = [...path];\n newPath.splice(i, 1);\n if (unique(newPath, rootDocument) &&\n rootDocument.querySelector(selector(newPath)) === input) {\n yield newPath;\n yield* optimize(newPath, input, config, rootDocument, startTime);\n }\n }\n }\n }\n\n // import { realpath } from \"fs\";\r\n\r\n function getAllDocumentElementsIncludingShadow(selectors, root = document) {\r\n const elements = Array.from(root.querySelectorAll(selectors));\r\n\r\n root.querySelectorAll('*').forEach(el => {\r\n if (el.shadowRoot) {\r\n elements.push(...getAllDocumentElementsIncludingShadow(selectors, el.shadowRoot));\r\n }\r\n });\r\n return elements;\r\n }\r\n\r\n function getAllFrames(root = document) {\r\n const result = [root];\r\n const frames = getAllDocumentElementsIncludingShadow('frame, iframe', root); \r\n frames.forEach(frame => {\r\n try {\r\n const frameDocument = frame.contentDocument || frame.contentWindow.document;\r\n if (frameDocument) {\r\n result.push(frameDocument);\r\n }\r\n } catch (e) {\r\n // Skip cross-origin frames\r\n console.warn('Could not access frame content:', e.message);\r\n }\r\n });\r\n\r\n return result;\r\n }\r\n\r\n function getAllElementsIncludingShadow(selectors, root = document) {\r\n const elements = [];\r\n\r\n getAllFrames(root).forEach(doc => {\r\n elements.push(...getAllDocumentElementsIncludingShadow(selectors, doc));\r\n });\r\n\r\n return elements;\r\n }\r\n\r\n /**\r\n * Deeply searches through DOM trees including Shadow DOM and frames/iframes\r\n * @param {string} selector - CSS selector to search for\r\n * @param {Document|Element} [root=document] - Starting point for the search\r\n * @param {Object} [options] - Search options\r\n * @param {boolean} [options.searchShadow=true] - Whether to search Shadow DOM\r\n * @param {boolean} [options.searchFrames=true] - Whether to search frames/iframes\r\n * @returns {Element[]} Array of found elements\r\n \r\n function getAllElementsIncludingShadow(selector, root = document, options = {}) {\r\n const {\r\n searchShadow = true,\r\n searchFrames = true\r\n } = options;\r\n\r\n const results = new Set();\r\n \r\n // Helper to check if an element is valid and not yet found\r\n const addIfValid = (element) => {\r\n if (element && !results.has(element)) {\r\n results.add(element);\r\n }\r\n };\r\n\r\n // Helper to process a single document or element\r\n function processNode(node) {\r\n // Search regular DOM\r\n node.querySelectorAll(selector).forEach(addIfValid);\r\n\r\n if (searchShadow) {\r\n // Search all shadow roots\r\n const treeWalker = document.createTreeWalker(\r\n node,\r\n NodeFilter.SHOW_ELEMENT,\r\n {\r\n acceptNode: (element) => {\r\n return element.shadowRoot ? \r\n NodeFilter.FILTER_ACCEPT : \r\n NodeFilter.FILTER_SKIP;\r\n }\r\n }\r\n );\r\n\r\n while (treeWalker.nextNode()) {\r\n const element = treeWalker.currentNode;\r\n if (element.shadowRoot) {\r\n // Search within shadow root\r\n element.shadowRoot.querySelectorAll(selector).forEach(addIfValid);\r\n // Recursively process the shadow root for nested shadow DOMs\r\n processNode(element.shadowRoot);\r\n }\r\n }\r\n }\r\n\r\n if (searchFrames) {\r\n // Search frames and iframes\r\n const frames = node.querySelectorAll('frame, iframe');\r\n frames.forEach(frame => {\r\n try {\r\n const frameDocument = frame.contentDocument;\r\n if (frameDocument) {\r\n processNode(frameDocument);\r\n }\r\n } catch (e) {\r\n // Skip cross-origin frames\r\n console.warn('Could not access frame content:', e.message);\r\n }\r\n });\r\n }\r\n }\r\n\r\n // Start processing from the root\r\n processNode(root);\r\n\r\n return Array.from(results);\r\n }\r\n */\r\n // <div x=1 y=2 role='combobox'> </div>\r\n function findDropdowns() {\r\n const dropdowns = [];\r\n \r\n // Native select elements\r\n dropdowns.push(...getAllElementsIncludingShadow('select'));\r\n \r\n // Elements with dropdown roles that don't have <input>..</input>\r\n const roleElements = getAllElementsIncludingShadow('[role=\"combobox\"], [role=\"listbox\"], [role=\"dropdown\"], [role=\"option\"], [role=\"menu\"], [role=\"menuitem\"]').filter(el => {\r\n return el.tagName.toLowerCase() !== 'input' || ![\"button\", \"checkbox\", \"radio\"].includes(el.getAttribute(\"type\"));\r\n });\r\n dropdowns.push(...roleElements);\r\n \r\n // Common dropdown class patterns\r\n const dropdownPattern = /.*(dropdown|select|combobox|menu).*/i;\r\n const elements = getAllElementsIncludingShadow('*');\r\n const dropdownClasses = Array.from(elements).filter(el => {\r\n const hasDropdownClass = dropdownPattern.test(el.className);\r\n const validTag = ['li', 'ul', 'span', 'div', 'p', 'a', 'button'].includes(el.tagName.toLowerCase());\r\n const style = window.getComputedStyle(el); \r\n const result = hasDropdownClass && validTag && (style.cursor === 'pointer' || el.tagName.toLowerCase() === 'a' || el.tagName.toLowerCase() === 'button');\r\n return result;\r\n });\r\n \r\n dropdowns.push(...dropdownClasses);\r\n \r\n // Elements with aria-haspopup attribute\r\n dropdowns.push(...getAllElementsIncludingShadow('[aria-haspopup=\"true\"], [aria-haspopup=\"listbox\"], [aria-haspopup=\"menu\"]'));\r\n\r\n // Improve navigation element detection\r\n // Semantic nav elements with list items\r\n dropdowns.push(...getAllElementsIncludingShadow('nav ul li, nav ol li'));\r\n \r\n // Navigation elements in common design patterns\r\n dropdowns.push(...getAllElementsIncludingShadow('header a, .header a, .nav a, .navigation a, .menu a, .sidebar a, aside a'));\r\n \r\n // Elements in primary navigation areas with common attributes\r\n dropdowns.push(...getAllElementsIncludingShadow('[role=\"navigation\"] a, [aria-label*=\"navigation\"] a, [aria-label*=\"menu\"] a'));\r\n\r\n return dropdowns;\r\n }\r\n\r\n function findClickables() {\r\n const clickables = [];\r\n \r\n const checkboxPattern = /checkbox/i;\r\n // Collect all clickable elements first\r\n const nativeLinks = [...getAllElementsIncludingShadow('a')];\r\n const nativeButtons = [...getAllElementsIncludingShadow('button')];\r\n const inputButtons = [...getAllElementsIncludingShadow('input[type=\"button\"], input[type=\"submit\"], input[type=\"reset\"]')];\r\n const roleButtons = [...getAllElementsIncludingShadow('[role=\"button\"]')];\r\n // const tabbable = [...getAllElementsIncludingShadow('[tabindex=\"0\"]')];\r\n const clickHandlers = [...getAllElementsIncludingShadow('[onclick]')];\r\n const dropdowns = findDropdowns();\r\n const nativeCheckboxes = [...getAllElementsIncludingShadow('input[type=\"checkbox\"]')]; \r\n const fauxCheckboxes = getAllElementsIncludingShadow('*').filter(el => {\r\n if (checkboxPattern.test(el.className)) {\r\n const realCheckboxes = getAllElementsIncludingShadow('input[type=\"checkbox\"]', el);\r\n if (realCheckboxes.length === 1) {\r\n const boundingRect = realCheckboxes[0].getBoundingClientRect();\r\n return boundingRect.width <= 1 && boundingRect.height <= 1 \r\n }\r\n }\r\n return false;\r\n });\r\n const nativeRadios = [...getAllElementsIncludingShadow('input[type=\"radio\"]')];\r\n const toggles = findToggles();\r\n const pointerElements = findElementsWithPointer();\r\n // Add all elements at once\r\n clickables.push(\r\n ...nativeLinks,\r\n ...nativeButtons,\r\n ...inputButtons,\r\n ...roleButtons,\r\n // ...tabbable,\r\n ...clickHandlers,\r\n ...dropdowns,\r\n ...nativeCheckboxes,\r\n ...fauxCheckboxes,\r\n ...nativeRadios,\r\n ...toggles,\r\n ...pointerElements\r\n );\r\n\r\n // Only uniquify once at the end\r\n return clickables; // Let findElements handle the uniquification\r\n }\r\n\r\n function findToggles() {\r\n const toggles = [];\r\n const checkboxes = getAllElementsIncludingShadow('input[type=\"checkbox\"]');\r\n const togglePattern = /switch|toggle|slider/i;\r\n\r\n checkboxes.forEach(checkbox => {\r\n let isToggle = false;\r\n\r\n // Check the checkbox itself\r\n if (togglePattern.test(checkbox.className) || togglePattern.test(checkbox.getAttribute('role') || '')) {\r\n isToggle = true;\r\n }\r\n\r\n // Check parent elements (up to 3 levels)\r\n if (!isToggle) {\r\n let element = checkbox;\r\n for (let i = 0; i < 3; i++) {\r\n const parent = element.parentElement;\r\n if (!parent) break;\r\n\r\n const className = parent.className || '';\r\n const role = parent.getAttribute('role') || '';\r\n\r\n if (togglePattern.test(className) || togglePattern.test(role)) {\r\n isToggle = true;\r\n break;\r\n }\r\n element = parent;\r\n }\r\n }\r\n\r\n // Check next sibling\r\n if (!isToggle) {\r\n const nextSibling = checkbox.nextElementSibling;\r\n if (nextSibling) {\r\n const className = nextSibling.className || '';\r\n const role = nextSibling.getAttribute('role') || '';\r\n if (togglePattern.test(className) || togglePattern.test(role)) {\r\n isToggle = true;\r\n }\r\n }\r\n }\r\n\r\n if (isToggle) {\r\n toggles.push(checkbox);\r\n }\r\n });\r\n\r\n return toggles;\r\n }\r\n\r\n function findNonInteractiveElements() {\r\n // Get all elements in the document\r\n const all = Array.from(getAllElementsIncludingShadow('*'));\r\n \r\n // Filter elements based on Python implementation rules\r\n return all.filter(element => {\r\n if (!element.firstElementChild) {\r\n const tag = element.tagName.toLowerCase(); \r\n if (!['select', 'button', 'a'].includes(tag)) {\r\n const validTags = ['p', 'span', 'div', 'input', 'textarea'].includes(tag) || /^h\\d$/.test(tag) || /text/.test(tag);\r\n const boundingRect = element.getBoundingClientRect();\r\n return validTags && boundingRect.height > 1 && boundingRect.width > 1;\r\n }\r\n }\r\n return false;\r\n });\r\n }\r\n\r\n\r\n\r\n // export function findNonInteractiveElements() {\r\n // const all = [];\r\n // try {\r\n // const elements = getAllElementsIncludingShadow('*');\r\n // all.push(...elements);\r\n // } catch (e) {\r\n // console.warn('Error getting elements:', e);\r\n // }\r\n \r\n // console.debug('Total elements found:', all.length);\r\n \r\n // return all.filter(element => {\r\n // try {\r\n // const tag = element.tagName.toLowerCase(); \r\n\r\n // // Special handling for input elements\r\n // if (tag === 'input' || tag === 'textarea') {\r\n // const boundingRect = element.getBoundingClientRect();\r\n // const value = element.value || '';\r\n // const placeholder = element.placeholder || '';\r\n // return boundingRect.height > 1 && \r\n // boundingRect.width > 1 && \r\n // (value.trim() !== '' || placeholder.trim() !== '');\r\n // }\r\n\r\n \r\n // // Check if it's a valid tag for text content\r\n // const validTags = ['p', 'span', 'div', 'label', 'th', 'td', 'li', 'button', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'select'].includes(tag) || \r\n // /^h\\d$/.test(tag) || \r\n // /text/.test(tag);\r\n\r\n // const boundingRect = element.getBoundingClientRect();\r\n\r\n // // Get direct text content, excluding child element text\r\n // let directText = '';\r\n // for (const node of element.childNodes) {\r\n // // Only include text nodes (nodeType 3)\r\n // if (node.nodeType === 3) {\r\n // directText += node.textContent || '';\r\n // }\r\n // }\r\n \r\n // // If no direct text and it's a table cell or heading, check label content\r\n // if (!directText.trim() && (tag === 'th' || tag === 'td' || tag === 'h1')) {\r\n // const labels = element.getElementsByTagName('label');\r\n // for (const label of labels) {\r\n // directText += label.textContent || '';\r\n // }\r\n // }\r\n\r\n // // If still no text and it's a heading, get all text content\r\n // if (!directText.trim() && tag === 'h1') {\r\n // directText = element.textContent || '';\r\n // }\r\n\r\n // directText = directText.trim();\r\n\r\n // // Debug logging\r\n // if (directText) {\r\n // console.debugg('Text element found:', {\r\n // tag,\r\n // text: directText,\r\n // dimensions: boundingRect,\r\n // element\r\n // });\r\n // }\r\n\r\n // return validTags && \r\n // boundingRect.height > 1 && \r\n // boundingRect.width > 1 && \r\n // directText !== '';\r\n \r\n // } catch (e) {\r\n // console.warn('Error processing element:', e);\r\n // return false;\r\n // }\r\n // });\r\n // }\r\n\r\n\r\n\r\n\r\n\r\n function findElementsWithPointer() {\r\n const elements = [];\r\n const allElements = getAllElementsIncludingShadow('*');\r\n \r\n console.log('Checking elements with pointer style...');\r\n \r\n allElements.forEach(element => {\r\n // Skip SVG elements for now\r\n if (element instanceof SVGElement || element.tagName.toLowerCase() === 'svg') {\r\n return;\r\n }\r\n \r\n const style = window.getComputedStyle(element);\r\n if (style.cursor === 'pointer') {\r\n elements.push(element);\r\n }\r\n });\r\n \r\n console.log(`Found ${elements.length} elements with pointer cursor`);\r\n return elements;\r\n }\r\n\r\n function findCheckables() {\r\n const elements = [];\r\n\r\n elements.push(...getAllElementsIncludingShadow('input[type=\"checkbox\"]'));\r\n elements.push(...getAllElementsIncludingShadow('input[type=\"radio\"]'));\r\n const all_elements = getAllElementsIncludingShadow('label');\r\n const radioClasses = Array.from(all_elements).filter(el => {\r\n return /.*radio.*/i.test(el.className); \r\n });\r\n elements.push(...radioClasses);\r\n return elements;\r\n }\r\n\r\n function findFillables() {\r\n const elements = [];\r\n\r\n const inputs = [...getAllElementsIncludingShadow('input:not([type=\"radio\"]):not([type=\"checkbox\"])')];\r\n console.log('Found inputs:', inputs.length, inputs);\r\n elements.push(...inputs);\r\n \r\n const textareas = [...getAllElementsIncludingShadow('textarea')];\r\n console.log('Found textareas:', textareas.length);\r\n elements.push(...textareas);\r\n \r\n const editables = [...getAllElementsIncludingShadow('[contenteditable=\"true\"]')];\r\n console.log('Found editables:', editables.length);\r\n elements.push(...editables);\r\n\r\n return elements;\r\n }\n\n // Helper function to check if element is a form control\r\n function isFormControl(elementInfo) {\r\n return /^(input|select|textarea|button|label)$/i.test(elementInfo.tag);\r\n }\r\n\r\n const isDropdownItem = (elementInfo) => {\r\n const dropdownPatterns = [\r\n /dropdown[-_]?item/i, // matches: dropdown-item, dropdownitem, dropdown_item\r\n /menu[-_]?item/i, // matches: menu-item, menuitem, menu_item\r\n /dropdown[-_]?link/i, // matches: dropdown-link, dropdownlink, dropdown_link\r\n /list[-_]?item/i, // matches: list-item, listitem, list_item\r\n /select[-_]?item/i, // matches: select-item, selectitem, select_item \r\n ];\r\n\r\n const rolePatterns = [\r\n /menu[-_]?item/i, // matches: menuitem, menu-item\r\n /option/i, // matches: option\r\n /list[-_]?item/i, // matches: listitem, list-item\r\n /tree[-_]?item/i // matches: treeitem, tree-item\r\n ];\r\n\r\n const hasMatchingClass = elementInfo.element.className && \r\n dropdownPatterns.some(pattern => \r\n pattern.test(elementInfo.element.className)\r\n );\r\n\r\n const hasMatchingRole = elementInfo.element.getAttribute('role') && \r\n rolePatterns.some(pattern => \r\n pattern.test(elementInfo.element.getAttribute('role'))\r\n );\r\n\r\n return hasMatchingClass || hasMatchingRole;\r\n };\r\n\r\n /**\r\n * Finds the first element matching a CSS selector, traversing Shadow DOM if necessary\r\n * @param {string} selector - CSS selector to search for\r\n * @param {Element} [root=document] - Root element to start searching from\r\n * @returns {Element|null} - The first matching element or null if not found\r\n */\r\n function querySelectorShadow(selector, root = document) {\r\n // First try to find in light DOM\r\n let element = root.querySelector(selector);\r\n if (element) return element;\r\n \r\n // Get all elements with shadow root\r\n const shadowElements = Array.from(root.querySelectorAll('*'))\r\n .filter(el => el.shadowRoot);\r\n \r\n // Search through each shadow root until we find a match\r\n for (const el of shadowElements) {\r\n element = querySelectorShadow(selector, el.shadowRoot);\r\n if (element) return element;\r\n }\r\n \r\n return null;\r\n }\r\n\r\n const getElementByXPathOrCssSelector = (element_info) => {\r\n console.log('getElementByXPathOrCssSelector:', element_info);\r\n\r\n findElement(document, element_info.iframe_selector, element_info.css_selector);\r\n };\r\n\r\n const findElement = (root, iframeSelector, cssSelector) => {\r\n let element;\r\n \r\n if (iframeSelector) { \r\n const frames = getAllDocumentElementsIncludingShadow('iframe', root);\r\n \r\n // Iterate over all frames and compare their CSS selectors\r\n for (const frame of frames) {\r\n const selector = generateCssPath(frame);\r\n if (selector === iframeSelector) {\r\n const frameDocument = frame.contentDocument || frame.contentWindow.document;\r\n element = querySelectorShadow(cssSelector, frameDocument);\r\n console.log('found element ', element);\r\n break;\r\n } \r\n } }\r\n else\r\n element = querySelectorShadow(cssSelector, root);\r\n \r\n if (!element) {\r\n console.warn('Failed to find element with CSS selector:', cssSelector);\r\n }\r\n\r\n return element;\r\n };\r\n\r\n /* export function generateXPath(element) {\r\n if (!element || element.getRootNode() instanceof ShadowRoot) return '';\r\n \r\n // If element has an id, use that (it's unique and shorter)\r\n if (element.id) {\r\n return `//*[@id=\"${element.id}\"]`;\r\n }\r\n \r\n const parts = [];\r\n let current = element;\r\n \r\n while (current && current.nodeType === Node.ELEMENT_NODE) {\r\n let index = 1;\r\n let sibling = current.previousSibling;\r\n \r\n while (sibling) {\r\n if (sibling.nodeType === Node.ELEMENT_NODE && sibling.tagName === current.tagName) {\r\n index++;\r\n }\r\n sibling = sibling.previousSibling;\r\n }\r\n \r\n const tagName = current.tagName.toLowerCase();\r\n parts.unshift(`${tagName}[${index}]`);\r\n current = current.parentNode;\r\n }\r\n \r\n return '/' + parts.join('/');\r\n } */\r\n\r\n function isDecendent(parent, child) {\r\n let element = child;\r\n while (element.nodeType === Node.ELEMENT_NODE) { \r\n \r\n if (element.assignedSlot) {\r\n element = element.assignedSlot;\r\n }\r\n else {\r\n element = element.parentNode;\r\n // Check if we're at a shadow root\r\n if (element && element.nodeType !== Node.ELEMENT_NODE && element.getRootNode() instanceof ShadowRoot) {\r\n // Get the shadow root's host element\r\n element = element.getRootNode().host; \r\n }\r\n }\r\n if (element === parent)\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n function generateXPath(element) {\r\n return extractElementPath(element).map(item => `${item.tagName}${item.onlyChild ? '' : `[${item.index}]`}`).join('/');\r\n }\r\n\r\n function generateCssPath(element) {\r\n return extractElementPath(element).map(item => `${item.tagName}:nth-of-type(${item.index})`).join(' > ');\r\n }\r\n\r\n function extractElementPath(element) {\r\n if (!element) {\r\n console.error('ERROR: No element provided to generatePath');\r\n return [];\r\n }\r\n const path = [];\r\n // traversing up the DOM tree\r\n while (element && element.nodeType === Node.ELEMENT_NODE) { \r\n let tagName = element.nodeName.toLowerCase();\r\n \r\n let sibling = element;\r\n let index = 1;\r\n while (sibling = sibling.previousElementSibling) {\r\n if (sibling.nodeName.toLowerCase() === tagName) index++;\r\n }\r\n sibling = element;\r\n let onlyChild = true;\r\n while (sibling = sibling.nextElementSibling) {\r\n if (sibling.nodeName.toLowerCase() === tagName) {\r\n onlyChild = false;\r\n break;\r\n }\r\n }\r\n \r\n // add a tuple with tagName, index (nth), and onlyChild \r\n path.unshift({\r\n tagName: tagName,\r\n index: index,\r\n onlyChild: onlyChild \r\n }); \r\n \r\n // move up the DOM tree\r\n element = element.parentNode;\r\n\r\n // Check if we're at a shadow root\r\n if (element && element.nodeType !== Node.ELEMENT_NODE && element.getRootNode() instanceof ShadowRoot) {\r\n // console.log('generatePath: found shadow root, moving to host');\r\n // Get the shadow root's host element\r\n element = element.getRootNode().host; \r\n }\r\n }\r\n \r\n return path;\r\n }\r\n\r\n\r\n function cleanHTML(rawHTML) {\r\n const parser = new DOMParser();\r\n const doc = parser.parseFromString(rawHTML, \"text/html\");\r\n\r\n function cleanElement(element) {\r\n const allowedAttributes = new Set([\r\n \"role\",\r\n \"type\",\r\n \"class\",\r\n \"href\",\r\n \"alt\",\r\n \"title\",\r\n \"readonly\",\r\n \"checked\",\r\n \"enabled\",\r\n \"disabled\",\r\n ]);\r\n\r\n [...element.attributes].forEach(attr => {\r\n const name = attr.name.toLowerCase();\r\n const value = attr.value;\r\n\r\n const isTestAttribute = /^(testid|test-id|data-test-id)$/.test(name);\r\n const isDataAttribute = name.startsWith(\"data-\") && value;\r\n const isBooleanAttribute = [\"readonly\", \"checked\", \"enabled\", \"disabled\"].includes(name);\r\n\r\n if (!allowedAttributes.has(name) && !isDataAttribute && !isTestAttribute && !isBooleanAttribute) {\r\n element.removeAttribute(name);\r\n }\r\n });\r\n\r\n // Handle SVG content - more aggressive replacement\r\n if (element.tagName.toLowerCase() === \"svg\") {\r\n // Remove all attributes except class and role\r\n [...element.attributes].forEach(attr => {\r\n const name = attr.name.toLowerCase();\r\n if (name !== \"class\" && name !== \"role\") {\r\n element.removeAttribute(name);\r\n }\r\n });\r\n element.innerHTML = \"CONTENT REMOVED\";\r\n } else {\r\n // Recursively clean child elements\r\n Array.from(element.children).forEach(cleanElement);\r\n }\r\n\r\n // Only remove empty elements that aren't semantic or icon elements\r\n const keepEmptyElements = ['i', 'span', 'svg', 'button', 'input'];\r\n if (!keepEmptyElements.includes(element.tagName.toLowerCase()) && \r\n !element.children.length && \r\n !element.textContent.trim()) {\r\n element.remove();\r\n }\r\n }\r\n\r\n // Process all elements in the document body\r\n Array.from(doc.body.children).forEach(cleanElement);\r\n return doc.body.innerHTML;\r\n }\r\n\r\n function getContainingIframe(element) {\r\n // If not in an iframe, return null\r\n if (element.ownerDocument.defaultView === window.top) {\r\n return null;\r\n }\r\n \r\n // Try to find the iframe in the parent document that contains our element\r\n try {\r\n const parentDocument = element.ownerDocument.defaultView.parent.document;\r\n const iframes = parentDocument.querySelectorAll('iframe');\r\n \r\n for (const iframe of iframes) {\r\n if (iframe.contentWindow === element.ownerDocument.defaultView) {\r\n return iframe;\r\n }\r\n }\r\n } catch (e) {\r\n // Cross-origin restriction\r\n return \"Cross-origin iframe - cannot access details\";\r\n }\r\n \r\n return null;\r\n }\r\n\r\n function getElementInfo(element, index) {\r\n\r\n const xpath = generateXPath(element);\r\n const css_selector = generateCssPath(element);\r\n //disabled since it's blocking event handling in recorder\r\n const short_css_selector = ''; //getRobustSelector(element);\r\n\r\n const iframe = getContainingIframe(element); \r\n const iframe_selector = iframe ? generateCssPath(iframe) : \"\";\r\n //disabled since it's blocking event handling in recorder\r\n const short_iframe_selector = ''; //iframe ? getRobustSelector(iframe) : \"\";\r\n\r\n // Return element info with pre-calculated values\r\n return new ElementInfo(element, index, {\r\n tag: element.tagName.toLowerCase(),\r\n type: element.type || '',\r\n text: element.innerText || element.placeholder || '', //getTextContent(element),\r\n html: cleanHTML(element.outerHTML),\r\n xpath: xpath,\r\n css_selector: css_selector,\r\n bounding_box: element.getBoundingClientRect(),\r\n iframe_selector: iframe_selector,\r\n short_css_selector: short_css_selector,\r\n short_iframe_selector: short_iframe_selector\r\n });\r\n }\r\n\r\n function getAriaLabelledByText(elementInfo, includeHidden=true) {\r\n if (!elementInfo.element.hasAttribute('aria-labelledby')) return '';\r\n\r\n const ids = elementInfo.element.getAttribute('aria-labelledby').split(/\\s+/);\r\n let labelText = '';\r\n\r\n //locate root (document or iFrame document if element is contained in an iframe)\r\n let root = document;\r\n if (elementInfo.iframe_selector) { \r\n const frames = getAllDocumentElementsIncludingShadow('iframe', document);\r\n \r\n // Iterate over all frames and compare their CSS selectors\r\n for (const frame of frames) {\r\n const selector = generateCssPath(frame);\r\n if (selector === elementInfo.iframe_selector) {\r\n root = frame.contentDocument || frame.contentWindow.document; \r\n break;\r\n }\r\n } \r\n }\r\n\r\n ids.forEach(id => {\r\n const el = querySelectorShadow(`#${CSS.escape(id)}`, root);\r\n if (el) {\r\n if (includeHidden || el.offsetParent !== null || getComputedStyle(el).display !== 'none') {\r\n labelText += el.textContent.trim() + ' ';\r\n }\r\n }\r\n });\r\n\r\n return labelText.trim();\r\n }\r\n\r\n\r\n\r\n const filterZeroDimensions = (elementInfo) => {\r\n const rect = elementInfo.bounding_box;\r\n //single pixel elements are typically faux controls and should be filtered too\r\n const hasSize = rect.width > 1 && rect.height > 1;\r\n const style = window.getComputedStyle(elementInfo.element);\r\n const isVisible = style.display !== 'none' && style.visibility !== 'hidden';\r\n \r\n if (!hasSize || !isVisible) {\r\n \r\n return false;\r\n }\r\n return true;\r\n };\r\n\r\n\r\n\r\n function uniquifyElements(elements) {\r\n const seen = new Set();\r\n\r\n console.log(`Starting uniquification with ${elements.length} elements`);\r\n\r\n // Filter out testing infrastructure elements first\r\n const filteredInfrastructure = elements.filter(element_info => {\r\n // Skip the highlight-overlay element completely - it's part of the testing infrastructure\r\n if (element_info.element.id === 'highlight-overlay' || \r\n (element_info.css_selector && element_info.css_selector.includes('#highlight-overlay'))) {\r\n console.log('Filtered out testing infrastructure element:', element_info.css_selector);\r\n return false;\r\n }\r\n \r\n // Filter out UI framework container/manager elements\r\n const el = element_info.element;\r\n // UI framework container checks - generic detection for any framework\r\n if ((el.getAttribute('data-rendered-by') || \r\n el.getAttribute('data-reactroot') || \r\n el.getAttribute('ng-version') || \r\n el.getAttribute('data-component-id') ||\r\n el.getAttribute('data-root') ||\r\n el.getAttribute('data-framework')) && \r\n (el.className && \r\n typeof el.className === 'string' && \r\n (el.className.includes('Container') || \r\n el.className.includes('container') || \r\n el.className.includes('Manager') || \r\n el.className.includes('manager')))) {\r\n console.log('Filtered out UI framework container element:', element_info.css_selector);\r\n return false;\r\n }\r\n \r\n // Direct filter for framework container elements that shouldn't be interactive\r\n // Consolidating multiple container detection patterns into one efficient check\r\n const isFullViewport = element_info.bounding_box && \r\n element_info.bounding_box.x <= 5 && \r\n element_info.bounding_box.y <= 5 && \r\n element_info.bounding_box.width >= (window.innerWidth * 0.95) && \r\n element_info.bounding_box.height >= (window.innerHeight * 0.95);\r\n \r\n // Empty content check\r\n const isEmpty = !el.innerText || el.innerText.trim() === '';\r\n \r\n // Check if it's a framework container element\r\n if (element_info.element.tagName === 'DIV' && \r\n isFullViewport && \r\n isEmpty && \r\n (\r\n // Pattern matching for root containers\r\n (element_info.xpath && \r\n (element_info.xpath.match(/^\\/html\\[\\d+\\]\\/body\\[\\d+\\]\\/div\\[\\d+\\]\\/div\\[\\d+\\]$/) || \r\n element_info.xpath.match(/^\\/\\/\\*\\[@id='[^']+'\\]\\/div\\[\\d+\\]$/))) ||\r\n \r\n // Simple DOM structure\r\n (element_info.css_selector.split(' > ').length <= 4 && element_info.depth <= 5) ||\r\n \r\n // Empty or container-like classes\r\n (!el.className || el.className === '' || \r\n (typeof el.className === 'string' && \r\n (el.className.includes('overlay') || \r\n el.className.includes('container') || \r\n el.className.includes('wrapper'))))\r\n )) {\r\n console.log('Filtered out framework container element:', element_info.css_selector);\r\n return false;\r\n }\r\n \r\n return true;\r\n });\r\n\r\n // First filter out elements with zero dimensions\r\n const nonZeroElements = filteredInfrastructure.filter(filterZeroDimensions);\r\n // sort by CSS selector depth so parents are processed first\r\n nonZeroElements.sort((a, b) => a.getDepth() - b.getDepth());\r\n console.log(`After dimension filtering: ${nonZeroElements.length} elements remain (${elements.length - nonZeroElements.length} removed)`);\r\n \r\n const filteredByParent = nonZeroElements.filter(element_info => {\r\n\r\n const parent = findClosestParent(seen, element_info);\r\n const keep = parent == null || shouldKeepNestedElement(element_info, parent);\r\n // console.log(\"node \", element_info.index, \": keep=\", keep, \" parent=\", parent);\r\n // if (!keep && !element_info.xpath) {\r\n // console.log(\"Filtered out element \", element_info,\" because it's a nested element of \", parent);\r\n // }\r\n if (keep)\r\n seen.add(element_info.css_selector);\r\n\r\n return keep;\r\n });\r\n\r\n console.log(`After parent/child filtering: ${filteredByParent.length} elements remain (${nonZeroElements.length - filteredByParent.length} removed)`);\r\n\r\n // Final overlap filtering\r\n const filteredResults = filteredByParent.filter(element => {\r\n\r\n // Look for any element that came BEFORE this one in the array\r\n const hasEarlierOverlap = filteredByParent.some(other => {\r\n // Only check elements that came before (lower index)\r\n if (filteredByParent.indexOf(other) >= filteredByParent.indexOf(element)) {\r\n return false;\r\n }\r\n \r\n const isOverlapping = areElementsOverlapping(element, other); \r\n return isOverlapping;\r\n }); \r\n\r\n // Keep element if it has no earlier overlapping elements\r\n return !hasEarlierOverlap;\r\n });\r\n \r\n \r\n \r\n // Check for overlay removal\r\n console.log(`After filtering: ${filteredResults.length} (${filteredByParent.length - filteredResults.length} removed by overlap)`);\r\n \r\n const nonOverlaidElements = filteredResults.filter(element => {\r\n return !isOverlaid(element);\r\n });\r\n\r\n console.log(`Final elements after overlay removal: ${nonOverlaidElements.length} (${filteredResults.length - nonOverlaidElements.length} removed)`);\r\n \r\n return nonOverlaidElements;\r\n\r\n }\r\n\r\n\r\n\r\n const areElementsOverlapping = (element1, element2) => {\r\n if (element1.css_selector === element2.css_selector) {\r\n return true;\r\n }\r\n \r\n const box1 = element1.bounding_box;\r\n const box2 = element2.bounding_box;\r\n \r\n return box1.x === box2.x &&\r\n box1.y === box2.y &&\r\n box1.width === box2.width &&\r\n box1.height === box2.height;\r\n // element1.text === element2.text &&\r\n // element2.tag === 'a';\r\n };\r\n\r\n function findClosestParent(seen, element_info) { \r\n \r\n // Split the xpath into segments\r\n const segments = element_info.css_selector.split(' > ');\r\n \r\n // Try increasingly shorter paths until we find one in the seen set\r\n for (let i = segments.length - 1; i > 0; i--) {\r\n const parentPath = segments.slice(0, i).join(' > ');\r\n if (seen.has(parentPath)) {\r\n return parentPath;\r\n }\r\n }\r\n\r\n return null;\r\n }\r\n\r\n function shouldKeepNestedElement(elementInfo, parentPath) {\r\n let result = false;\r\n const parentSegments = parentPath.split(' > ');\r\n\r\n const isParentLink = /^a(:nth-of-type\\(\\d+\\))?$/.test(parentSegments[parentSegments.length - 1]);\r\n if (isParentLink) {\r\n return false; \r\n }\r\n // If this is a checkbox/radio input\r\n if (elementInfo.tag === 'input' && \r\n (elementInfo.type === 'checkbox' || elementInfo.type === 'radio')) {\r\n \r\n // Check if parent is a label by looking at the parent xpath's last segment\r\n \r\n const isParentLabel = /^label(:nth-of-type\\(\\d+\\))?$/.test(parentSegments[parentSegments.length - 1]);\r\n \r\n // If parent is a label, don't keep the input (we'll keep the label instead)\r\n if (isParentLabel) {\r\n return false;\r\n }\r\n }\r\n \r\n // Keep all other form controls and dropdown items\r\n if (isFormControl(elementInfo) || isDropdownItem(elementInfo)) {\r\n result = true;\r\n }\r\n\r\n if(isTableCell(elementInfo)) {\r\n result = true;\r\n }\r\n \r\n \r\n // console.log(`shouldKeepNestedElement: ${elementInfo.tag} ${elementInfo.text} ${elementInfo.xpath} -> ${parentXPath} -> ${result}`);\r\n return result;\r\n }\r\n\r\n\r\n function isTableCell(elementInfo) {\r\n const element = elementInfo.element;\r\n if(!element || !(element instanceof HTMLElement)) {\r\n return false;\r\n }\r\n const validTags = new Set(['td', 'th']);\r\n const validRoles = new Set(['cell', 'gridcell', 'columnheader', 'rowheader']);\r\n \r\n const tag = element.tagName.toLowerCase();\r\n const role = element.getAttribute('role')?.toLowerCase();\r\n\r\n if (validTags.has(tag) || (role && validRoles.has(role))) {\r\n return true;\r\n }\r\n return false;\r\n \r\n }\r\n\r\n function isOverlaid(elementInfo) {\r\n const element = elementInfo.element;\r\n const boundingRect = elementInfo.bounding_box;\r\n \r\n\r\n \r\n \r\n // Create a diagnostic logging function that only logs when needed\r\n const diagnosticLog = (...args) => {\r\n { // set to true for debugging\r\n console.log('[OVERLAY-DEBUG]', ...args);\r\n }\r\n };\r\n\r\n // Special handling for tooltips\r\n if (elementInfo.element.className && typeof elementInfo.element.className === 'string' && \r\n elementInfo.element.className.includes('tooltip')) {\r\n diagnosticLog('Element is a tooltip, not considering it overlaid');\r\n return false;\r\n }\r\n \r\n \r\n \r\n // Get element at the center point to check if it's covered by a popup/modal\r\n const middleX = boundingRect.x + boundingRect.width/2;\r\n const middleY = boundingRect.y + boundingRect.height/2;\r\n const elementAtMiddle = element.ownerDocument.elementFromPoint(middleX, middleY);\r\n \r\n if (elementAtMiddle && \r\n elementAtMiddle !== element && \r\n !isDecendent(element, elementAtMiddle) && \r\n !isDecendent(elementAtMiddle, element)) {\r\n\r\n \r\n return true;\r\n }\r\n \r\n \r\n return false;\r\n \r\n }\r\n\r\n\r\n\r\n /**\r\n * Get the “best” short, unique, and robust CSS selector for an element.\r\n * \r\n * @param {Element} element\r\n * @returns {string} A selector guaranteed to find exactly that element in its context\r\n */\r\n function getRobustSelector(element) {\r\n // 1. Figure out the real “root” (iframe doc, shadow root, or main doc)\r\n const root = (() => {\r\n const rootNode = element.getRootNode();\r\n if (rootNode instanceof ShadowRoot) {\r\n return rootNode;\r\n }\r\n return element.ownerDocument;\r\n })();\r\n\r\n // 2. Options to bias toward stable attrs and away from auto-generated classes\r\n const options = {\r\n root,\r\n // only use data-*, id or aria-label by default\r\n attr(name, value) {\r\n if (name === 'id' || name.startsWith('data-') || name === 'aria-label') {\r\n return true;\r\n }\r\n return false;\r\n },\r\n // skip framework junk\r\n filter(name, value) {\r\n if (name.startsWith('ng-') || name.startsWith('_ngcontent') || /^p-/.test(name)) {\r\n return false;\r\n }\r\n return true;\r\n },\r\n // let finder try really short seeds\r\n seedMinLength: 1,\r\n optimizedMinLength: 1,\r\n };\r\n\r\n let selector;\r\n try {\r\n selector = finder(element, options);\r\n // 3. Verify it really works in the context\r\n const found = root.querySelectorAll(selector);\r\n if (found.length !== 1 || found[0] !== element) {\r\n throw new Error('not unique or not found');\r\n }\r\n return selector;\r\n } catch (err) {\r\n // 4. Fallback: full path (you already have this utility)\r\n console.warn('[getRobustSelector] finder failed, falling back to full path:', err);\r\n return generateCssPath(element); // you’d import or define this elsewhere\r\n }\r\n }\r\n\r\n /**\r\n * Checks if an element is scrollable (has scrollable content)\r\n * \r\n * @param element - The element to check\r\n * @returns boolean indicating if the element is scrollable\r\n */\r\n function isScrollableContainer(element) {\r\n if (!element) return false;\r\n \r\n const style = window.getComputedStyle(element);\r\n \r\n // Reliable way to detect if an element has scrollbars or is scrollable\r\n const hasScrollHeight = element.scrollHeight > element.clientHeight;\r\n const hasScrollWidth = element.scrollWidth > element.clientWidth;\r\n \r\n // Check actual style properties\r\n const hasOverflowY = style.overflowY === 'auto' || \r\n style.overflowY === 'scroll' || \r\n style.overflowY === 'overlay';\r\n const hasOverflowX = style.overflowX === 'auto' || \r\n style.overflowX === 'scroll' || \r\n style.overflowX === 'overlay';\r\n \r\n // Check common class names and attributes for scrollable containers across frameworks\r\n const hasScrollClasses = element.classList.contains('scroll') || \r\n element.classList.contains('scrollable') ||\r\n element.classList.contains('overflow') ||\r\n element.classList.contains('overflow-auto') ||\r\n element.classList.contains('overflow-scroll') ||\r\n element.getAttribute('data-scrollable') === 'true';\r\n \r\n // Check for height/max-height constraints that often indicate scrolling content\r\n const hasHeightConstraint = style.maxHeight && \r\n style.maxHeight !== 'none' && \r\n style.maxHeight !== 'auto';\r\n \r\n // An element is scrollable if it has:\r\n // 1. Actual scrollbars in use (most reliable check) OR\r\n // 2. Overflow styles allowing scrolling AND content that would require scrolling\r\n return (hasScrollHeight && hasOverflowY) || \r\n (hasScrollWidth && hasOverflowX) ||\r\n (hasScrollClasses && (hasScrollHeight || hasScrollWidth)) ||\r\n (hasHeightConstraint && hasScrollHeight);\r\n }\r\n\r\n /**\r\n * Detects scrollable containers that are ancestors of the target element\r\n * \r\n * This function traverses up the DOM tree from the target element and identifies\r\n * all scrollable containers (elements that have scrollable content).\r\n * \r\n * @param target - The target element to start the search from\r\n * @returns Array of objects with selector and scroll properties\r\n */\r\n function detectScrollableContainers(target) {\r\n const scrollableContainers = [];\r\n \r\n if (!target) {\r\n return scrollableContainers;\r\n }\r\n \r\n console.log('🔍 [detectScrollableContainers] Starting detection for target:', target.tagName, target.id, target.className);\r\n \r\n // Detect if target is inside an iframe\r\n const iframe = getContainingIframe(target);\r\n const iframe_selector = iframe ? generateCssPath(iframe) : \"\";\r\n \r\n console.log('🔍 [detectScrollableContainers] Iframe context:', iframe ? 'inside iframe' : 'main document', 'selector:', iframe_selector);\r\n \r\n // Start from the target element and traverse up the DOM tree\r\n let currentElement = target;\r\n let depth = 0;\r\n const MAX_DEPTH = 10; // Limit traversal depth to avoid infinite loops\r\n \r\n while (currentElement && depth < MAX_DEPTH) {\r\n // Skip if we've reached the document body or html element\r\n if (currentElement === document.body || currentElement === document.documentElement) {\r\n break;\r\n }\r\n \r\n // Check if the current element is scrollable\r\n if (isScrollableContainer(currentElement)) {\r\n console.log('🔍 [detectScrollableContainers] Found scrollable container at depth', depth, ':', currentElement.tagName, currentElement.id, currentElement.className);\r\n \r\n const container = {\r\n containerEl: currentElement,\r\n selector: generateCssPath(currentElement),\r\n iframe_selector: iframe_selector,\r\n scrollTop: currentElement.scrollTop,\r\n scrollLeft: currentElement.scrollLeft,\r\n scrollHeight: currentElement.scrollHeight,\r\n scrollWidth: currentElement.scrollWidth,\r\n clientHeight: currentElement.clientHeight,\r\n clientWidth: currentElement.clientWidth\r\n };\r\n \r\n scrollableContainers.push(container);\r\n }\r\n \r\n // Move to parent element\r\n if (currentElement.assignedSlot) {\r\n // Handle Shadow DOM\r\n currentElement = currentElement.assignedSlot;\r\n } else {\r\n currentElement = currentElement.parentElement;\r\n \r\n // Handle Shadow Root boundaries\r\n if (currentElement) {\r\n const rootNode = currentElement.getRootNode();\r\n if (rootNode instanceof ShadowRoot) {\r\n currentElement = rootNode.host;\r\n }\r\n }\r\n }\r\n \r\n depth++;\r\n }\r\n \r\n console.log('🔍 [detectScrollableContainers] Detection complete. Found', scrollableContainers.length, 'scrollable containers');\r\n return scrollableContainers;\r\n }\n\n class DOMSerializer {\r\n constructor(options = {}) {\r\n this.options = {\r\n includeStyles: true,\r\n includeScripts: false, // Security consideration\r\n includeFrames: true,\r\n includeShadowDOM: true,\r\n maxDepth: 50,\r\n ...options\r\n };\r\n this.serializedFrames = new Map();\r\n this.shadowRoots = new Map();\r\n }\r\n \r\n /**\r\n * Serialize a complete document or element\r\n */\r\n serialize(rootElement = document) {\r\n try {\r\n const serialized = {\r\n type: 'document',\r\n doctype: this.serializeDoctype(rootElement),\r\n documentElement: this.serializeElement(rootElement.documentElement || rootElement),\r\n frames: [],\r\n timestamp: Date.now(),\r\n url: rootElement.URL || window.location?.href,\r\n metadata: {\r\n title: rootElement.title,\r\n charset: rootElement.characterSet,\r\n contentType: rootElement.contentType\r\n }\r\n };\r\n \r\n // Serialize frames and iframes if enabled\r\n if (this.options.includeFrames) {\r\n serialized.frames = this.serializeFrames(rootElement);\r\n }\r\n \r\n return serialized;\r\n } catch (error) {\r\n console.error('Serialization error:', error);\r\n throw new Error(`DOM serialization failed: ${error.message}`);\r\n }\r\n }\r\n \r\n /**\r\n * Serialize document type declaration\r\n */\r\n serializeDoctype(doc) {\r\n if (!doc.doctype) return null;\r\n \r\n return {\r\n name: doc.doctype.name,\r\n publicId: doc.doctype.publicId,\r\n systemId: doc.doctype.systemId\r\n };\r\n }\r\n \r\n /**\r\n * Serialize an individual element and its children\r\n */\r\n serializeElement(element, depth = 0) {\r\n if (depth > this.options.maxDepth) {\r\n return { type: 'text', content: '<!-- Max depth exceeded -->' };\r\n }\r\n \r\n const nodeType = element.nodeType;\r\n \r\n switch (nodeType) {\r\n case Node.ELEMENT_NODE:\r\n return this.serializeElementNode(element, depth);\r\n case Node.TEXT_NODE:\r\n return this.serializeTextNode(element);\r\n case Node.COMMENT_NODE:\r\n return this.serializeCommentNode(element);\r\n case Node.DOCUMENT_FRAGMENT_NODE:\r\n return this.serializeDocumentFragment(element, depth);\r\n default:\r\n return null;\r\n }\r\n }\r\n \r\n /**\r\n * Serialize element node with attributes and children\r\n */\r\n serializeElementNode(element, depth) {\r\n const tagName = element.tagName.toLowerCase();\r\n \r\n // Skip script tags for security unless explicitly enabled\r\n if (tagName === 'script' && !this.options.includeScripts) {\r\n return { type: 'comment', content: '<!-- Script tag removed for security -->' };\r\n }\r\n \r\n const serialized = {\r\n type: 'element',\r\n tagName: tagName,\r\n attributes: this.serializeAttributes(element),\r\n children: [],\r\n shadowRoot: null\r\n };\r\n \r\n // Handle Shadow DOM\r\n if (this.options.includeShadowDOM && element.shadowRoot) {\r\n serialized.shadowRoot = this.serializeShadowRoot(element.shadowRoot, depth + 1);\r\n }\r\n \r\n // Handle special elements\r\n if (tagName === 'iframe' || tagName === 'frame') {\r\n serialized.frameData = this.serializeFrameElement(element);\r\n }\r\n \r\n // Serialize children\r\n for (const child of element.childNodes) {\r\n const serializedChild = this.serializeElement(child, depth + 1);\r\n if (serializedChild) {\r\n serialized.children.push(serializedChild);\r\n }\r\n }\r\n \r\n // Include computed styles if enabled\r\n if (this.options.includeStyles && element.nodeType === Node.ELEMENT_NODE) {\r\n serialized.computedStyle = this.serializeComputedStyle(element);\r\n }\r\n \r\n return serialized;\r\n }\r\n \r\n /**\r\n * Serialize element attributes\r\n */\r\n serializeAttributes(element) {\r\n const attributes = {};\r\n \r\n if (element.attributes) {\r\n for (const attr of element.attributes) {\r\n attributes[attr.name] = attr.value;\r\n }\r\n }\r\n \r\n return attributes;\r\n }\r\n \r\n /**\r\n * Serialize computed styles\r\n */\r\n serializeComputedStyle(element) {\r\n try {\r\n const computedStyle = window.getComputedStyle(element);\r\n const styles = {};\r\n \r\n // Only serialize non-default values to reduce size\r\n const importantStyles = [\r\n 'display', 'position', 'width', 'height', 'margin', 'padding',\r\n 'border', 'background', 'color', 'font-family', 'font-size',\r\n 'text-align', 'visibility', 'z-index', 'transform'\r\n ];\r\n \r\n for (const prop of importantStyles) {\r\n const value = computedStyle.getPropertyValue(prop);\r\n if (value && value !== 'initial' && value !== 'normal') {\r\n styles[prop] = value;\r\n }\r\n }\r\n \r\n return styles;\r\n } catch (error) {\r\n return {};\r\n }\r\n }\r\n \r\n /**\r\n * Serialize text node\r\n */\r\n serializeTextNode(node) {\r\n return {\r\n type: 'text',\r\n content: node.textContent\r\n };\r\n }\r\n \r\n /**\r\n * Serialize comment node\r\n */\r\n serializeCommentNode(node) {\r\n return {\r\n type: 'comment',\r\n content: node.textContent\r\n };\r\n }\r\n \r\n /**\r\n * Serialize document fragment\r\n */\r\n serializeDocumentFragment(fragment, depth) {\r\n const serialized = {\r\n type: 'fragment',\r\n children: []\r\n };\r\n \r\n for (const child of fragment.childNodes) {\r\n const serializedChild = this.serializeElement(child, depth + 1);\r\n if (serializedChild) {\r\n serialized.children.push(serializedChild);\r\n }\r\n }\r\n \r\n return serialized;\r\n }\r\n \r\n /**\r\n * Serialize Shadow DOM\r\n */\r\n serializeShadowRoot(shadowRoot, depth) {\r\n const serialized = {\r\n type: 'shadowRoot',\r\n mode: shadowRoot.mode,\r\n children: []\r\n };\r\n \r\n for (const child of shadowRoot.childNodes) {\r\n const serializedChild = this.serializeElement(child, depth + 1);\r\n if (serializedChild) {\r\n serialized.children.push(serializedChild);\r\n }\r\n }\r\n \r\n return serialized;\r\n }\r\n \r\n /**\r\n * Serialize frame/iframe elements\r\n */\r\n serializeFrameElement(frameElement) {\r\n const frameData = {\r\n src: frameElement.src,\r\n name: frameElement.name,\r\n id: frameElement.id,\r\n sandbox: frameElement.sandbox?.toString() || '',\r\n allowfullscreen: frameElement.allowFullscreen\r\n };\r\n \r\n // Try to access frame content (may fail due to CORS)\r\n try {\r\n const frameDoc = frameElement.contentDocument;\r\n if (frameDoc && this.options.includeFrames) {\r\n frameData.content = this.serialize(frameDoc);\r\n }\r\n } catch (error) {\r\n frameData.accessError = 'Cross-origin frame content not accessible';\r\n }\r\n \r\n return frameData;\r\n }\r\n \r\n /**\r\n * Serialize all frames in document\r\n */\r\n serializeFrames(doc) {\r\n const frames = [];\r\n const frameElements = doc.querySelectorAll('iframe, frame');\r\n \r\n for (const frameElement of frameElements) {\r\n try {\r\n const frameDoc = frameElement.contentDocument;\r\n if (frameDoc) {\r\n frames.push({\r\n element: this.serializeElement(frameElement),\r\n content: this.serialize(frameDoc)\r\n });\r\n }\r\n } catch (error) {\r\n frames.push({\r\n element: this.serializeElement(frameElement),\r\n error: 'Frame content not accessible'\r\n });\r\n }\r\n }\r\n \r\n return frames;\r\n }\r\n \r\n /**\r\n * Deserialize serialized DOM data back to DOM nodes\r\n */\r\n deserialize(serializedData, targetDocument = document) {\r\n try {\r\n if (serializedData.type === 'document') {\r\n return this.deserializeDocument(serializedData, targetDocument);\r\n } else {\r\n return this.deserializeElement(serializedData, targetDocument);\r\n }\r\n } catch (error) {\r\n console.error('Deserialization error:', error);\r\n throw new Error(`DOM deserialization failed: ${error.message}`);\r\n }\r\n }\r\n \r\n /**\r\n * Deserialize complete document\r\n */\r\n deserializeDocument(serializedDoc, targetDoc) {\r\n // Create new document if needed\r\n const doc = targetDoc || document.implementation.createHTMLDocument();\r\n \r\n // Set doctype if present\r\n if (serializedDoc.doctype) {\r\n const doctype = document.implementation.createDocumentType(\r\n serializedDoc.doctype.name,\r\n serializedDoc.doctype.publicId,\r\n serializedDoc.doctype.systemId\r\n );\r\n doc.replaceChild(doctype, doc.doctype);\r\n }\r\n \r\n // Deserialize document element\r\n if (serializedDoc.documentElement) {\r\n const newDocElement = this.deserializeElement(serializedDoc.documentElement, doc);\r\n doc.replaceChild(newDocElement, doc.documentElement);\r\n }\r\n \r\n // Handle metadata\r\n if (serializedDoc.metadata) {\r\n doc.title = serializedDoc.metadata.title || '';\r\n }\r\n \r\n return doc;\r\n }\r\n \r\n /**\r\n * Deserialize individual element\r\n */\r\n deserializeElement(serializedNode, doc) {\r\n switch (serializedNode.type) {\r\n case 'element':\r\n return this.deserializeElementNode(serializedNode, doc);\r\n case 'text':\r\n return doc.createTextNode(serializedNode.content);\r\n case 'comment':\r\n return doc.createComment(serializedNode.content);\r\n case 'fragment':\r\n return this.deserializeDocumentFragment(serializedNode, doc);\r\n case 'shadowRoot':\r\n // Shadow roots are handled during element creation\r\n return null;\r\n default:\r\n return null;\r\n }\r\n }\r\n \r\n /**\r\n * Deserialize element node\r\n */\r\n deserializeElementNode(serializedElement, doc) {\r\n const element = doc.createElement(serializedElement.tagName);\r\n \r\n // Set attributes\r\n if (serializedElement.attributes) {\r\n for (const [name, value] of Object.entries(serializedElement.attributes)) {\r\n try {\r\n element.setAttribute(name, value);\r\n } catch (error) {\r\n console.warn(`Failed to set attribute ${name}:`, error);\r\n }\r\n }\r\n }\r\n \r\n // Apply computed styles if available\r\n if (serializedElement.computedStyle && this.options.includeStyles) {\r\n for (const [prop, value] of Object.entries(serializedElement.computedStyle)) {\r\n try {\r\n element.style.setProperty(prop, value);\r\n } catch (error) {\r\n console.warn(`Failed to set style ${prop}:`, error);\r\n }\r\n }\r\n }\r\n \r\n // Create shadow root if present\r\n if (serializedElement.shadowRoot && element.attachShadow) {\r\n try {\r\n const shadowRoot = element.attachShadow({ \r\n mode: serializedElement.shadowRoot.mode || 'open' \r\n });\r\n \r\n // Deserialize shadow root children\r\n for (const child of serializedElement.shadowRoot.children) {\r\n const childElement = this.deserializeElement(child, doc);\r\n if (childElement) {\r\n shadowRoot.appendChild(childElement);\r\n }\r\n }\r\n } catch (error) {\r\n console.warn('Failed to create shadow root:', error);\r\n }\r\n }\r\n \r\n // Deserialize children\r\n if (serializedElement.children) {\r\n for (const child of serializedElement.children) {\r\n const childElement = this.deserializeElement(child, doc);\r\n if (childElement) {\r\n element.appendChild(childElement);\r\n }\r\n }\r\n }\r\n \r\n // Handle frame content\r\n if (serializedElement.frameData && serializedElement.frameData.content) {\r\n // Frame content deserialization would happen after the frame loads\r\n element.addEventListener('load', () => {\r\n try {\r\n const frameDoc = element.contentDocument;\r\n if (frameDoc) {\r\n this.deserializeDocument(serializedElement.frameData.content, frameDoc);\r\n }\r\n } catch (error) {\r\n console.warn('Failed to deserialize frame content:', error);\r\n }\r\n });\r\n }\r\n \r\n return element;\r\n }\r\n \r\n /**\r\n * Deserialize document fragment\r\n */\r\n deserializeDocumentFragment(serializedFragment, doc) {\r\n const fragment = doc.createDocumentFragment();\r\n \r\n if (serializedFragment.children) {\r\n for (const child of serializedFragment.children) {\r\n const childElement = this.deserializeElement(child, doc);\r\n if (childElement) {\r\n fragment.appendChild(childElement);\r\n }\r\n }\r\n }\r\n \r\n return fragment;\r\n }\r\n }\r\n \r\n // Usage example and utility functions\r\n class DOMUtils {\r\n /**\r\n * Create serializer with common presets\r\n */\r\n static createSerializer(preset = 'default') {\r\n const presets = {\r\n default: {\r\n includeStyles: true,\r\n includeScripts: false,\r\n includeFrames: true,\r\n includeShadowDOM: true\r\n },\r\n minimal: {\r\n includeStyles: false,\r\n includeScripts: false,\r\n includeFrames: false,\r\n includeShadowDOM: false\r\n },\r\n complete: {\r\n includeStyles: true,\r\n includeScripts: true,\r\n includeFrames: true,\r\n includeShadowDOM: true\r\n },\r\n secure: {\r\n includeStyles: true,\r\n includeScripts: false,\r\n includeFrames: false,\r\n includeShadowDOM: true\r\n }\r\n };\r\n \r\n return new DOMSerializer(presets[preset] || presets.default);\r\n }\r\n \r\n /**\r\n * Serialize DOM to JSON string\r\n */\r\n static serializeToJSON(element, options) {\r\n const serializer = new DOMSerializer(options);\r\n const serialized = serializer.serialize(element);\r\n return JSON.stringify(serialized, null, 2);\r\n }\r\n \r\n /**\r\n * Deserialize from JSON string\r\n */\r\n static deserializeFromJSON(jsonString, targetDocument) {\r\n const serialized = JSON.parse(jsonString);\r\n const serializer = new DOMSerializer();\r\n return serializer.deserialize(serialized, targetDocument);\r\n }\r\n \r\n /**\r\n * Clone DOM with full fidelity including Shadow DOM\r\n */\r\n static deepClone(element, options) {\r\n const serializer = new DOMSerializer(options);\r\n const serialized = serializer.serialize(element);\r\n return serializer.deserialize(serialized, element.ownerDocument);\r\n }\r\n \r\n /**\r\n * Compare two DOM structures\r\n */\r\n static compare(element1, element2, options) {\r\n const serializer = new DOMSerializer(options);\r\n const serialized1 = serializer.serialize(element1);\r\n const serialized2 = serializer.serialize(element2);\r\n \r\n return JSON.stringify(serialized1) === JSON.stringify(serialized2);\r\n }\r\n }\r\n \r\n /*\r\n // Export for use\r\n if (typeof module !== 'undefined' && module.exports) {\r\n module.exports = { DOMSerializer, DOMUtils };\r\n } else if (typeof window !== 'undefined') {\r\n window.DOMSerializer = DOMSerializer;\r\n window.DOMUtils = DOMUtils;\r\n }\r\n */\r\n\r\n /* Usage Examples:\r\n \r\n // Basic serialization\r\n const serializer = new DOMSerializer();\r\n const serialized = serializer.serialize(document);\r\n console.log(JSON.stringify(serialized, null, 2));\r\n \r\n // Deserialize back to DOM\r\n const clonedDoc = serializer.deserialize(serialized);\r\n \r\n // Using presets\r\n const minimalSerializer = DOMUtils.createSerializer('minimal');\r\n const secureSerializer = DOMUtils.createSerializer('secure');\r\n \r\n // Serialize specific element with Shadow DOM\r\n const customElement = document.querySelector('my-custom-element');\r\n const serializedElement = serializer.serialize(customElement);\r\n \r\n // JSON utilities\r\n const jsonString = DOMUtils.serializeToJSON(document.body);\r\n const restored = DOMUtils.deserializeFromJSON(jsonString);\r\n \r\n // Deep clone with Shadow DOM support\r\n const clone = DOMUtils.deepClone(document.body, { includeShadowDOM: true });\r\n \r\n */\r\n\r\n function serializeNodeToJSON(nodeElement) {\r\n return DOMUtils.serializeToJSON(nodeElement, {includeStyles: false});\r\n }\r\n\r\n function deserializeNodeFromJSON(jsonString) {\r\n return DOMUtils.deserializeFromJSON(jsonString);\r\n }\n\n /**\r\n * Checks if a point is inside a bounding box\r\n * \r\n * @param point The point to check\r\n * @param box The bounding box\r\n * @returns boolean indicating if the point is inside the box\r\n */\r\n function isPointInsideBox(point, box) {\r\n return point.x >= box.x &&\r\n point.x <= box.x + box.width &&\r\n point.y >= box.y &&\r\n point.y <= box.y + box.height;\r\n }\r\n\r\n /**\r\n * Calculates the overlap area between two bounding boxes\r\n * \r\n * @param box1 First bounding box\r\n * @param box2 Second bounding box\r\n * @returns The overlap area\r\n */\r\n function calculateOverlap(box1, box2) {\r\n const xOverlap = Math.max(0,\r\n Math.min(box1.x + box1.width, box2.x + box2.width) -\r\n Math.max(box1.x, box2.x)\r\n );\r\n const yOverlap = Math.max(0,\r\n Math.min(box1.y + box1.height, box2.y + box2.height) -\r\n Math.max(box1.y, box2.y)\r\n );\r\n return xOverlap * yOverlap;\r\n }\r\n\r\n /**\r\n * Finds an exact match between candidate elements and the actual interaction element\r\n * \r\n * @param candidate_elements Array of candidate element infos\r\n * @param actualInteractionElementInfo The actual interaction element info\r\n * @returns The matching candidate element info, or null if no match is found\r\n */\r\n function findExactMatch(candidate_elements, actualInteractionElementInfo) {\r\n if (!actualInteractionElementInfo.element) {\r\n return null;\r\n }\r\n\r\n const exactMatch = candidate_elements.find(elementInfo => \r\n elementInfo.element && elementInfo.element === actualInteractionElementInfo.element\r\n );\r\n \r\n if (exactMatch) {\r\n console.log('✅ Found exact element match:', {\r\n matchedElement: exactMatch.element?.tagName,\r\n matchedElementClass: exactMatch.element?.className,\r\n index: exactMatch.index\r\n });\r\n return exactMatch;\r\n }\r\n \r\n return null;\r\n }\r\n\r\n /**\r\n * Finds a match by traversing up the parent elements\r\n * \r\n * @param candidate_elements Array of candidate element infos\r\n * @param actualInteractionElementInfo The actual interaction element info\r\n * @returns The matching candidate element info, or null if no match is found\r\n */\r\n function findParentMatch(candidate_elements, actualInteractionElementInfo) {\r\n if (!actualInteractionElementInfo.element) {\r\n return null;\r\n }\r\n\r\n let element = actualInteractionElementInfo.element;\r\n while (element.parentElement) {\r\n element = element.parentElement;\r\n const parentMatch = candidate_elements.find(candidate => \r\n candidate.element && candidate.element === element\r\n );\r\n \r\n if (parentMatch) {\r\n console.log('✅ Found parent element match:', {\r\n matchedElement: parentMatch.element?.tagName,\r\n matchedElementClass: parentMatch.element?.className,\r\n index: parentMatch.index,\r\n depth: element.tagName\r\n });\r\n return parentMatch;\r\n }\r\n \r\n // Stop if we hit another candidate element\r\n if (candidate_elements.some(candidate => \r\n candidate.element && candidate.element === element\r\n )) {\r\n console.log('⚠️ Stopped parent search - hit another candidate element:', element.tagName);\r\n break;\r\n }\r\n }\r\n \r\n return null;\r\n }\r\n\r\n /**\r\n * Finds a match based on spatial relationships between elements\r\n * \r\n * @param candidate_elements Array of candidate element infos\r\n * @param actualInteractionElementInfo The actual interaction element info\r\n * @returns The matching candidate element info, or null if no match is found\r\n */\r\n function findSpatialMatch(candidate_elements, actualInteractionElementInfo) {\r\n if (!actualInteractionElementInfo.element || !actualInteractionElementInfo.bounding_box) {\r\n return null;\r\n }\r\n\r\n const actualBox = actualInteractionElementInfo.bounding_box;\r\n let bestMatch = null;\r\n let bestScore = 0;\r\n\r\n for (const candidateInfo of candidate_elements) {\r\n if (!candidateInfo.bounding_box) continue;\r\n \r\n const candidateBox = candidateInfo.bounding_box;\r\n let score = 0;\r\n\r\n // Check if actual element is contained within candidate\r\n if (isPointInsideBox({ x: actualBox.x, y: actualBox.y }, candidateBox) &&\r\n isPointInsideBox({ x: actualBox.x + actualBox.width, y: actualBox.y + actualBox.height }, candidateBox)) {\r\n score += 100; // High score for containment\r\n }\r\n\r\n // Calculate overlap area as a factor\r\n const overlap = calculateOverlap(actualBox, candidateBox);\r\n score += overlap;\r\n\r\n // Consider proximity if no containment\r\n if (score === 0) {\r\n const distance = Math.sqrt(\r\n Math.pow((actualBox.x + actualBox.width/2) - (candidateBox.x + candidateBox.width/2), 2) +\r\n Math.pow((actualBox.y + actualBox.height/2) - (candidateBox.y + candidateBox.height/2), 2)\r\n );\r\n // Convert distance to a score (closer = higher score)\r\n score = 1000 / (distance + 1);\r\n }\r\n\r\n if (score > bestScore) {\r\n bestScore = score;\r\n bestMatch = candidateInfo;\r\n console.log('📏 New best spatial match:', {\r\n element: candidateInfo.element?.tagName,\r\n class: candidateInfo.element?.className,\r\n index: candidateInfo.index,\r\n score: score\r\n });\r\n }\r\n }\r\n\r\n if (bestMatch) {\r\n console.log('✅ Final spatial match selected:', {\r\n element: bestMatch.element?.tagName,\r\n class: bestMatch.element?.className,\r\n index: bestMatch.index,\r\n finalScore: bestScore\r\n });\r\n return bestMatch;\r\n }\r\n\r\n return null;\r\n }\r\n\r\n /**\r\n * Finds a matching candidate element for an actual interaction element\r\n * \r\n * @param candidate_elements Array of candidate element infos\r\n * @param actualInteractionElementInfo The actual interaction element info\r\n * @returns The matching candidate element info, or null if no match is found\r\n */\r\n function findMatchingCandidateElementInfo(candidate_elements, actualInteractionElementInfo) {\r\n if (!actualInteractionElementInfo.element || !actualInteractionElementInfo.bounding_box) {\r\n console.error('❌ Missing required properties in actualInteractionElementInfo');\r\n return null;\r\n }\r\n\r\n console.log('🔍 Starting element matching for:', {\r\n clickedElement: actualInteractionElementInfo.element.tagName,\r\n clickedElementClass: actualInteractionElementInfo.element.className,\r\n totalCandidates: candidate_elements.length\r\n });\r\n\r\n // First try exact element match\r\n const exactMatch = findExactMatch(candidate_elements, actualInteractionElementInfo);\r\n if (exactMatch) {\r\n return exactMatch;\r\n }\r\n console.log('❌ No exact element match found, trying parent matching...');\r\n\r\n // Try finding closest clickable parent\r\n const parentMatch = findParentMatch(candidate_elements, actualInteractionElementInfo);\r\n if (parentMatch) {\r\n return parentMatch;\r\n }\r\n console.log('❌ No parent match found, falling back to spatial matching...');\r\n\r\n // If no exact or parent match, look for spatial relationships\r\n const spatialMatch = findSpatialMatch(candidate_elements, actualInteractionElementInfo);\r\n if (spatialMatch) {\r\n return spatialMatch;\r\n }\r\n\r\n console.error('❌ No matching element found for actual interaction element:', actualInteractionElementInfo);\r\n return null;\r\n }\n\n const highlight = {\r\n execute: async function(elementTypes, handleScroll=false) {\r\n const elements = await findElements(elementTypes);\r\n highlightElements(elements, handleScroll);\r\n return elements;\r\n },\r\n\r\n unexecute: function(handleScroll=false) {\r\n unhighlightElements(handleScroll);\r\n },\r\n\r\n generateJSON: async function() {\r\n const json = {};\r\n\r\n // Capture viewport dimensions\r\n const viewportData = {\r\n width: window.innerWidth,\r\n height: window.innerHeight,\r\n documentWidth: document.documentElement.clientWidth,\r\n documentHeight: document.documentElement.clientHeight,\r\n timestamp: new Date().toISOString()\r\n };\r\n\r\n // Add viewport data to the JSON output\r\n json.viewport = viewportData;\r\n\r\n\r\n await Promise.all(Object.values(ElementTag).map(async elementType => {\r\n const elements = await findElements(elementType);\r\n json[elementType] = elements;\r\n }));\r\n\r\n // Serialize the JSON object\r\n const jsonString = JSON.stringify(json, null, 4); // Pretty print with 4 spaces\r\n\r\n console.log(`JSON: ${jsonString}`);\r\n return jsonString;\r\n },\r\n\r\n getElementInfo\r\n };\r\n\r\n\r\n function unhighlightElements(handleScroll=false) {\r\n const documents = getAllFrames();\r\n documents.forEach(doc => {\r\n const overlay = doc.getElementById('highlight-overlay');\r\n if (overlay) {\r\n if (handleScroll) {\r\n // Remove event listeners\r\n doc.removeEventListener('scroll', overlay.scrollHandler, true);\r\n doc.removeEventListener('resize', overlay.resizeHandler);\r\n }\r\n overlay.remove();\r\n }\r\n });\r\n }\r\n\r\n\r\n\r\n\r\n async function findElements(elementTypes, verbose=true) {\r\n const typesArray = Array.isArray(elementTypes) ? elementTypes : [elementTypes];\r\n console.log('Starting element search for types:', typesArray);\r\n\r\n const elements = [];\r\n typesArray.forEach(elementType => {\r\n if (elementType === ElementTag.FILLABLE) {\r\n elements.push(...findFillables());\r\n }\r\n if (elementType === ElementTag.SELECTABLE) {\r\n elements.push(...findDropdowns());\r\n }\r\n if (elementType === ElementTag.CLICKABLE) {\r\n elements.push(...findClickables());\r\n elements.push(...findToggles());\r\n elements.push(...findCheckables());\r\n }\r\n if (elementType === ElementTag.NON_INTERACTIVE_ELEMENT) {\r\n elements.push(...findNonInteractiveElements());\r\n }\r\n });\r\n\r\n // console.log('Before uniquify:', elements.length);\r\n const elementsWithInfo = elements.map((element, index) => \r\n getElementInfo(element, index)\r\n );\r\n\r\n \r\n \r\n const uniqueElements = uniquifyElements(elementsWithInfo);\r\n console.log(`Found ${uniqueElements.length} elements:`);\r\n \r\n // More comprehensive visibility check\r\n const visibleElements = uniqueElements.filter(elementInfo => {\r\n const el = elementInfo.element;\r\n const style = getComputedStyle(el);\r\n \r\n // Check various style properties that affect visibility\r\n if (style.display === 'none' || \r\n style.visibility === 'hidden') {\r\n return false;\r\n }\r\n \r\n // Check if element has non-zero dimensions\r\n const rect = el.getBoundingClientRect();\r\n if (rect.width === 0 || rect.height === 0) {\r\n return false;\r\n }\r\n \r\n // Check if element is within viewport\r\n if (rect.bottom < 0 || \r\n rect.top > window.innerHeight || \r\n rect.right < 0 || \r\n rect.left > window.innerWidth) {\r\n // Element is outside viewport, but still might be valid \r\n // if user scrolls to it, so we'll include it\r\n return true;\r\n }\r\n \r\n return true;\r\n });\r\n \r\n console.log(`Out of which ${visibleElements.length} elements are visible:`);\r\n if (verbose) {\r\n visibleElements.forEach(info => {\r\n console.log(`Element ${info.index}:`, info);\r\n });\r\n }\r\n \r\n return visibleElements;\r\n }\r\n\r\n // elements is an array of objects with index, xpath\r\n function highlightElements(elements, handleScroll=false) {\r\n // console.log('[highlightElements] called with', elements.length, 'elements');\r\n // Create overlay if it doesn't exist and store it in a dictionary\r\n const documents = getAllFrames(); \r\n let overlays = {};\r\n documents.forEach(doc => {\r\n let overlay = doc.getElementById('highlight-overlay');\r\n if (!overlay) {\r\n overlay = doc.createElement('div');\r\n overlay.id = 'highlight-overlay';\r\n overlay.style.cssText = `\r\n position: fixed;\r\n top: 0;\r\n left: 0;\r\n width: 100%;\r\n height: 100%;\r\n pointer-events: none;\r\n z-index: 2147483647;\r\n `;\r\n doc.body.appendChild(overlay);\r\n // console.log('[highlightElements] Created overlay in document:', doc);\r\n }\r\n overlays[doc.documentURI] = overlay;\r\n });\r\n \r\n\r\n const updateHighlights = (doc = null) => {\r\n if (doc) {\r\n overlays[doc.documentURI].innerHTML = '';\r\n } else {\r\n Object.values(overlays).forEach(overlay => { overlay.innerHTML = ''; });\r\n } \r\n elements.forEach((elementInfo, idx) => {\r\n //console.log(`[highlightElements] Processing element ${idx}:`, elementInfo.tag, elementInfo.css_selector, elementInfo.bounding_box);\r\n let element = elementInfo.element; //getElementByXPathOrCssSelector(elementInfo);\r\n if (!element) {\r\n element = getElementByXPathOrCssSelector(elementInfo);\r\n if (!element) {\r\n console.warn('[highlightElements] Could not find element for:', elementInfo);\r\n return;\r\n }\r\n }\r\n //if highlights requested for a specific doc, skip unrelated elements\r\n if (doc && element.ownerDocument !== doc) {\r\n console.log(\"[highlightElements] Skipped element since it doesn't belong to document\", doc);\r\n return;\r\n }\r\n const rect = element.getBoundingClientRect();\r\n if (rect.width === 0 || rect.height === 0) {\r\n console.warn('[highlightElements] Element has zero dimensions:', elementInfo);\r\n return;\r\n }\r\n // Create border highlight (red rectangle)\r\n // use ownerDocument to support iframes/frames\r\n const highlight = element.ownerDocument.createElement('div');\r\n highlight.style.cssText = `\r\n position: fixed;\r\n left: ${rect.x}px;\r\n top: ${rect.y}px;\r\n width: ${rect.width}px;\r\n height: ${rect.height}px;\r\n border: 1px solid rgb(255, 0, 0);\r\n transition: all 0.2s ease-in-out;\r\n `;\r\n // Create index label container - now positioned to the right and slightly up\r\n const labelContainer = element.ownerDocument.createElement('div');\r\n labelContainer.style.cssText = `\r\n position: absolute;\r\n right: -10px; /* Offset to the right */\r\n top: -10px; /* Offset upwards */\r\n padding: 4px;\r\n background-color: rgba(255, 255, 0, 0.6);\r\n display: flex;\r\n align-items: center;\r\n justify-content: center;\r\n `;\r\n const text = element.ownerDocument.createElement('span');\r\n text.style.cssText = `\r\n color: rgb(0, 0, 0, 0.8);\r\n font-family: 'Courier New', Courier, monospace;\r\n font-size: 12px;\r\n font-weight: bold;\r\n line-height: 1;\r\n `;\r\n text.textContent = elementInfo.index;\r\n labelContainer.appendChild(text);\r\n highlight.appendChild(labelContainer); \r\n overlays[element.ownerDocument.documentURI].appendChild(highlight);\r\n \r\n });\r\n };\r\n\r\n // Initial highlight\r\n updateHighlights();\r\n\r\n if (handleScroll) {\r\n documents.forEach(doc => {\r\n // Update highlights on scroll and resize\r\n console.log('registering scroll and resize handlers for document: ', doc);\r\n const scrollHandler = () => {\r\n requestAnimationFrame(() => updateHighlights(doc));\r\n };\r\n const resizeHandler = () => {\r\n updateHighlights(doc);\r\n };\r\n doc.addEventListener('scroll', scrollHandler, true);\r\n doc.addEventListener('resize', resizeHandler);\r\n // Store event handlers for cleanup\r\n overlays[doc.documentURI].scrollHandler = scrollHandler;\r\n overlays[doc.documentURI].resizeHandler = resizeHandler;\r\n }); \r\n }\r\n }\r\n\r\n // function unexecute() {\r\n // unhighlightElements();\r\n // }\r\n\r\n // Make it available globally for both Extension and Playwright\r\n if (typeof window !== 'undefined') {\r\n function stripElementRefs(elementInfo) {\r\n if (!elementInfo) return null;\r\n const { element, ...rest } = elementInfo;\r\n return rest;\r\n }\r\n\r\n window.ProboLabs = window.ProboLabs || {};\r\n\r\n // --- Caching State ---\r\n window.ProboLabs.candidates = [];\r\n window.ProboLabs.actual = null;\r\n window.ProboLabs.matchingCandidate = null;\r\n\r\n // --- Methods ---\r\n /**\r\n * Find and cache candidate elements of a given type (e.g., 'CLICKABLE').\r\n * NOTE: This function is async and must be awaited from Playwright/Node.\r\n */\r\n window.ProboLabs.findAndCacheCandidateElements = async function(elementType) {\r\n //console.log('[ProboLabs] findAndCacheCandidateElements called with:', elementType);\r\n const found = await findElements(elementType);\r\n window.ProboLabs.candidates = found;\r\n // console.log('[ProboLabs] candidates set to:', found, 'type:', typeof found, 'isArray:', Array.isArray(found));\r\n return found.length;\r\n };\r\n\r\n window.ProboLabs.findAndCacheActualElement = function(cssSelector, iframeSelector, isHover=false) {\r\n // console.log('[ProboLabs] findAndCacheActualElement called with:', cssSelector, iframeSelector);\r\n let el = findElement(document, iframeSelector, cssSelector);\r\n if(isHover) {\r\n const visibleElement = findClosestVisibleElement(el);\r\n if (visibleElement) {\r\n el = visibleElement;\r\n }\r\n }\r\n if (!el) {\r\n window.ProboLabs.actual = null;\r\n // console.log('[ProboLabs] actual set to null');\r\n return false;\r\n }\r\n window.ProboLabs.actual = getElementInfo(el, -1);\r\n // console.log('[ProboLabs] actual set to:', window.ProboLabs.actual);\r\n return true;\r\n };\r\n\r\n window.ProboLabs.findAndCacheMatchingCandidate = function() {\r\n // console.log('[ProboLabs] findAndCacheMatchingCandidate called');\r\n if (!window.ProboLabs.candidates.length || !window.ProboLabs.actual) {\r\n window.ProboLabs.matchingCandidate = null;\r\n // console.log('[ProboLabs] matchingCandidate set to null');\r\n return false;\r\n }\r\n window.ProboLabs.matchingCandidate = findMatchingCandidateElementInfo(window.ProboLabs.candidates, window.ProboLabs.actual);\r\n // console.log('[ProboLabs] matchingCandidate set to:', window.ProboLabs.matchingCandidate);\r\n return !!window.ProboLabs.matchingCandidate;\r\n };\r\n\r\n window.ProboLabs.highlightCachedElements = function(which) {\r\n let elements = [];\r\n if (which === 'candidates') elements = window.ProboLabs.candidates;\r\n if (which === 'actual' && window.ProboLabs.actual) elements = [window.ProboLabs.actual];\r\n if (which === 'matching' && window.ProboLabs.matchingCandidate) elements = [window.ProboLabs.matchingCandidate];\r\n console.log(`[ProboLabs] highlightCachedElements ${which} with ${elements.length} elements`);\r\n highlightElements(elements);\r\n };\r\n\r\n window.ProboLabs.unhighlight = function() {\r\n // console.log('[ProboLabs] unhighlight called');\r\n unhighlightElements();\r\n };\r\n\r\n window.ProboLabs.reset = function() {\r\n console.log('[ProboLabs] reset called');\r\n window.ProboLabs.candidates = [];\r\n window.ProboLabs.actual = null;\r\n window.ProboLabs.matchingCandidate = null;\r\n unhighlightElements();\r\n };\r\n\r\n window.ProboLabs.getCandidates = function() {\r\n // console.log('[ProboLabs] getCandidates called. candidates:', window.ProboLabs.candidates, 'type:', typeof window.ProboLabs.candidates, 'isArray:', Array.isArray(window.ProboLabs.candidates));\r\n const arr = Array.isArray(window.ProboLabs.candidates) ? window.ProboLabs.candidates : [];\r\n return arr.map(stripElementRefs);\r\n };\r\n window.ProboLabs.getActual = function() {\r\n return stripElementRefs(window.ProboLabs.actual);\r\n };\r\n window.ProboLabs.getMatchingCandidate = function() {\r\n return stripElementRefs(window.ProboLabs.matchingCandidate);\r\n };\r\n\r\n // Retain existing API for backward compatibility\r\n window.ProboLabs.ElementTag = ElementTag;\r\n window.ProboLabs.highlightElements = highlightElements;\r\n window.ProboLabs.unhighlightElements = unhighlightElements;\r\n window.ProboLabs.findElements = findElements;\r\n window.ProboLabs.getElementInfo = getElementInfo;\r\n window.ProboLabs.highlight = window.ProboLabs.highlight;\r\n window.ProboLabs.unhighlight = window.ProboLabs.unhighlight;\r\n\r\n // --- Utility Functions ---\r\n function findClosestVisibleElement(element) {\r\n let current = element;\r\n while (current) {\r\n const style = window.getComputedStyle(current);\r\n if (\r\n style &&\r\n style.display !== 'none' &&\r\n style.visibility !== 'hidden' &&\r\n current.offsetWidth > 0 &&\r\n current.offsetHeight > 0\r\n ) {\r\n return current;\r\n }\r\n if (!current.parentElement || current === document.body) break;\r\n current = current.parentElement;\r\n }\r\n return null;\r\n }\r\n }\n\n exports.ElementInfo = ElementInfo;\n exports.ElementTag = ElementTag;\n exports.deserializeNodeFromJSON = deserializeNodeFromJSON;\n exports.detectScrollableContainers = detectScrollableContainers;\n exports.findElement = findElement;\n exports.findElements = findElements;\n exports.generateCssPath = generateCssPath;\n exports.getAriaLabelledByText = getAriaLabelledByText;\n exports.getContainingIframe = getContainingIframe;\n exports.getElementInfo = getElementInfo;\n exports.getRobustSelector = getRobustSelector;\n exports.highlight = highlight;\n exports.highlightElements = highlightElements;\n exports.isScrollableContainer = isScrollableContainer;\n exports.serializeNodeToJSON = serializeNodeToJSON;\n exports.unhighlightElements = unhighlightElements;\n\n}));\n//# sourceMappingURL=probolabs.umd.js.map\n";
|
|
1
|
+
const highlighterCode = "(function (global, factory) {\n typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :\n typeof define === 'function' && define.amd ? define(['exports'], factory) :\n (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ProboLabs = {}));\n})(this, (function (exports) { 'use strict';\n\n const ElementTag = {\n CLICKABLE: \"CLICKABLE\", // button, link, toggle switch, checkbox, radio, dropdowns, clickable divs\n FILLABLE: \"FILLABLE\", // input, textarea content_editable, date picker??\n SELECTABLE: \"SELECTABLE\", // select\n NON_INTERACTIVE_ELEMENT: 'NON_INTERACTIVE_ELEMENT',\n };\n\n class ElementInfo {\n constructor(element, index, {tag, type, text, html, xpath, css_selector, bounding_box, iframe_selector, short_css_selector, short_iframe_selector}) {\n this.index = index.toString();\n this.tag = tag;\n this.type = type;\n this.text = text;\n this.html = html;\n this.xpath = xpath;\n this.css_selector = css_selector;\n this.bounding_box = bounding_box;\n this.iframe_selector = iframe_selector;\n this.element = element;\n this.depth = -1;\n this.short_css_selector = short_css_selector;\n this.short_iframe_selector = short_iframe_selector;\n }\n\n getSelector() {\n return this.xpath ? this.xpath : this.css_selector;\n }\n\n getDepth() {\n if (this.depth >= 0) {\n return this.depth;\n }\n \n this.depth = 0;\n let currentElement = this.element;\n \n while (currentElement.nodeType === Node.ELEMENT_NODE) { \n this.depth++;\n currentElement = getParentNode(currentElement);\n }\n \n return this.depth;\n }\n }\n\n function getParentNode(element) {\n if (!element || element.nodeType !== Node.ELEMENT_NODE) return null;\n \n let parent = null;\n // SF is using slots and shadow DOM heavily\n // However, there might be slots in the light DOM which shouldn't be traversed\n if (element.assignedSlot && element.getRootNode() instanceof ShadowRoot)\n parent = element.assignedSlot;\n else \n parent = element.parentNode;\n \n // Check if we're at a shadow root\n if (parent && parent.nodeType !== Node.ELEMENT_NODE && parent.getRootNode() instanceof ShadowRoot) \n parent = parent.getRootNode().host; \n\n return parent;\n }\n\n // License: MIT\n // Author: Anton Medvedev <anton@medv.io>\n // Source: https://github.com/antonmedv/finder\n const acceptedAttrNames = new Set(['role', 'name', 'aria-label', 'rel', 'href']);\n /** Check if attribute name and value are word-like. */\n function attr(name, value) {\n let nameIsOk = acceptedAttrNames.has(name);\n nameIsOk ||= name.startsWith('data-') && wordLike(name);\n let valueIsOk = wordLike(value) && value.length < 100;\n valueIsOk ||= value.startsWith('#') && wordLike(value.slice(1));\n return nameIsOk && valueIsOk;\n }\n /** Check if id name is word-like. */\n function idName(name) {\n return wordLike(name);\n }\n /** Check if class name is word-like. */\n function className(name) {\n return wordLike(name);\n }\n /** Check if tag name is word-like. */\n function tagName(name) {\n return true;\n }\n /** Finds unique CSS selectors for the given element. */\n function finder(input, options) {\n if (input.nodeType !== Node.ELEMENT_NODE) {\n throw new Error(`Can't generate CSS selector for non-element node type.`);\n }\n if (input.tagName.toLowerCase() === 'html') {\n return 'html';\n }\n const defaults = {\n root: document.body,\n idName: idName,\n className: className,\n tagName: tagName,\n attr: attr,\n timeoutMs: 1000,\n seedMinLength: 3,\n optimizedMinLength: 2,\n maxNumberOfPathChecks: Infinity,\n };\n const startTime = new Date();\n const config = { ...defaults, ...options };\n const rootDocument = findRootDocument(config.root, defaults);\n let foundPath;\n let count = 0;\n for (const candidate of search(input, config, rootDocument)) {\n const elapsedTimeMs = new Date().getTime() - startTime.getTime();\n if (elapsedTimeMs > config.timeoutMs ||\n count >= config.maxNumberOfPathChecks) {\n const fPath = fallback(input, rootDocument);\n if (!fPath) {\n throw new Error(`Timeout: Can't find a unique selector after ${config.timeoutMs}ms`);\n }\n return selector(fPath);\n }\n count++;\n if (unique(candidate, rootDocument)) {\n foundPath = candidate;\n break;\n }\n }\n if (!foundPath) {\n throw new Error(`Selector was not found.`);\n }\n const optimized = [\n ...optimize(foundPath, input, config, rootDocument, startTime),\n ];\n optimized.sort(byPenalty);\n if (optimized.length > 0) {\n return selector(optimized[0]);\n }\n return selector(foundPath);\n }\n function* search(input, config, rootDocument) {\n const stack = [];\n let paths = [];\n let current = input;\n let i = 0;\n while (current && current !== rootDocument) {\n const level = tie(current, config);\n for (const node of level) {\n node.level = i;\n }\n stack.push(level);\n current = current.parentElement;\n i++;\n paths.push(...combinations(stack));\n if (i >= config.seedMinLength) {\n paths.sort(byPenalty);\n for (const candidate of paths) {\n yield candidate;\n }\n paths = [];\n }\n }\n paths.sort(byPenalty);\n for (const candidate of paths) {\n yield candidate;\n }\n }\n function wordLike(name) {\n if (/^[a-z\\-]{3,}$/i.test(name)) {\n const words = name.split(/-|[A-Z]/);\n for (const word of words) {\n if (word.length <= 2) {\n return false;\n }\n if (/[^aeiou]{4,}/i.test(word)) {\n return false;\n }\n }\n return true;\n }\n return false;\n }\n function tie(element, config) {\n const level = [];\n const elementId = element.getAttribute('id');\n if (elementId && config.idName(elementId)) {\n level.push({\n name: '#' + CSS.escape(elementId),\n penalty: 0,\n });\n }\n for (let i = 0; i < element.classList.length; i++) {\n const name = element.classList[i];\n if (config.className(name)) {\n level.push({\n name: '.' + CSS.escape(name),\n penalty: 1,\n });\n }\n }\n for (let i = 0; i < element.attributes.length; i++) {\n const attr = element.attributes[i];\n if (config.attr(attr.name, attr.value)) {\n level.push({\n name: `[${CSS.escape(attr.name)}=\"${CSS.escape(attr.value)}\"]`,\n penalty: 2,\n });\n }\n }\n const tagName = element.tagName.toLowerCase();\n if (config.tagName(tagName)) {\n level.push({\n name: tagName,\n penalty: 5,\n });\n const index = indexOf(element, tagName);\n if (index !== undefined) {\n level.push({\n name: nthOfType(tagName, index),\n penalty: 10,\n });\n }\n }\n const nth = indexOf(element);\n if (nth !== undefined) {\n level.push({\n name: nthChild(tagName, nth),\n penalty: 50,\n });\n }\n return level;\n }\n function selector(path) {\n let node = path[0];\n let query = node.name;\n for (let i = 1; i < path.length; i++) {\n const level = path[i].level || 0;\n if (node.level === level - 1) {\n query = `${path[i].name} > ${query}`;\n }\n else {\n query = `${path[i].name} ${query}`;\n }\n node = path[i];\n }\n return query;\n }\n function penalty(path) {\n return path.map((node) => node.penalty).reduce((acc, i) => acc + i, 0);\n }\n function byPenalty(a, b) {\n return penalty(a) - penalty(b);\n }\n function indexOf(input, tagName) {\n const parent = input.parentNode;\n if (!parent) {\n return undefined;\n }\n let child = parent.firstChild;\n if (!child) {\n return undefined;\n }\n let i = 0;\n while (child) {\n if (child.nodeType === Node.ELEMENT_NODE &&\n (tagName === undefined ||\n child.tagName.toLowerCase() === tagName)) {\n i++;\n }\n if (child === input) {\n break;\n }\n child = child.nextSibling;\n }\n return i;\n }\n function fallback(input, rootDocument) {\n let i = 0;\n let current = input;\n const path = [];\n while (current && current !== rootDocument) {\n const tagName = current.tagName.toLowerCase();\n const index = indexOf(current, tagName);\n if (index === undefined) {\n return;\n }\n path.push({\n name: nthOfType(tagName, index),\n penalty: NaN,\n level: i,\n });\n current = current.parentElement;\n i++;\n }\n if (unique(path, rootDocument)) {\n return path;\n }\n }\n function nthChild(tagName, index) {\n if (tagName === 'html') {\n return 'html';\n }\n return `${tagName}:nth-child(${index})`;\n }\n function nthOfType(tagName, index) {\n if (tagName === 'html') {\n return 'html';\n }\n return `${tagName}:nth-of-type(${index})`;\n }\n function* combinations(stack, path = []) {\n if (stack.length > 0) {\n for (let node of stack[0]) {\n yield* combinations(stack.slice(1, stack.length), path.concat(node));\n }\n }\n else {\n yield path;\n }\n }\n function findRootDocument(rootNode, defaults) {\n if (rootNode.nodeType === Node.DOCUMENT_NODE) {\n return rootNode;\n }\n if (rootNode === defaults.root) {\n return rootNode.ownerDocument;\n }\n return rootNode;\n }\n function unique(path, rootDocument) {\n const css = selector(path);\n switch (rootDocument.querySelectorAll(css).length) {\n case 0:\n throw new Error(`Can't select any node with this selector: ${css}`);\n case 1:\n return true;\n default:\n return false;\n }\n }\n function* optimize(path, input, config, rootDocument, startTime) {\n if (path.length > 2 && path.length > config.optimizedMinLength) {\n for (let i = 1; i < path.length - 1; i++) {\n const elapsedTimeMs = new Date().getTime() - startTime.getTime();\n if (elapsedTimeMs > config.timeoutMs) {\n return;\n }\n const newPath = [...path];\n newPath.splice(i, 1);\n if (unique(newPath, rootDocument) &&\n rootDocument.querySelector(selector(newPath)) === input) {\n yield newPath;\n yield* optimize(newPath, input, config, rootDocument, startTime);\n }\n }\n }\n }\n\n // import { realpath } from \"fs\";\n\n function getAllDocumentElementsIncludingShadow(selectors, root = document) {\n const elements = Array.from(root.querySelectorAll(selectors));\n\n root.querySelectorAll('*').forEach(el => {\n if (el.shadowRoot) {\n elements.push(...getAllDocumentElementsIncludingShadow(selectors, el.shadowRoot));\n }\n });\n return elements;\n }\n\n function getAllFrames(root = document) {\n const result = [root];\n const frames = getAllDocumentElementsIncludingShadow('frame, iframe', root); \n frames.forEach(frame => {\n try {\n const frameDocument = frame.contentDocument || frame.contentWindow.document;\n if (frameDocument) {\n result.push(frameDocument);\n }\n } catch (e) {\n // Skip cross-origin frames\n console.warn('Could not access frame content:', e.message);\n }\n });\n\n return result;\n }\n\n function getAllElementsIncludingShadow(selectors, root = document) {\n const elements = [];\n\n getAllFrames(root).forEach(doc => {\n elements.push(...getAllDocumentElementsIncludingShadow(selectors, doc));\n });\n\n return elements;\n }\n\n /**\n * Deeply searches through DOM trees including Shadow DOM and frames/iframes\n * @param {string} selector - CSS selector to search for\n * @param {Document|Element} [root=document] - Starting point for the search\n * @param {Object} [options] - Search options\n * @param {boolean} [options.searchShadow=true] - Whether to search Shadow DOM\n * @param {boolean} [options.searchFrames=true] - Whether to search frames/iframes\n * @returns {Element[]} Array of found elements\n \n function getAllElementsIncludingShadow(selector, root = document, options = {}) {\n const {\n searchShadow = true,\n searchFrames = true\n } = options;\n\n const results = new Set();\n \n // Helper to check if an element is valid and not yet found\n const addIfValid = (element) => {\n if (element && !results.has(element)) {\n results.add(element);\n }\n };\n\n // Helper to process a single document or element\n function processNode(node) {\n // Search regular DOM\n node.querySelectorAll(selector).forEach(addIfValid);\n\n if (searchShadow) {\n // Search all shadow roots\n const treeWalker = document.createTreeWalker(\n node,\n NodeFilter.SHOW_ELEMENT,\n {\n acceptNode: (element) => {\n return element.shadowRoot ? \n NodeFilter.FILTER_ACCEPT : \n NodeFilter.FILTER_SKIP;\n }\n }\n );\n\n while (treeWalker.nextNode()) {\n const element = treeWalker.currentNode;\n if (element.shadowRoot) {\n // Search within shadow root\n element.shadowRoot.querySelectorAll(selector).forEach(addIfValid);\n // Recursively process the shadow root for nested shadow DOMs\n processNode(element.shadowRoot);\n }\n }\n }\n\n if (searchFrames) {\n // Search frames and iframes\n const frames = node.querySelectorAll('frame, iframe');\n frames.forEach(frame => {\n try {\n const frameDocument = frame.contentDocument;\n if (frameDocument) {\n processNode(frameDocument);\n }\n } catch (e) {\n // Skip cross-origin frames\n console.warn('Could not access frame content:', e.message);\n }\n });\n }\n }\n\n // Start processing from the root\n processNode(root);\n\n return Array.from(results);\n }\n */\n // <div x=1 y=2 role='combobox'> </div>\n function findDropdowns() {\n const dropdowns = [];\n \n // Native select elements\n dropdowns.push(...getAllElementsIncludingShadow('select'));\n \n // Elements with dropdown roles that don't have <input>..</input>\n const roleElements = getAllElementsIncludingShadow('[role=\"combobox\"], [role=\"listbox\"], [role=\"dropdown\"], [role=\"option\"], [role=\"menu\"], [role=\"menuitem\"]').filter(el => {\n return el.tagName.toLowerCase() !== 'input' || ![\"button\", \"checkbox\", \"radio\"].includes(el.getAttribute(\"type\"));\n });\n dropdowns.push(...roleElements);\n \n // Common dropdown class patterns\n const dropdownPattern = /.*(dropdown|select|combobox|menu).*/i;\n const elements = getAllElementsIncludingShadow('*');\n const dropdownClasses = Array.from(elements).filter(el => {\n const hasDropdownClass = dropdownPattern.test(el.className);\n const validTag = ['li', 'ul', 'span', 'div', 'p', 'a', 'button'].includes(el.tagName.toLowerCase());\n const style = window.getComputedStyle(el); \n const result = hasDropdownClass && validTag && (style.cursor === 'pointer' || el.tagName.toLowerCase() === 'a' || el.tagName.toLowerCase() === 'button');\n return result;\n });\n \n dropdowns.push(...dropdownClasses);\n \n // Elements with aria-haspopup attribute\n dropdowns.push(...getAllElementsIncludingShadow('[aria-haspopup=\"true\"], [aria-haspopup=\"listbox\"], [aria-haspopup=\"menu\"]'));\n\n // Improve navigation element detection\n // Semantic nav elements with list items\n dropdowns.push(...getAllElementsIncludingShadow('nav ul li, nav ol li'));\n \n // Navigation elements in common design patterns\n dropdowns.push(...getAllElementsIncludingShadow('header a, .header a, .nav a, .navigation a, .menu a, .sidebar a, aside a'));\n \n // Elements in primary navigation areas with common attributes\n dropdowns.push(...getAllElementsIncludingShadow('[role=\"navigation\"] a, [aria-label*=\"navigation\"] a, [aria-label*=\"menu\"] a'));\n\n return dropdowns;\n }\n\n function findClickables() {\n const clickables = [];\n \n const checkboxPattern = /checkbox/i;\n // Collect all clickable elements first\n const nativeLinks = [...getAllElementsIncludingShadow('a')];\n const nativeButtons = [...getAllElementsIncludingShadow('button')];\n const inputButtons = [...getAllElementsIncludingShadow('input[type=\"button\"], input[type=\"submit\"], input[type=\"reset\"]')];\n const roleButtons = [...getAllElementsIncludingShadow('[role=\"button\"]')];\n // const tabbable = [...getAllElementsIncludingShadow('[tabindex=\"0\"]')];\n const clickHandlers = [...getAllElementsIncludingShadow('[onclick]')];\n const dropdowns = findDropdowns();\n const nativeCheckboxes = [...getAllElementsIncludingShadow('input[type=\"checkbox\"]')]; \n const fauxCheckboxes = getAllElementsIncludingShadow('*').filter(el => {\n if (checkboxPattern.test(el.className)) {\n const realCheckboxes = getAllElementsIncludingShadow('input[type=\"checkbox\"]', el);\n if (realCheckboxes.length === 1) {\n const boundingRect = realCheckboxes[0].getBoundingClientRect();\n return boundingRect.width <= 1 && boundingRect.height <= 1 \n }\n }\n return false;\n });\n const nativeRadios = [...getAllElementsIncludingShadow('input[type=\"radio\"]')];\n const toggles = findToggles();\n const pointerElements = findElementsWithPointer();\n // Add all elements at once\n clickables.push(\n ...nativeLinks,\n ...nativeButtons,\n ...inputButtons,\n ...roleButtons,\n // ...tabbable,\n ...clickHandlers,\n ...dropdowns,\n ...nativeCheckboxes,\n ...fauxCheckboxes,\n ...nativeRadios,\n ...toggles,\n ...pointerElements\n );\n\n // Only uniquify once at the end\n return clickables; // Let findElements handle the uniquification\n }\n\n function findToggles() {\n const toggles = [];\n const checkboxes = getAllElementsIncludingShadow('input[type=\"checkbox\"]');\n const togglePattern = /switch|toggle|slider/i;\n\n checkboxes.forEach(checkbox => {\n let isToggle = false;\n\n // Check the checkbox itself\n if (togglePattern.test(checkbox.className) || togglePattern.test(checkbox.getAttribute('role') || '')) {\n isToggle = true;\n }\n\n // Check parent elements (up to 3 levels)\n if (!isToggle) {\n let element = checkbox;\n for (let i = 0; i < 3; i++) {\n const parent = element.parentElement;\n if (!parent) break;\n\n const className = parent.className || '';\n const role = parent.getAttribute('role') || '';\n\n if (togglePattern.test(className) || togglePattern.test(role)) {\n isToggle = true;\n break;\n }\n element = parent;\n }\n }\n\n // Check next sibling\n if (!isToggle) {\n const nextSibling = checkbox.nextElementSibling;\n if (nextSibling) {\n const className = nextSibling.className || '';\n const role = nextSibling.getAttribute('role') || '';\n if (togglePattern.test(className) || togglePattern.test(role)) {\n isToggle = true;\n }\n }\n }\n\n if (isToggle) {\n toggles.push(checkbox);\n }\n });\n\n return toggles;\n }\n\n function findNonInteractiveElements() {\n // Get all elements in the document\n const all = Array.from(getAllElementsIncludingShadow('*'));\n \n // Filter elements based on Python implementation rules\n return all.filter(element => {\n if (!element.firstElementChild) {\n const tag = element.tagName.toLowerCase(); \n if (!['select', 'button', 'a'].includes(tag)) {\n const validTags = ['p', 'span', 'div', 'input', 'textarea'].includes(tag) || /^h\\d$/.test(tag) || /text/.test(tag);\n const boundingRect = element.getBoundingClientRect();\n return validTags && boundingRect.height > 1 && boundingRect.width > 1;\n }\n }\n return false;\n });\n }\n\n\n\n // export function findNonInteractiveElements() {\n // const all = [];\n // try {\n // const elements = getAllElementsIncludingShadow('*');\n // all.push(...elements);\n // } catch (e) {\n // console.warn('Error getting elements:', e);\n // }\n \n // console.debug('Total elements found:', all.length);\n \n // return all.filter(element => {\n // try {\n // const tag = element.tagName.toLowerCase(); \n\n // // Special handling for input elements\n // if (tag === 'input' || tag === 'textarea') {\n // const boundingRect = element.getBoundingClientRect();\n // const value = element.value || '';\n // const placeholder = element.placeholder || '';\n // return boundingRect.height > 1 && \n // boundingRect.width > 1 && \n // (value.trim() !== '' || placeholder.trim() !== '');\n // }\n\n \n // // Check if it's a valid tag for text content\n // const validTags = ['p', 'span', 'div', 'label', 'th', 'td', 'li', 'button', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'select'].includes(tag) || \n // /^h\\d$/.test(tag) || \n // /text/.test(tag);\n\n // const boundingRect = element.getBoundingClientRect();\n\n // // Get direct text content, excluding child element text\n // let directText = '';\n // for (const node of element.childNodes) {\n // // Only include text nodes (nodeType 3)\n // if (node.nodeType === 3) {\n // directText += node.textContent || '';\n // }\n // }\n \n // // If no direct text and it's a table cell or heading, check label content\n // if (!directText.trim() && (tag === 'th' || tag === 'td' || tag === 'h1')) {\n // const labels = element.getElementsByTagName('label');\n // for (const label of labels) {\n // directText += label.textContent || '';\n // }\n // }\n\n // // If still no text and it's a heading, get all text content\n // if (!directText.trim() && tag === 'h1') {\n // directText = element.textContent || '';\n // }\n\n // directText = directText.trim();\n\n // // Debug logging\n // if (directText) {\n // console.debugg('Text element found:', {\n // tag,\n // text: directText,\n // dimensions: boundingRect,\n // element\n // });\n // }\n\n // return validTags && \n // boundingRect.height > 1 && \n // boundingRect.width > 1 && \n // directText !== '';\n \n // } catch (e) {\n // console.warn('Error processing element:', e);\n // return false;\n // }\n // });\n // }\n\n\n\n\n\n function findElementsWithPointer() {\n const elements = [];\n const allElements = getAllElementsIncludingShadow('*');\n \n console.log('Checking elements with pointer style...');\n \n allElements.forEach(element => {\n // Skip SVG elements for now\n if (element instanceof SVGElement || element.tagName.toLowerCase() === 'svg') {\n return;\n }\n \n const style = window.getComputedStyle(element);\n if (style.cursor === 'pointer') {\n elements.push(element);\n }\n });\n \n console.log(`Found ${elements.length} elements with pointer cursor`);\n return elements;\n }\n\n function findCheckables() {\n const elements = [];\n\n elements.push(...getAllElementsIncludingShadow('input[type=\"checkbox\"]'));\n elements.push(...getAllElementsIncludingShadow('input[type=\"radio\"]'));\n const all_elements = getAllElementsIncludingShadow('label');\n const radioClasses = Array.from(all_elements).filter(el => {\n return /.*radio.*/i.test(el.className); \n });\n elements.push(...radioClasses);\n return elements;\n }\n\n function findFillables() {\n const elements = [];\n\n const inputs = [...getAllElementsIncludingShadow('input:not([type=\"radio\"]):not([type=\"checkbox\"])')];\n console.log('Found inputs:', inputs.length, inputs);\n elements.push(...inputs);\n \n const textareas = [...getAllElementsIncludingShadow('textarea')];\n console.log('Found textareas:', textareas.length);\n elements.push(...textareas);\n \n const editables = [...getAllElementsIncludingShadow('[contenteditable=\"true\"]')];\n console.log('Found editables:', editables.length);\n elements.push(...editables);\n\n return elements;\n }\n\n // Helper function to check if element is a form control\n function isFormControl(elementInfo) {\n return /^(input|select|textarea|button|label)$/i.test(elementInfo.tag);\n }\n\n const isDropdownItem = (elementInfo) => {\n const dropdownPatterns = [\n /dropdown[-_]?item/i, // matches: dropdown-item, dropdownitem, dropdown_item\n /menu[-_]?item/i, // matches: menu-item, menuitem, menu_item\n /dropdown[-_]?link/i, // matches: dropdown-link, dropdownlink, dropdown_link\n /list[-_]?item/i, // matches: list-item, listitem, list_item\n /select[-_]?item/i, // matches: select-item, selectitem, select_item \n ];\n\n const rolePatterns = [\n /menu[-_]?item/i, // matches: menuitem, menu-item\n /option/i, // matches: option\n /list[-_]?item/i, // matches: listitem, list-item\n /tree[-_]?item/i // matches: treeitem, tree-item\n ];\n\n const hasMatchingClass = elementInfo.element.className && \n dropdownPatterns.some(pattern => \n pattern.test(elementInfo.element.className)\n );\n\n const hasMatchingRole = elementInfo.element.getAttribute('role') && \n rolePatterns.some(pattern => \n pattern.test(elementInfo.element.getAttribute('role'))\n );\n\n return hasMatchingClass || hasMatchingRole;\n };\n\n /**\n * Finds the first element matching a CSS selector, traversing Shadow DOM if necessary\n * @param {string} selector - CSS selector to search for\n * @param {Element} [root=document] - Root element to start searching from\n * @returns {Element|null} - The first matching element or null if not found\n */\n function querySelectorShadow(selector, root = document) {\n // First try to find in light DOM\n let element = root.querySelector(selector);\n if (element) return element;\n \n // Get all elements with shadow root\n const shadowElements = Array.from(root.querySelectorAll('*'))\n .filter(el => el.shadowRoot);\n \n // Search through each shadow root until we find a match\n for (const el of shadowElements) {\n element = querySelectorShadow(selector, el.shadowRoot);\n if (element) return element;\n }\n \n return null;\n }\n\n const getElementByXPathOrCssSelector = (element_info) => {\n console.log('getElementByXPathOrCssSelector:', element_info);\n\n findElement(document, element_info.iframe_selector, element_info.css_selector);\n };\n\n const findElement = (root, iframeSelector, cssSelector) => {\n let element;\n \n if (iframeSelector) { \n const frames = getAllDocumentElementsIncludingShadow('iframe', root);\n \n // Iterate over all frames and compare their CSS selectors\n for (const frame of frames) {\n const selector = generateCssPath(frame);\n if (selector === iframeSelector) {\n const frameDocument = frame.contentDocument || frame.contentWindow.document;\n element = querySelectorShadow(cssSelector, frameDocument);\n console.log('found element ', element);\n break;\n } \n } }\n else\n element = querySelectorShadow(cssSelector, root);\n \n if (!element) {\n console.warn('Failed to find element with CSS selector:', cssSelector);\n }\n\n return element;\n };\n\n\n function isDecendent(parent, child) {\n let element = child;\n while (element && element !== parent && element.nodeType === Node.ELEMENT_NODE) { \n element = getParentNode(element); \n }\n return element === parent;\n }\n\n function generateXPath(element) {\n return '/'+extractElementPath(element).map(item => `${item.tagName}${item.onlyChild ? '' : `[${item.index}]`}`).join('/');\n }\n\n function generateCssPath(element) {\n return extractElementPath(element).map(item => `${item.tagName}:nth-of-type(${item.index})`).join(' > ');\n }\n\n function extractElementPath(element) {\n if (!element) {\n console.error('ERROR: No element provided to generatePath');\n return [];\n }\n const path = [];\n // traversing up the DOM tree\n while (element && element.nodeType === Node.ELEMENT_NODE) { \n let tagName = element.nodeName.toLowerCase();\n \n let sibling = element;\n let index = 1;\n \n while (sibling = sibling.previousElementSibling) {\n if (sibling.nodeName.toLowerCase() === tagName) index++;\n }\n sibling = element;\n \n let onlyChild = (index === 1);\n while (onlyChild && (sibling = sibling.nextElementSibling)) {\n if (sibling.nodeName.toLowerCase() === tagName) onlyChild = false;\n }\n \n // add a tuple with tagName, index (nth), and onlyChild \n path.unshift({\n tagName: tagName,\n index: index,\n onlyChild: onlyChild \n }); \n\n element = getParentNode(element);\n }\n \n return path;\n }\n\n function cleanHTML(rawHTML) {\n const parser = new DOMParser();\n const doc = parser.parseFromString(rawHTML, \"text/html\");\n\n function cleanElement(element) {\n const allowedAttributes = new Set([\n \"role\",\n \"type\",\n \"class\",\n \"href\",\n \"alt\",\n \"title\",\n \"readonly\",\n \"checked\",\n \"enabled\",\n \"disabled\",\n ]);\n\n [...element.attributes].forEach(attr => {\n const name = attr.name.toLowerCase();\n const value = attr.value;\n\n const isTestAttribute = /^(testid|test-id|data-test-id)$/.test(name);\n const isDataAttribute = name.startsWith(\"data-\") && value;\n const isBooleanAttribute = [\"readonly\", \"checked\", \"enabled\", \"disabled\"].includes(name);\n\n if (!allowedAttributes.has(name) && !isDataAttribute && !isTestAttribute && !isBooleanAttribute) {\n element.removeAttribute(name);\n }\n });\n\n // Handle SVG content - more aggressive replacement\n if (element.tagName.toLowerCase() === \"svg\") {\n // Remove all attributes except class and role\n [...element.attributes].forEach(attr => {\n const name = attr.name.toLowerCase();\n if (name !== \"class\" && name !== \"role\") {\n element.removeAttribute(name);\n }\n });\n element.innerHTML = \"CONTENT REMOVED\";\n } else {\n // Recursively clean child elements\n Array.from(element.children).forEach(cleanElement);\n }\n\n // Only remove empty elements that aren't semantic or icon elements\n const keepEmptyElements = ['i', 'span', 'svg', 'button', 'input'];\n if (!keepEmptyElements.includes(element.tagName.toLowerCase()) && \n !element.children.length && \n !element.textContent.trim()) {\n element.remove();\n }\n }\n\n // Process all elements in the document body\n Array.from(doc.body.children).forEach(cleanElement);\n return doc.body.innerHTML;\n }\n\n function getContainingIframe(element) {\n // If not in an iframe, return null\n if (element.ownerDocument.defaultView === window.top) {\n return null;\n }\n \n // Try to find the iframe in the parent document that contains our element\n try {\n const parentDocument = element.ownerDocument.defaultView.parent.document;\n const iframes = parentDocument.querySelectorAll('iframe');\n \n for (const iframe of iframes) {\n if (iframe.contentWindow === element.ownerDocument.defaultView) {\n return iframe;\n }\n }\n } catch (e) {\n // Cross-origin restriction\n return \"Cross-origin iframe - cannot access details\";\n }\n \n return null;\n }\n\n function getElementInfo(element, index) {\n // Get text content with spaces between elements\n /* function getTextContent(element) {\n const walker = document.createTreeWalker(\n element,\n NodeFilter.SHOW_TEXT,\n null,\n false\n );\n\n let text = '';\n let node;\n\n while (node = walker.nextNode()) {\n const trimmedText = node.textContent.trim();\n if (trimmedText) {\n // Add space if there's already text\n if (text) {\n text += ' ';\n }\n text += trimmedText;\n }\n }\n\n return text;\n } */\n\n const xpath = generateXPath(element);\n const css_selector = generateCssPath(element);\n //disabled since it's blocking event handling in recorder\n const short_css_selector = ''; //getRobustSelector(element);\n\n const iframe = getContainingIframe(element); \n const iframe_selector = iframe ? generateCssPath(iframe) : \"\";\n //disabled since it's blocking event handling in recorder\n const short_iframe_selector = ''; //iframe ? getRobustSelector(iframe) : \"\";\n\n // Return element info with pre-calculated values\n return new ElementInfo(element, index, {\n tag: element.tagName.toLowerCase(),\n type: element.type || '',\n text: element.innerText || element.placeholder || '', //getTextContent(element),\n html: cleanHTML(element.outerHTML),\n xpath: xpath,\n css_selector: css_selector,\n bounding_box: element.getBoundingClientRect(),\n iframe_selector: iframe_selector,\n short_css_selector: short_css_selector,\n short_iframe_selector: short_iframe_selector\n });\n }\n\n function getAriaLabelledByText(elementInfo, includeHidden=true) {\n if (!elementInfo.element.hasAttribute('aria-labelledby')) return '';\n\n const ids = elementInfo.element.getAttribute('aria-labelledby').split(/\\s+/);\n let labelText = '';\n\n //locate root (document or iFrame document if element is contained in an iframe)\n let root = document;\n if (elementInfo.iframe_selector) { \n const frames = getAllDocumentElementsIncludingShadow('iframe', document);\n \n // Iterate over all frames and compare their CSS selectors\n for (const frame of frames) {\n const selector = generateCssPath(frame);\n if (selector === elementInfo.iframe_selector) {\n root = frame.contentDocument || frame.contentWindow.document; \n break;\n }\n } \n }\n\n ids.forEach(id => {\n const el = querySelectorShadow(`#${CSS.escape(id)}`, root);\n if (el) {\n if (includeHidden || el.offsetParent !== null || getComputedStyle(el).display !== 'none') {\n labelText += el.textContent.trim() + ' ';\n }\n }\n });\n\n return labelText.trim();\n }\n\n\n\n const filterZeroDimensions = (elementInfo) => {\n const rect = elementInfo.bounding_box;\n //single pixel elements are typically faux controls and should be filtered too\n const hasSize = rect.width > 1 && rect.height > 1;\n const style = window.getComputedStyle(elementInfo.element);\n const isVisible = style.display !== 'none' && style.visibility !== 'hidden';\n \n if (!hasSize || !isVisible) {\n \n return false;\n }\n return true;\n };\n\n\n\n function uniquifyElements(elements) {\n const seen = new Set();\n\n console.log(`Starting uniquification with ${elements.length} elements`);\n\n // Filter out testing infrastructure elements first\n const filteredInfrastructure = elements.filter(element_info => {\n // Skip the highlight-overlay element completely - it's part of the testing infrastructure\n if (element_info.element.id === 'highlight-overlay' || \n (element_info.css_selector && element_info.css_selector.includes('#highlight-overlay'))) {\n console.log('Filtered out testing infrastructure element:', element_info.css_selector);\n return false;\n }\n \n // Filter out UI framework container/manager elements\n const el = element_info.element;\n // UI framework container checks - generic detection for any framework\n if ((el.getAttribute('data-rendered-by') || \n el.getAttribute('data-reactroot') || \n el.getAttribute('ng-version') || \n el.getAttribute('data-component-id') ||\n el.getAttribute('data-root') ||\n el.getAttribute('data-framework')) && \n (el.className && \n typeof el.className === 'string' && \n (el.className.includes('Container') || \n el.className.includes('container') || \n el.className.includes('Manager') || \n el.className.includes('manager')))) {\n console.log('Filtered out UI framework container element:', element_info.css_selector);\n return false;\n }\n \n // Direct filter for framework container elements that shouldn't be interactive\n // Consolidating multiple container detection patterns into one efficient check\n const isFullViewport = element_info.bounding_box && \n element_info.bounding_box.x <= 5 && \n element_info.bounding_box.y <= 5 && \n element_info.bounding_box.width >= (window.innerWidth * 0.95) && \n element_info.bounding_box.height >= (window.innerHeight * 0.95);\n \n // Empty content check\n const isEmpty = !el.innerText || el.innerText.trim() === '';\n \n // Check if it's a framework container element\n if (element_info.element.tagName === 'DIV' && \n isFullViewport && \n isEmpty && \n (\n // Pattern matching for root containers\n (element_info.xpath && \n (element_info.xpath.match(/^\\/html\\[\\d+\\]\\/body\\[\\d+\\]\\/div\\[\\d+\\]\\/div\\[\\d+\\]$/) || \n element_info.xpath.match(/^\\/\\/\\*\\[@id='[^']+'\\]\\/div\\[\\d+\\]$/))) ||\n \n // Simple DOM structure\n (element_info.css_selector.split(' > ').length <= 4 && element_info.depth <= 5) ||\n \n // Empty or container-like classes\n (!el.className || el.className === '' || \n (typeof el.className === 'string' && \n (el.className.includes('overlay') || \n el.className.includes('container') || \n el.className.includes('wrapper'))))\n )) {\n console.log('Filtered out framework container element:', element_info.css_selector);\n return false;\n }\n \n return true;\n });\n\n // First filter out elements with zero dimensions\n const nonZeroElements = filteredInfrastructure.filter(filterZeroDimensions);\n // sort by CSS selector depth so parents are processed first\n nonZeroElements.sort((a, b) => a.getDepth() - b.getDepth());\n console.log(`After dimension filtering: ${nonZeroElements.length} elements remain (${elements.length - nonZeroElements.length} removed)`);\n \n const filteredByParent = nonZeroElements.filter(element_info => {\n\n const parent = findClosestParent(seen, element_info);\n const keep = parent == null || shouldKeepNestedElement(element_info, parent);\n // console.log(\"node \", element_info.index, \": keep=\", keep, \" parent=\", parent);\n // if (!keep && !element_info.xpath) {\n // console.log(\"Filtered out element \", element_info,\" because it's a nested element of \", parent);\n // }\n if (keep)\n seen.add(element_info.css_selector);\n\n return keep;\n });\n\n console.log(`After parent/child filtering: ${filteredByParent.length} elements remain (${nonZeroElements.length - filteredByParent.length} removed)`);\n\n // Final overlap filtering\n const filteredResults = filteredByParent.filter(element => {\n\n // Look for any element that came BEFORE this one in the array\n const hasEarlierOverlap = filteredByParent.some(other => {\n // Only check elements that came before (lower index)\n if (filteredByParent.indexOf(other) >= filteredByParent.indexOf(element)) {\n return false;\n }\n \n const isOverlapping = areElementsOverlapping(element, other); \n return isOverlapping;\n }); \n\n // Keep element if it has no earlier overlapping elements\n return !hasEarlierOverlap;\n });\n \n \n \n // Check for overlay removal\n console.log(`After filtering: ${filteredResults.length} (${filteredByParent.length - filteredResults.length} removed by overlap)`);\n \n const nonOverlaidElements = filteredResults.filter(element => {\n return !isOverlaid(element);\n });\n\n console.log(`Final elements after overlay removal: ${nonOverlaidElements.length} (${filteredResults.length - nonOverlaidElements.length} removed)`);\n \n return nonOverlaidElements;\n\n }\n\n\n\n const areElementsOverlapping = (element1, element2) => {\n if (element1.css_selector === element2.css_selector) {\n return true;\n }\n \n const box1 = element1.bounding_box;\n const box2 = element2.bounding_box;\n \n return box1.x === box2.x &&\n box1.y === box2.y &&\n box1.width === box2.width &&\n box1.height === box2.height;\n // element1.text === element2.text &&\n // element2.tag === 'a';\n };\n\n function findClosestParent(seen, element_info) { \n \n // Split the xpath into segments\n const segments = element_info.css_selector.split(' > ');\n \n // Try increasingly shorter paths until we find one in the seen set\n for (let i = segments.length - 1; i > 0; i--) {\n const parentPath = segments.slice(0, i).join(' > ');\n if (seen.has(parentPath)) {\n return parentPath;\n }\n }\n\n return null;\n }\n\n function shouldKeepNestedElement(elementInfo, parentPath) {\n let result = false;\n const parentSegments = parentPath.split(' > ');\n\n const isParentLink = /^a(:nth-of-type\\(\\d+\\))?$/.test(parentSegments[parentSegments.length - 1]);\n if (isParentLink) {\n return false; \n }\n // If this is a checkbox/radio input\n if (elementInfo.tag === 'input' && \n (elementInfo.type === 'checkbox' || elementInfo.type === 'radio')) {\n \n // Check if parent is a label by looking at the parent xpath's last segment\n \n const isParentLabel = /^label(:nth-of-type\\(\\d+\\))?$/.test(parentSegments[parentSegments.length - 1]);\n \n // If parent is a label, don't keep the input (we'll keep the label instead)\n if (isParentLabel) {\n return false;\n }\n }\n \n // Keep all other form controls and dropdown items\n if (isFormControl(elementInfo) || isDropdownItem(elementInfo)) {\n result = true;\n }\n\n if(isTableCell(elementInfo)) {\n result = true;\n }\n \n \n // console.log(`shouldKeepNestedElement: ${elementInfo.tag} ${elementInfo.text} ${elementInfo.xpath} -> ${parentXPath} -> ${result}`);\n return result;\n }\n\n\n function isTableCell(elementInfo) {\n const element = elementInfo.element;\n if(!element || !(element instanceof HTMLElement)) {\n return false;\n }\n const validTags = new Set(['td', 'th']);\n const validRoles = new Set(['cell', 'gridcell', 'columnheader', 'rowheader']);\n \n const tag = element.tagName.toLowerCase();\n const role = element.getAttribute('role')?.toLowerCase();\n\n if (validTags.has(tag) || (role && validRoles.has(role))) {\n return true;\n }\n return false;\n \n }\n\n function isOverlaid(elementInfo) {\n const element = elementInfo.element;\n const boundingRect = elementInfo.bounding_box;\n \n\n \n \n // Create a diagnostic logging function that only logs when needed\n const diagnosticLog = (...args) => {\n { // set to true for debugging\n console.log('[OVERLAY-DEBUG]', ...args);\n }\n };\n\n // Special handling for tooltips\n if (elementInfo.element.className && typeof elementInfo.element.className === 'string' && \n elementInfo.element.className.includes('tooltip')) {\n diagnosticLog('Element is a tooltip, not considering it overlaid');\n return false;\n }\n \n \n \n // Get element at the center point to check if it's covered by a popup/modal\n const middleX = boundingRect.x + boundingRect.width/2;\n const middleY = boundingRect.y + boundingRect.height/2;\n const elementAtMiddle = element.ownerDocument.elementFromPoint(middleX, middleY);\n \n if (elementAtMiddle && \n elementAtMiddle !== element && \n !isDecendent(element, elementAtMiddle) && \n !isDecendent(elementAtMiddle, element)) {\n\n \n return true;\n }\n \n \n return false;\n \n }\n\n\n\n /**\n * Get the “best” short, unique, and robust CSS selector for an element.\n * \n * @param {Element} element\n * @returns {string} A selector guaranteed to find exactly that element in its context\n */\n function getRobustSelector(element) {\n // 1. Figure out the real “root” (iframe doc, shadow root, or main doc)\n const root = (() => {\n const rootNode = element.getRootNode();\n if (rootNode instanceof ShadowRoot) {\n return rootNode;\n }\n return element.ownerDocument;\n })();\n\n // 2. Options to bias toward stable attrs and away from auto-generated classes\n const options = {\n root,\n // only use data-*, id or aria-label by default\n attr(name, value) {\n if (name === 'id' || name.startsWith('data-') || name === 'aria-label') {\n return true;\n }\n return false;\n },\n // skip framework junk\n filter(name, value) {\n if (name.startsWith('ng-') || name.startsWith('_ngcontent') || /^p-/.test(name)) {\n return false;\n }\n return true;\n },\n // let finder try really short seeds\n seedMinLength: 1,\n optimizedMinLength: 1,\n };\n\n let selector;\n try {\n selector = finder(element, options);\n // 3. Verify it really works in the context\n const found = root.querySelectorAll(selector);\n if (found.length !== 1 || found[0] !== element) {\n throw new Error('not unique or not found');\n }\n return selector;\n } catch (err) {\n // 4. Fallback: full path (you already have this utility)\n console.warn('[getRobustSelector] finder failed, falling back to full path:', err);\n return generateCssPath(element); // you’d import or define this elsewhere\n }\n }\n\n /**\n * Checks if an element is scrollable (has scrollable content)\n * \n * @param element - The element to check\n * @returns boolean indicating if the element is scrollable\n */\n function isScrollableContainer(element) {\n if (!element) return false;\n \n const style = window.getComputedStyle(element);\n \n // Reliable way to detect if an element has scrollbars or is scrollable\n const hasScrollHeight = element.scrollHeight > element.clientHeight;\n const hasScrollWidth = element.scrollWidth > element.clientWidth;\n \n // Check actual style properties\n const hasOverflowY = style.overflowY === 'auto' || \n style.overflowY === 'scroll' || \n style.overflowY === 'overlay';\n const hasOverflowX = style.overflowX === 'auto' || \n style.overflowX === 'scroll' || \n style.overflowX === 'overlay';\n \n // Check common class names and attributes for scrollable containers across frameworks\n const hasScrollClasses = element.classList.contains('scroll') || \n element.classList.contains('scrollable') ||\n element.classList.contains('overflow') ||\n element.classList.contains('overflow-auto') ||\n element.classList.contains('overflow-scroll') ||\n element.getAttribute('data-scrollable') === 'true';\n \n // Check for height/max-height constraints that often indicate scrolling content\n const hasHeightConstraint = style.maxHeight && \n style.maxHeight !== 'none' && \n style.maxHeight !== 'auto';\n \n // An element is scrollable if it has:\n // 1. Actual scrollbars in use (most reliable check) OR\n // 2. Overflow styles allowing scrolling AND content that would require scrolling\n return (hasScrollHeight && hasOverflowY) || \n (hasScrollWidth && hasOverflowX) ||\n (hasScrollClasses && (hasScrollHeight || hasScrollWidth)) ||\n (hasHeightConstraint && hasScrollHeight);\n }\n\n /**\n * Detects scrollable containers that are ancestors of the target element\n * \n * This function traverses up the DOM tree from the target element and identifies\n * all scrollable containers (elements that have scrollable content).\n * \n * @param target - The target element to start the search from\n * @returns Array of objects with selector and scroll properties\n */\n function detectScrollableContainers(target) {\n const scrollableContainers = [];\n \n if (!target) {\n return scrollableContainers;\n }\n \n console.log('🔍 [detectScrollableContainers] Starting detection for target:', target.tagName, target.id, target.className);\n \n // Detect if target is inside an iframe\n const iframe = getContainingIframe(target);\n const iframe_selector = iframe ? generateCssPath(iframe) : \"\";\n \n console.log('🔍 [detectScrollableContainers] Iframe context:', iframe ? 'inside iframe' : 'main document', 'selector:', iframe_selector);\n \n // Start from the target element and traverse up the DOM tree\n let currentElement = target;\n let depth = 0;\n const MAX_DEPTH = 10; // Limit traversal depth to avoid infinite loops\n \n while (currentElement && currentElement.nodeType === Node.ELEMENT_NODE && depth < MAX_DEPTH) { \n // Check if the current element is scrollable\n if (isScrollableContainer(currentElement)) {\n console.log('🔍 [detectScrollableContainers] Found scrollable container at depth', depth, ':', currentElement.tagName, currentElement.id, currentElement.className);\n \n const container = {\n containerEl: currentElement,\n selector: generateCssPath(currentElement),\n iframe_selector: iframe_selector,\n scrollTop: currentElement.scrollTop,\n scrollLeft: currentElement.scrollLeft,\n scrollHeight: currentElement.scrollHeight,\n scrollWidth: currentElement.scrollWidth,\n clientHeight: currentElement.clientHeight,\n clientWidth: currentElement.clientWidth\n };\n \n scrollableContainers.push(container);\n }\n \n // Move to parent element\n currentElement = getParentNode(currentElement);\n \n depth++;\n }\n \n console.log('🔍 [detectScrollableContainers] Detection complete. Found', scrollableContainers.length, 'scrollable containers');\n return scrollableContainers;\n }\n\n class DOMSerializer {\n constructor(options = {}) {\n this.options = {\n includeStyles: true,\n includeScripts: false, // Security consideration\n includeFrames: true,\n includeShadowDOM: true,\n maxDepth: 50,\n ...options\n };\n this.serializedFrames = new Map();\n this.shadowRoots = new Map();\n }\n \n /**\n * Serialize a complete document or element\n */\n serialize(rootElement = document) {\n try {\n const serialized = {\n type: 'document',\n doctype: this.serializeDoctype(rootElement),\n documentElement: this.serializeElement(rootElement.documentElement || rootElement),\n frames: [],\n timestamp: Date.now(),\n url: rootElement.URL || window.location?.href,\n metadata: {\n title: rootElement.title,\n charset: rootElement.characterSet,\n contentType: rootElement.contentType\n }\n };\n \n // Serialize frames and iframes if enabled\n if (this.options.includeFrames) {\n serialized.frames = this.serializeFrames(rootElement);\n }\n \n return serialized;\n } catch (error) {\n console.error('Serialization error:', error);\n throw new Error(`DOM serialization failed: ${error.message}`);\n }\n }\n \n /**\n * Serialize document type declaration\n */\n serializeDoctype(doc) {\n if (!doc.doctype) return null;\n \n return {\n name: doc.doctype.name,\n publicId: doc.doctype.publicId,\n systemId: doc.doctype.systemId\n };\n }\n \n /**\n * Serialize an individual element and its children\n */\n serializeElement(element, depth = 0) {\n if (depth > this.options.maxDepth) {\n return { type: 'text', content: '<!-- Max depth exceeded -->' };\n }\n \n const nodeType = element.nodeType;\n \n switch (nodeType) {\n case Node.ELEMENT_NODE:\n return this.serializeElementNode(element, depth);\n case Node.TEXT_NODE:\n return this.serializeTextNode(element);\n case Node.COMMENT_NODE:\n return this.serializeCommentNode(element);\n case Node.DOCUMENT_FRAGMENT_NODE:\n return this.serializeDocumentFragment(element, depth);\n default:\n return null;\n }\n }\n \n /**\n * Serialize element node with attributes and children\n */\n serializeElementNode(element, depth) {\n const tagName = element.tagName.toLowerCase();\n \n // Skip script tags for security unless explicitly enabled\n if (tagName === 'script' && !this.options.includeScripts) {\n return { type: 'comment', content: '<!-- Script tag removed for security -->' };\n }\n \n const serialized = {\n type: 'element',\n tagName: tagName,\n attributes: this.serializeAttributes(element),\n children: [],\n shadowRoot: null\n };\n \n // Handle Shadow DOM\n if (this.options.includeShadowDOM && element.shadowRoot) {\n serialized.shadowRoot = this.serializeShadowRoot(element.shadowRoot, depth + 1);\n }\n \n // Handle special elements\n if (tagName === 'iframe' || tagName === 'frame') {\n serialized.frameData = this.serializeFrameElement(element);\n }\n \n // Serialize children\n for (const child of element.childNodes) {\n const serializedChild = this.serializeElement(child, depth + 1);\n if (serializedChild) {\n serialized.children.push(serializedChild);\n }\n }\n \n // Include computed styles if enabled\n if (this.options.includeStyles && element.nodeType === Node.ELEMENT_NODE) {\n serialized.computedStyle = this.serializeComputedStyle(element);\n }\n \n return serialized;\n }\n \n /**\n * Serialize element attributes\n */\n serializeAttributes(element) {\n const attributes = {};\n \n if (element.attributes) {\n for (const attr of element.attributes) {\n attributes[attr.name] = attr.value;\n }\n }\n \n return attributes;\n }\n \n /**\n * Serialize computed styles\n */\n serializeComputedStyle(element) {\n try {\n const computedStyle = window.getComputedStyle(element);\n const styles = {};\n \n // Only serialize non-default values to reduce size\n const importantStyles = [\n 'display', 'position', 'width', 'height', 'margin', 'padding',\n 'border', 'background', 'color', 'font-family', 'font-size',\n 'text-align', 'visibility', 'z-index', 'transform'\n ];\n \n for (const prop of importantStyles) {\n const value = computedStyle.getPropertyValue(prop);\n if (value && value !== 'initial' && value !== 'normal') {\n styles[prop] = value;\n }\n }\n \n return styles;\n } catch (error) {\n return {};\n }\n }\n \n /**\n * Serialize text node\n */\n serializeTextNode(node) {\n return {\n type: 'text',\n content: node.textContent\n };\n }\n \n /**\n * Serialize comment node\n */\n serializeCommentNode(node) {\n return {\n type: 'comment',\n content: node.textContent\n };\n }\n \n /**\n * Serialize document fragment\n */\n serializeDocumentFragment(fragment, depth) {\n const serialized = {\n type: 'fragment',\n children: []\n };\n \n for (const child of fragment.childNodes) {\n const serializedChild = this.serializeElement(child, depth + 1);\n if (serializedChild) {\n serialized.children.push(serializedChild);\n }\n }\n \n return serialized;\n }\n \n /**\n * Serialize Shadow DOM\n */\n serializeShadowRoot(shadowRoot, depth) {\n const serialized = {\n type: 'shadowRoot',\n mode: shadowRoot.mode,\n children: []\n };\n \n for (const child of shadowRoot.childNodes) {\n const serializedChild = this.serializeElement(child, depth + 1);\n if (serializedChild) {\n serialized.children.push(serializedChild);\n }\n }\n \n return serialized;\n }\n \n /**\n * Serialize frame/iframe elements\n */\n serializeFrameElement(frameElement) {\n const frameData = {\n src: frameElement.src,\n name: frameElement.name,\n id: frameElement.id,\n sandbox: frameElement.sandbox?.toString() || '',\n allowfullscreen: frameElement.allowFullscreen\n };\n \n // Try to access frame content (may fail due to CORS)\n try {\n const frameDoc = frameElement.contentDocument;\n if (frameDoc && this.options.includeFrames) {\n frameData.content = this.serialize(frameDoc);\n }\n } catch (error) {\n frameData.accessError = 'Cross-origin frame content not accessible';\n }\n \n return frameData;\n }\n \n /**\n * Serialize all frames in document\n */\n serializeFrames(doc) {\n const frames = [];\n const frameElements = doc.querySelectorAll('iframe, frame');\n \n for (const frameElement of frameElements) {\n try {\n const frameDoc = frameElement.contentDocument;\n if (frameDoc) {\n frames.push({\n element: this.serializeElement(frameElement),\n content: this.serialize(frameDoc)\n });\n }\n } catch (error) {\n frames.push({\n element: this.serializeElement(frameElement),\n error: 'Frame content not accessible'\n });\n }\n }\n \n return frames;\n }\n \n /**\n * Deserialize serialized DOM data back to DOM nodes\n */\n deserialize(serializedData, targetDocument = document) {\n try {\n if (serializedData.type === 'document') {\n return this.deserializeDocument(serializedData, targetDocument);\n } else {\n return this.deserializeElement(serializedData, targetDocument);\n }\n } catch (error) {\n console.error('Deserialization error:', error);\n throw new Error(`DOM deserialization failed: ${error.message}`);\n }\n }\n \n /**\n * Deserialize complete document\n */\n deserializeDocument(serializedDoc, targetDoc) {\n // Create new document if needed\n const doc = targetDoc || document.implementation.createHTMLDocument();\n \n // Set doctype if present\n if (serializedDoc.doctype) {\n const doctype = document.implementation.createDocumentType(\n serializedDoc.doctype.name,\n serializedDoc.doctype.publicId,\n serializedDoc.doctype.systemId\n );\n doc.replaceChild(doctype, doc.doctype);\n }\n \n // Deserialize document element\n if (serializedDoc.documentElement) {\n const newDocElement = this.deserializeElement(serializedDoc.documentElement, doc);\n doc.replaceChild(newDocElement, doc.documentElement);\n }\n \n // Handle metadata\n if (serializedDoc.metadata) {\n doc.title = serializedDoc.metadata.title || '';\n }\n \n return doc;\n }\n \n /**\n * Deserialize individual element\n */\n deserializeElement(serializedNode, doc) {\n switch (serializedNode.type) {\n case 'element':\n return this.deserializeElementNode(serializedNode, doc);\n case 'text':\n return doc.createTextNode(serializedNode.content);\n case 'comment':\n return doc.createComment(serializedNode.content);\n case 'fragment':\n return this.deserializeDocumentFragment(serializedNode, doc);\n case 'shadowRoot':\n // Shadow roots are handled during element creation\n return null;\n default:\n return null;\n }\n }\n \n /**\n * Deserialize element node\n */\n deserializeElementNode(serializedElement, doc) {\n const element = doc.createElement(serializedElement.tagName);\n \n // Set attributes\n if (serializedElement.attributes) {\n for (const [name, value] of Object.entries(serializedElement.attributes)) {\n try {\n element.setAttribute(name, value);\n } catch (error) {\n console.warn(`Failed to set attribute ${name}:`, error);\n }\n }\n }\n \n // Apply computed styles if available\n if (serializedElement.computedStyle && this.options.includeStyles) {\n for (const [prop, value] of Object.entries(serializedElement.computedStyle)) {\n try {\n element.style.setProperty(prop, value);\n } catch (error) {\n console.warn(`Failed to set style ${prop}:`, error);\n }\n }\n }\n \n // Create shadow root if present\n if (serializedElement.shadowRoot && element.attachShadow) {\n try {\n const shadowRoot = element.attachShadow({ \n mode: serializedElement.shadowRoot.mode || 'open' \n });\n \n // Deserialize shadow root children\n for (const child of serializedElement.shadowRoot.children) {\n const childElement = this.deserializeElement(child, doc);\n if (childElement) {\n shadowRoot.appendChild(childElement);\n }\n }\n } catch (error) {\n console.warn('Failed to create shadow root:', error);\n }\n }\n \n // Deserialize children\n if (serializedElement.children) {\n for (const child of serializedElement.children) {\n const childElement = this.deserializeElement(child, doc);\n if (childElement) {\n element.appendChild(childElement);\n }\n }\n }\n \n // Handle frame content\n if (serializedElement.frameData && serializedElement.frameData.content) {\n // Frame content deserialization would happen after the frame loads\n element.addEventListener('load', () => {\n try {\n const frameDoc = element.contentDocument;\n if (frameDoc) {\n this.deserializeDocument(serializedElement.frameData.content, frameDoc);\n }\n } catch (error) {\n console.warn('Failed to deserialize frame content:', error);\n }\n });\n }\n \n return element;\n }\n \n /**\n * Deserialize document fragment\n */\n deserializeDocumentFragment(serializedFragment, doc) {\n const fragment = doc.createDocumentFragment();\n \n if (serializedFragment.children) {\n for (const child of serializedFragment.children) {\n const childElement = this.deserializeElement(child, doc);\n if (childElement) {\n fragment.appendChild(childElement);\n }\n }\n }\n \n return fragment;\n }\n }\n \n // Usage example and utility functions\n class DOMUtils {\n /**\n * Create serializer with common presets\n */\n static createSerializer(preset = 'default') {\n const presets = {\n default: {\n includeStyles: true,\n includeScripts: false,\n includeFrames: true,\n includeShadowDOM: true\n },\n minimal: {\n includeStyles: false,\n includeScripts: false,\n includeFrames: false,\n includeShadowDOM: false\n },\n complete: {\n includeStyles: true,\n includeScripts: true,\n includeFrames: true,\n includeShadowDOM: true\n },\n secure: {\n includeStyles: true,\n includeScripts: false,\n includeFrames: false,\n includeShadowDOM: true\n }\n };\n \n return new DOMSerializer(presets[preset] || presets.default);\n }\n \n /**\n * Serialize DOM to JSON string\n */\n static serializeToJSON(element, options) {\n const serializer = new DOMSerializer(options);\n const serialized = serializer.serialize(element);\n return JSON.stringify(serialized, null, 2);\n }\n \n /**\n * Deserialize from JSON string\n */\n static deserializeFromJSON(jsonString, targetDocument) {\n const serialized = JSON.parse(jsonString);\n const serializer = new DOMSerializer();\n return serializer.deserialize(serialized, targetDocument);\n }\n \n /**\n * Clone DOM with full fidelity including Shadow DOM\n */\n static deepClone(element, options) {\n const serializer = new DOMSerializer(options);\n const serialized = serializer.serialize(element);\n return serializer.deserialize(serialized, element.ownerDocument);\n }\n \n /**\n * Compare two DOM structures\n */\n static compare(element1, element2, options) {\n const serializer = new DOMSerializer(options);\n const serialized1 = serializer.serialize(element1);\n const serialized2 = serializer.serialize(element2);\n \n return JSON.stringify(serialized1) === JSON.stringify(serialized2);\n }\n }\n \n /*\n // Export for use\n if (typeof module !== 'undefined' && module.exports) {\n module.exports = { DOMSerializer, DOMUtils };\n } else if (typeof window !== 'undefined') {\n window.DOMSerializer = DOMSerializer;\n window.DOMUtils = DOMUtils;\n }\n */\n\n /* Usage Examples:\n \n // Basic serialization\n const serializer = new DOMSerializer();\n const serialized = serializer.serialize(document);\n console.log(JSON.stringify(serialized, null, 2));\n \n // Deserialize back to DOM\n const clonedDoc = serializer.deserialize(serialized);\n \n // Using presets\n const minimalSerializer = DOMUtils.createSerializer('minimal');\n const secureSerializer = DOMUtils.createSerializer('secure');\n \n // Serialize specific element with Shadow DOM\n const customElement = document.querySelector('my-custom-element');\n const serializedElement = serializer.serialize(customElement);\n \n // JSON utilities\n const jsonString = DOMUtils.serializeToJSON(document.body);\n const restored = DOMUtils.deserializeFromJSON(jsonString);\n \n // Deep clone with Shadow DOM support\n const clone = DOMUtils.deepClone(document.body, { includeShadowDOM: true });\n \n */\n\n function serializeNodeToJSON(nodeElement) {\n return DOMUtils.serializeToJSON(nodeElement, {includeStyles: false});\n }\n\n function deserializeNodeFromJSON(jsonString) {\n return DOMUtils.deserializeFromJSON(jsonString);\n }\n\n /**\n * Checks if a point is inside a bounding box\n * \n * @param point The point to check\n * @param box The bounding box\n * @returns boolean indicating if the point is inside the box\n */\n function isPointInsideBox(point, box) {\n return point.x >= box.x &&\n point.x <= box.x + box.width &&\n point.y >= box.y &&\n point.y <= box.y + box.height;\n }\n\n /**\n * Calculates the overlap area between two bounding boxes\n * \n * @param box1 First bounding box\n * @param box2 Second bounding box\n * @returns The overlap area\n */\n function calculateOverlap(box1, box2) {\n const xOverlap = Math.max(0,\n Math.min(box1.x + box1.width, box2.x + box2.width) -\n Math.max(box1.x, box2.x)\n );\n const yOverlap = Math.max(0,\n Math.min(box1.y + box1.height, box2.y + box2.height) -\n Math.max(box1.y, box2.y)\n );\n return xOverlap * yOverlap;\n }\n\n /**\n * Finds an exact match between candidate elements and the actual interaction element\n * \n * @param candidate_elements Array of candidate element infos\n * @param actualInteractionElementInfo The actual interaction element info\n * @returns The matching candidate element info, or null if no match is found\n */\n function findExactMatch(candidate_elements, actualInteractionElementInfo) {\n if (!actualInteractionElementInfo.element) {\n return null;\n }\n\n const exactMatch = candidate_elements.find(elementInfo => \n elementInfo.element && elementInfo.element === actualInteractionElementInfo.element\n );\n \n if (exactMatch) {\n console.log('✅ Found exact element match:', {\n matchedElement: exactMatch.element?.tagName,\n matchedElementClass: exactMatch.element?.className,\n index: exactMatch.index\n });\n return exactMatch;\n }\n \n return null;\n }\n\n /**\n * Finds a match by traversing up the parent elements\n * \n * @param candidate_elements Array of candidate element infos\n * @param actualInteractionElementInfo The actual interaction element info\n * @returns The matching candidate element info, or null if no match is found\n */\n function findParentMatch(candidate_elements, actualInteractionElementInfo) {\n if (!actualInteractionElementInfo.element) {\n return null;\n }\n\n let element = actualInteractionElementInfo.element;\n while (element.parentElement) {\n element = element.parentElement;\n const parentMatch = candidate_elements.find(candidate => \n candidate.element && candidate.element === element\n );\n \n if (parentMatch) {\n console.log('✅ Found parent element match:', {\n matchedElement: parentMatch.element?.tagName,\n matchedElementClass: parentMatch.element?.className,\n index: parentMatch.index,\n depth: element.tagName\n });\n return parentMatch;\n }\n \n // Stop if we hit another candidate element\n if (candidate_elements.some(candidate => \n candidate.element && candidate.element === element\n )) {\n console.log('⚠️ Stopped parent search - hit another candidate element:', element.tagName);\n break;\n }\n }\n \n return null;\n }\n\n /**\n * Finds a match based on spatial relationships between elements\n * \n * @param candidate_elements Array of candidate element infos\n * @param actualInteractionElementInfo The actual interaction element info\n * @returns The matching candidate element info, or null if no match is found\n */\n function findSpatialMatch(candidate_elements, actualInteractionElementInfo) {\n if (!actualInteractionElementInfo.element || !actualInteractionElementInfo.bounding_box) {\n return null;\n }\n\n const actualBox = actualInteractionElementInfo.bounding_box;\n let bestMatch = null;\n let bestScore = 0;\n\n for (const candidateInfo of candidate_elements) {\n if (!candidateInfo.bounding_box) continue;\n \n const candidateBox = candidateInfo.bounding_box;\n let score = 0;\n\n // Check if actual element is contained within candidate\n if (isPointInsideBox({ x: actualBox.x, y: actualBox.y }, candidateBox) &&\n isPointInsideBox({ x: actualBox.x + actualBox.width, y: actualBox.y + actualBox.height }, candidateBox)) {\n score += 100; // High score for containment\n }\n\n // Calculate overlap area as a factor\n const overlap = calculateOverlap(actualBox, candidateBox);\n score += overlap;\n\n // Consider proximity if no containment\n if (score === 0) {\n const distance = Math.sqrt(\n Math.pow((actualBox.x + actualBox.width/2) - (candidateBox.x + candidateBox.width/2), 2) +\n Math.pow((actualBox.y + actualBox.height/2) - (candidateBox.y + candidateBox.height/2), 2)\n );\n // Convert distance to a score (closer = higher score)\n score = 1000 / (distance + 1);\n }\n\n if (score > bestScore) {\n bestScore = score;\n bestMatch = candidateInfo;\n console.log('📏 New best spatial match:', {\n element: candidateInfo.element?.tagName,\n class: candidateInfo.element?.className,\n index: candidateInfo.index,\n score: score\n });\n }\n }\n\n if (bestMatch) {\n console.log('✅ Final spatial match selected:', {\n element: bestMatch.element?.tagName,\n class: bestMatch.element?.className,\n index: bestMatch.index,\n finalScore: bestScore\n });\n return bestMatch;\n }\n\n return null;\n }\n\n /**\n * Finds a matching candidate element for an actual interaction element\n * \n * @param candidate_elements Array of candidate element infos\n * @param actualInteractionElementInfo The actual interaction element info\n * @returns The matching candidate element info, or null if no match is found\n */\n function findMatchingCandidateElementInfo(candidate_elements, actualInteractionElementInfo) {\n if (!actualInteractionElementInfo.element || !actualInteractionElementInfo.bounding_box) {\n console.error('❌ Missing required properties in actualInteractionElementInfo');\n return null;\n }\n\n console.log('🔍 Starting element matching for:', {\n clickedElement: actualInteractionElementInfo.element.tagName,\n clickedElementClass: actualInteractionElementInfo.element.className,\n totalCandidates: candidate_elements.length\n });\n\n // First try exact element match\n const exactMatch = findExactMatch(candidate_elements, actualInteractionElementInfo);\n if (exactMatch) {\n return exactMatch;\n }\n console.log('❌ No exact element match found, trying parent matching...');\n\n // Try finding closest clickable parent\n const parentMatch = findParentMatch(candidate_elements, actualInteractionElementInfo);\n if (parentMatch) {\n return parentMatch;\n }\n console.log('❌ No parent match found, falling back to spatial matching...');\n\n // If no exact or parent match, look for spatial relationships\n const spatialMatch = findSpatialMatch(candidate_elements, actualInteractionElementInfo);\n if (spatialMatch) {\n return spatialMatch;\n }\n\n console.error('❌ No matching element found for actual interaction element:', actualInteractionElementInfo);\n return null;\n }\n\n const highlight = {\n execute: async function(elementTypes, handleScroll=false) {\n const elements = await findElements(elementTypes);\n highlightElements(elements, handleScroll);\n return elements;\n },\n\n unexecute: function(handleScroll=false) {\n unhighlightElements(handleScroll);\n },\n\n generateJSON: async function() {\n const json = {};\n\n // Capture viewport dimensions\n const viewportData = {\n width: window.innerWidth,\n height: window.innerHeight,\n documentWidth: document.documentElement.clientWidth,\n documentHeight: document.documentElement.clientHeight,\n timestamp: new Date().toISOString()\n };\n\n // Add viewport data to the JSON output\n json.viewport = viewportData;\n\n\n await Promise.all(Object.values(ElementTag).map(async elementType => {\n const elements = await findElements(elementType);\n json[elementType] = elements;\n }));\n\n // Serialize the JSON object\n const jsonString = JSON.stringify(json, null, 4); // Pretty print with 4 spaces\n\n console.log(`JSON: ${jsonString}`);\n return jsonString;\n },\n\n getElementInfo\n };\n\n\n function unhighlightElements(handleScroll=false) {\n const documents = getAllFrames();\n documents.forEach(doc => {\n const overlay = doc.getElementById('highlight-overlay');\n if (overlay) {\n if (handleScroll) {\n // Remove event listeners\n doc.removeEventListener('scroll', overlay.scrollHandler, true);\n doc.removeEventListener('resize', overlay.resizeHandler);\n }\n overlay.remove();\n }\n });\n }\n\n\n\n\n async function findElements(elementTypes, verbose=true) {\n const typesArray = Array.isArray(elementTypes) ? elementTypes : [elementTypes];\n console.log('Starting element search for types:', typesArray);\n\n const elements = [];\n typesArray.forEach(elementType => {\n if (elementType === ElementTag.FILLABLE) {\n elements.push(...findFillables());\n }\n if (elementType === ElementTag.SELECTABLE) {\n elements.push(...findDropdowns());\n }\n if (elementType === ElementTag.CLICKABLE) {\n elements.push(...findClickables());\n elements.push(...findToggles());\n elements.push(...findCheckables());\n }\n if (elementType === ElementTag.NON_INTERACTIVE_ELEMENT) {\n elements.push(...findNonInteractiveElements());\n }\n });\n\n // console.log('Before uniquify:', elements.length);\n const elementsWithInfo = elements.map((element, index) => \n getElementInfo(element, index)\n );\n\n \n \n const uniqueElements = uniquifyElements(elementsWithInfo);\n console.log(`Found ${uniqueElements.length} elements:`);\n \n // More comprehensive visibility check\n const visibleElements = uniqueElements.filter(elementInfo => {\n const el = elementInfo.element;\n const style = getComputedStyle(el);\n \n // Check various style properties that affect visibility\n if (style.display === 'none' || \n style.visibility === 'hidden') {\n return false;\n }\n \n // Check if element has non-zero dimensions\n const rect = el.getBoundingClientRect();\n if (rect.width === 0 || rect.height === 0) {\n return false;\n }\n \n // Check if element is within viewport\n if (rect.bottom < 0 || \n rect.top > window.innerHeight || \n rect.right < 0 || \n rect.left > window.innerWidth) {\n // Element is outside viewport, but still might be valid \n // if user scrolls to it, so we'll include it\n return true;\n }\n \n return true;\n });\n \n console.log(`Out of which ${visibleElements.length} elements are visible:`);\n if (verbose) {\n visibleElements.forEach(info => {\n console.log(`Element ${info.index}:`, info);\n });\n }\n \n return visibleElements;\n }\n\n // elements is an array of objects with index, xpath\n function highlightElements(elements, handleScroll=false) {\n // console.log('[highlightElements] called with', elements.length, 'elements');\n // Create overlay if it doesn't exist and store it in a dictionary\n const documents = getAllFrames(); \n let overlays = {};\n documents.forEach(doc => {\n let overlay = doc.getElementById('highlight-overlay');\n if (!overlay) {\n overlay = doc.createElement('div');\n overlay.id = 'highlight-overlay';\n overlay.style.cssText = `\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n pointer-events: none;\n z-index: 2147483647;\n `;\n doc.body.appendChild(overlay);\n // console.log('[highlightElements] Created overlay in document:', doc);\n }\n overlays[doc.documentURI] = overlay;\n });\n \n\n const updateHighlights = (doc = null) => {\n if (doc) {\n overlays[doc.documentURI].innerHTML = '';\n } else {\n Object.values(overlays).forEach(overlay => { overlay.innerHTML = ''; });\n } \n elements.forEach((elementInfo, idx) => {\n //console.log(`[highlightElements] Processing element ${idx}:`, elementInfo.tag, elementInfo.css_selector, elementInfo.bounding_box);\n let element = elementInfo.element; //getElementByXPathOrCssSelector(elementInfo);\n if (!element) {\n element = getElementByXPathOrCssSelector(elementInfo);\n if (!element) {\n console.warn('[highlightElements] Could not find element for:', elementInfo);\n return;\n }\n }\n //if highlights requested for a specific doc, skip unrelated elements\n if (doc && element.ownerDocument !== doc) {\n console.log(\"[highlightElements] Skipped element since it doesn't belong to document\", doc);\n return;\n }\n const rect = element.getBoundingClientRect();\n if (rect.width === 0 || rect.height === 0) {\n console.warn('[highlightElements] Element has zero dimensions:', elementInfo);\n return;\n }\n // Create border highlight (red rectangle)\n // use ownerDocument to support iframes/frames\n const highlight = element.ownerDocument.createElement('div');\n highlight.style.cssText = `\n position: fixed;\n left: ${rect.x}px;\n top: ${rect.y}px;\n width: ${rect.width}px;\n height: ${rect.height}px;\n border: 1px solid rgb(255, 0, 0);\n transition: all 0.2s ease-in-out;\n `;\n // Create index label container - now positioned to the right and slightly up\n const labelContainer = element.ownerDocument.createElement('div');\n labelContainer.style.cssText = `\n position: absolute;\n right: -10px; /* Offset to the right */\n top: -10px; /* Offset upwards */\n padding: 4px;\n background-color: rgba(255, 255, 0, 0.6);\n display: flex;\n align-items: center;\n justify-content: center;\n `;\n const text = element.ownerDocument.createElement('span');\n text.style.cssText = `\n color: rgb(0, 0, 0, 0.8);\n font-family: 'Courier New', Courier, monospace;\n font-size: 12px;\n font-weight: bold;\n line-height: 1;\n `;\n text.textContent = elementInfo.index;\n labelContainer.appendChild(text);\n highlight.appendChild(labelContainer); \n overlays[element.ownerDocument.documentURI].appendChild(highlight);\n \n });\n };\n\n // Initial highlight\n updateHighlights();\n\n if (handleScroll) {\n documents.forEach(doc => {\n // Update highlights on scroll and resize\n console.log('registering scroll and resize handlers for document: ', doc);\n const scrollHandler = () => {\n requestAnimationFrame(() => updateHighlights(doc));\n };\n const resizeHandler = () => {\n updateHighlights(doc);\n };\n doc.addEventListener('scroll', scrollHandler, true);\n doc.addEventListener('resize', resizeHandler);\n // Store event handlers for cleanup\n overlays[doc.documentURI].scrollHandler = scrollHandler;\n overlays[doc.documentURI].resizeHandler = resizeHandler;\n }); \n }\n }\n\n // function unexecute() {\n // unhighlightElements();\n // }\n\n // Make it available globally for both Extension and Playwright\n if (typeof window !== 'undefined') {\n function stripElementRefs(elementInfo) {\n if (!elementInfo) return null;\n const { element, ...rest } = elementInfo;\n return rest;\n }\n\n window.ProboLabs = window.ProboLabs || {};\n\n // --- Caching State ---\n window.ProboLabs.candidates = [];\n window.ProboLabs.actual = null;\n window.ProboLabs.matchingCandidate = null;\n\n // --- Methods ---\n /**\n * Find and cache candidate elements of a given type (e.g., 'CLICKABLE').\n * NOTE: This function is async and must be awaited from Playwright/Node.\n */\n window.ProboLabs.findAndCacheCandidateElements = async function(elementType) {\n //console.log('[ProboLabs] findAndCacheCandidateElements called with:', elementType);\n const found = await findElements(elementType);\n window.ProboLabs.candidates = found;\n // console.log('[ProboLabs] candidates set to:', found, 'type:', typeof found, 'isArray:', Array.isArray(found));\n return found.length;\n };\n\n window.ProboLabs.findAndCacheActualElement = function(cssSelector, iframeSelector, isHover=false) {\n // console.log('[ProboLabs] findAndCacheActualElement called with:', cssSelector, iframeSelector);\n let el = findElement(document, iframeSelector, cssSelector);\n if(isHover) {\n const visibleElement = findClosestVisibleElement(el);\n if (visibleElement) {\n el = visibleElement;\n }\n }\n if (!el) {\n window.ProboLabs.actual = null;\n // console.log('[ProboLabs] actual set to null');\n return false;\n }\n window.ProboLabs.actual = getElementInfo(el, -1);\n // console.log('[ProboLabs] actual set to:', window.ProboLabs.actual);\n return true;\n };\n\n window.ProboLabs.findAndCacheMatchingCandidate = function() {\n // console.log('[ProboLabs] findAndCacheMatchingCandidate called');\n if (!window.ProboLabs.candidates.length || !window.ProboLabs.actual) {\n window.ProboLabs.matchingCandidate = null;\n // console.log('[ProboLabs] matchingCandidate set to null');\n return false;\n }\n window.ProboLabs.matchingCandidate = findMatchingCandidateElementInfo(window.ProboLabs.candidates, window.ProboLabs.actual);\n // console.log('[ProboLabs] matchingCandidate set to:', window.ProboLabs.matchingCandidate);\n return !!window.ProboLabs.matchingCandidate;\n };\n\n window.ProboLabs.highlightCachedElements = function(which) {\n let elements = [];\n if (which === 'candidates') elements = window.ProboLabs.candidates;\n if (which === 'actual' && window.ProboLabs.actual) elements = [window.ProboLabs.actual];\n if (which === 'matching' && window.ProboLabs.matchingCandidate) elements = [window.ProboLabs.matchingCandidate];\n console.log(`[ProboLabs] highlightCachedElements ${which} with ${elements.length} elements`);\n highlightElements(elements);\n };\n\n window.ProboLabs.unhighlight = function() {\n // console.log('[ProboLabs] unhighlight called');\n unhighlightElements();\n };\n\n window.ProboLabs.reset = function() {\n console.log('[ProboLabs] reset called');\n window.ProboLabs.candidates = [];\n window.ProboLabs.actual = null;\n window.ProboLabs.matchingCandidate = null;\n unhighlightElements();\n };\n\n window.ProboLabs.getCandidates = function() {\n // console.log('[ProboLabs] getCandidates called. candidates:', window.ProboLabs.candidates, 'type:', typeof window.ProboLabs.candidates, 'isArray:', Array.isArray(window.ProboLabs.candidates));\n const arr = Array.isArray(window.ProboLabs.candidates) ? window.ProboLabs.candidates : [];\n return arr.map(stripElementRefs);\n };\n window.ProboLabs.getActual = function() {\n return stripElementRefs(window.ProboLabs.actual);\n };\n window.ProboLabs.getMatchingCandidate = function() {\n return stripElementRefs(window.ProboLabs.matchingCandidate);\n };\n\n // Retain existing API for backward compatibility\n window.ProboLabs.ElementTag = ElementTag;\n window.ProboLabs.highlightElements = highlightElements;\n window.ProboLabs.unhighlightElements = unhighlightElements;\n window.ProboLabs.findElements = findElements;\n window.ProboLabs.getElementInfo = getElementInfo;\n window.ProboLabs.highlight = window.ProboLabs.highlight;\n window.ProboLabs.unhighlight = window.ProboLabs.unhighlight;\n\n // --- Utility Functions ---\n function findClosestVisibleElement(element) {\n let current = element;\n while (current) {\n const style = window.getComputedStyle(current);\n if (\n style &&\n style.display !== 'none' &&\n style.visibility !== 'hidden' &&\n current.offsetWidth > 0 &&\n current.offsetHeight > 0\n ) {\n return current;\n }\n if (!current.parentElement || current === document.body) break;\n current = current.parentElement;\n }\n return null;\n }\n }\n\n exports.ElementInfo = ElementInfo;\n exports.ElementTag = ElementTag;\n exports.deserializeNodeFromJSON = deserializeNodeFromJSON;\n exports.detectScrollableContainers = detectScrollableContainers;\n exports.findElement = findElement;\n exports.findElements = findElements;\n exports.generateCssPath = generateCssPath;\n exports.getAriaLabelledByText = getAriaLabelledByText;\n exports.getContainingIframe = getContainingIframe;\n exports.getElementInfo = getElementInfo;\n exports.getParentNode = getParentNode;\n exports.getRobustSelector = getRobustSelector;\n exports.highlight = highlight;\n exports.highlightElements = highlightElements;\n exports.isScrollableContainer = isScrollableContainer;\n exports.serializeNodeToJSON = serializeNodeToJSON;\n exports.unhighlightElements = unhighlightElements;\n\n}));\n//# sourceMappingURL=probolabs.umd.js.map\n";
|
|
2
|
+
import { test as test$1, chromium } from '@playwright/test';
|
|
3
|
+
export { expect } from '@playwright/test';
|
|
4
|
+
|
|
2
5
|
var ApplyAIStatus;
|
|
3
6
|
(function (ApplyAIStatus) {
|
|
4
7
|
ApplyAIStatus["PREPARE_START"] = "PREPARE_START";
|
|
@@ -20,26 +23,6 @@ var ReplayStatus;
|
|
|
20
23
|
ReplayStatus["REPLAY_CANCELLED"] = "REPLAY_CANCELLED";
|
|
21
24
|
})(ReplayStatus || (ReplayStatus = {}));
|
|
22
25
|
|
|
23
|
-
// WebSocketsMessageType enum for WebSocket and event message types shared across the app
|
|
24
|
-
var WebSocketsMessageType;
|
|
25
|
-
(function (WebSocketsMessageType) {
|
|
26
|
-
WebSocketsMessageType["INTERACTION_APPLY_AI_PREPARE_START"] = "INTERACTION_APPLY_AI_PREPARE_START";
|
|
27
|
-
WebSocketsMessageType["INTERACTION_APPLY_AI_PREPARE_SUCCESS"] = "INTERACTION_APPLY_AI_PREPARE_SUCCESS";
|
|
28
|
-
WebSocketsMessageType["INTERACTION_APPLY_AI_PREPARE_ERROR"] = "INTERACTION_APPLY_AI_PREPARE_ERROR";
|
|
29
|
-
WebSocketsMessageType["INTERACTION_APPLY_AI_SEND_TO_LLM_START"] = "INTERACTION_APPLY_AI_SEND_TO_LLM_START";
|
|
30
|
-
WebSocketsMessageType["INTERACTION_APPLY_AI_SEND_TO_LLM_SUCCESS"] = "INTERACTION_APPLY_AI_SEND_TO_LLM_SUCCESS";
|
|
31
|
-
WebSocketsMessageType["INTERACTION_APPLY_AI_SEND_TO_LLM_ERROR"] = "INTERACTION_APPLY_AI_SEND_TO_LLM_ERROR";
|
|
32
|
-
WebSocketsMessageType["INTERACTION_REPLAY_START"] = "INTERACTION_REPLAY_START";
|
|
33
|
-
WebSocketsMessageType["INTERACTION_REPLAY_SUCCESS"] = "INTERACTION_REPLAY_SUCCESS";
|
|
34
|
-
WebSocketsMessageType["INTERACTION_REPLAY_ERROR"] = "INTERACTION_REPLAY_ERROR";
|
|
35
|
-
WebSocketsMessageType["INTERACTION_REPLAY_CANCELLED"] = "INTERACTION_REPLAY_CANCELLED";
|
|
36
|
-
WebSocketsMessageType["INTERACTION_APPLY_AI_CANCELLED"] = "INTERACTION_APPLY_AI_CANCELLED";
|
|
37
|
-
WebSocketsMessageType["INTERACTION_APPLY_AI_ERROR"] = "INTERACTION_APPLY_AI_ERROR";
|
|
38
|
-
WebSocketsMessageType["INTERACTION_STEP_CREATED"] = "INTERACTION_STEP_CREATED";
|
|
39
|
-
WebSocketsMessageType["INTERACTION_APPLY_AI_SUMMARY_COMPLETED"] = "INTERACTION_APPLY_AI_SUMMARY_COMPLETED";
|
|
40
|
-
WebSocketsMessageType["INTERACTION_APPLY_AI_SUMMARY_ERROR"] = "INTERACTION_APPLY_AI_SUMMARY_ERROR";
|
|
41
|
-
})(WebSocketsMessageType || (WebSocketsMessageType = {}));
|
|
42
|
-
|
|
43
26
|
// Action constants
|
|
44
27
|
var PlaywrightAction;
|
|
45
28
|
(function (PlaywrightAction) {
|
|
@@ -57,14 +40,34 @@ var PlaywrightAction;
|
|
|
57
40
|
PlaywrightAction["VALIDATE_EXACT_VALUE"] = "VALIDATE_EXACT_VALUE";
|
|
58
41
|
PlaywrightAction["VALIDATE_CONTAINS_VALUE"] = "VALIDATE_CONTAINS_VALUE";
|
|
59
42
|
PlaywrightAction["VALIDATE_URL"] = "VALIDATE_URL";
|
|
60
|
-
PlaywrightAction["VALIDATE"] = "VALIDATE";
|
|
61
43
|
PlaywrightAction["SCROLL_TO_ELEMENT"] = "SCROLL_TO_ELEMENT";
|
|
62
44
|
PlaywrightAction["EXTRACT_VALUE"] = "EXTRACT_VALUE";
|
|
63
45
|
PlaywrightAction["ASK_AI"] = "ASK_AI";
|
|
64
46
|
PlaywrightAction["EXECUTE_SCRIPT"] = "EXECUTE_SCRIPT";
|
|
65
47
|
PlaywrightAction["UPLOAD_FILES"] = "UPLOAD_FILES";
|
|
48
|
+
PlaywrightAction["WAIT_FOR"] = "WAIT_FOR";
|
|
66
49
|
})(PlaywrightAction || (PlaywrightAction = {}));
|
|
67
50
|
|
|
51
|
+
// WebSocketsMessageType enum for WebSocket and event message types shared across the app
|
|
52
|
+
var WebSocketsMessageType;
|
|
53
|
+
(function (WebSocketsMessageType) {
|
|
54
|
+
WebSocketsMessageType["INTERACTION_APPLY_AI_PREPARE_START"] = "INTERACTION_APPLY_AI_PREPARE_START";
|
|
55
|
+
WebSocketsMessageType["INTERACTION_APPLY_AI_PREPARE_SUCCESS"] = "INTERACTION_APPLY_AI_PREPARE_SUCCESS";
|
|
56
|
+
WebSocketsMessageType["INTERACTION_APPLY_AI_PREPARE_ERROR"] = "INTERACTION_APPLY_AI_PREPARE_ERROR";
|
|
57
|
+
WebSocketsMessageType["INTERACTION_APPLY_AI_SEND_TO_LLM_START"] = "INTERACTION_APPLY_AI_SEND_TO_LLM_START";
|
|
58
|
+
WebSocketsMessageType["INTERACTION_APPLY_AI_SEND_TO_LLM_SUCCESS"] = "INTERACTION_APPLY_AI_SEND_TO_LLM_SUCCESS";
|
|
59
|
+
WebSocketsMessageType["INTERACTION_APPLY_AI_SEND_TO_LLM_ERROR"] = "INTERACTION_APPLY_AI_SEND_TO_LLM_ERROR";
|
|
60
|
+
WebSocketsMessageType["INTERACTION_REPLAY_START"] = "INTERACTION_REPLAY_START";
|
|
61
|
+
WebSocketsMessageType["INTERACTION_REPLAY_SUCCESS"] = "INTERACTION_REPLAY_SUCCESS";
|
|
62
|
+
WebSocketsMessageType["INTERACTION_REPLAY_ERROR"] = "INTERACTION_REPLAY_ERROR";
|
|
63
|
+
WebSocketsMessageType["INTERACTION_REPLAY_CANCELLED"] = "INTERACTION_REPLAY_CANCELLED";
|
|
64
|
+
WebSocketsMessageType["INTERACTION_APPLY_AI_CANCELLED"] = "INTERACTION_APPLY_AI_CANCELLED";
|
|
65
|
+
WebSocketsMessageType["INTERACTION_APPLY_AI_ERROR"] = "INTERACTION_APPLY_AI_ERROR";
|
|
66
|
+
WebSocketsMessageType["INTERACTION_STEP_CREATED"] = "INTERACTION_STEP_CREATED";
|
|
67
|
+
WebSocketsMessageType["INTERACTION_APPLY_AI_SUMMARY_COMPLETED"] = "INTERACTION_APPLY_AI_SUMMARY_COMPLETED";
|
|
68
|
+
WebSocketsMessageType["INTERACTION_APPLY_AI_SUMMARY_ERROR"] = "INTERACTION_APPLY_AI_SUMMARY_ERROR";
|
|
69
|
+
})(WebSocketsMessageType || (WebSocketsMessageType = {}));
|
|
70
|
+
|
|
68
71
|
/**
|
|
69
72
|
* Logging levels for Probo
|
|
70
73
|
*/
|
|
@@ -206,6 +209,44 @@ function cleanupInstructionElements(instruction) {
|
|
|
206
209
|
elementLogger.debug('Instruction cleaning completed');
|
|
207
210
|
return cleaned;
|
|
208
211
|
}
|
|
212
|
+
function matchRegex(str, regex) {
|
|
213
|
+
//parse the regex string
|
|
214
|
+
const match = regex.match(/^\/(.+)\/([gimsuy]*)$/);
|
|
215
|
+
if (!match) // normal string
|
|
216
|
+
return str.includes(regex);
|
|
217
|
+
else { // regex string
|
|
218
|
+
const pattern = match[1].replace(/\\/g, '\\');
|
|
219
|
+
const flags = match[2];
|
|
220
|
+
console.log(`Matching ${str} against ${pattern} with flags ${flags}`);
|
|
221
|
+
return new RegExp(pattern, flags).test(str);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Sets up browser console logging on a Playwright page.
|
|
226
|
+
* This function removes all existing console listeners and optionally adds a new one.
|
|
227
|
+
* @param page The Playwright page instance
|
|
228
|
+
* @param enableConsoleLogs Whether to enable console logging
|
|
229
|
+
* @param logger Optional logger instance to use for output (defaults to console.log)
|
|
230
|
+
*/
|
|
231
|
+
const setupBrowserConsoleLogs = (page, enableConsoleLogs, logger = null) => {
|
|
232
|
+
// Always remove all existing console listeners first
|
|
233
|
+
page.removeAllListeners('console');
|
|
234
|
+
// Always add a listener, but filter output based on enableConsoleLogs
|
|
235
|
+
const listener = (msg) => {
|
|
236
|
+
if (enableConsoleLogs) {
|
|
237
|
+
const type = msg.type();
|
|
238
|
+
const text = msg.text();
|
|
239
|
+
if (logger) {
|
|
240
|
+
logger.log(`[Browser-${type}]: ${text}`);
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
console.log(`[Browser-${type}]: ${text}`);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// If disabled, do nothing (silently ignore the console message)
|
|
247
|
+
};
|
|
248
|
+
page.on('console', listener);
|
|
249
|
+
};
|
|
209
250
|
|
|
210
251
|
function getDefaultExportFromCjs (x) {
|
|
211
252
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
@@ -705,6 +746,7 @@ class ApiClient {
|
|
|
705
746
|
initial_screenshot: options.initial_screenshot_url,
|
|
706
747
|
initial_html_content: options.initial_html_content,
|
|
707
748
|
use_cache: options.use_cache,
|
|
749
|
+
url: options.url
|
|
708
750
|
}),
|
|
709
751
|
});
|
|
710
752
|
const data = await this.handleResponse(response);
|
|
@@ -778,7 +820,7 @@ class ApiClient {
|
|
|
778
820
|
}
|
|
779
821
|
});
|
|
780
822
|
}
|
|
781
|
-
async findStepByPrompt(prompt, scenarioName) {
|
|
823
|
+
async findStepByPrompt(prompt, scenarioName, url = '') {
|
|
782
824
|
apiLogger.debug(`Finding step by prompt: ${prompt} and scenario: ${scenarioName}`);
|
|
783
825
|
return pRetry(async () => {
|
|
784
826
|
const response = await fetch(`${this.apiUrl}/step-runners/find-step-by-prompt/`, {
|
|
@@ -786,7 +828,8 @@ class ApiClient {
|
|
|
786
828
|
headers: this.getHeaders(),
|
|
787
829
|
body: JSON.stringify({
|
|
788
830
|
prompt: prompt,
|
|
789
|
-
scenario_name: scenarioName
|
|
831
|
+
scenario_name: scenarioName,
|
|
832
|
+
url: url
|
|
790
833
|
}),
|
|
791
834
|
});
|
|
792
835
|
try {
|
|
@@ -935,6 +978,13 @@ const DEFAULT_PLAYWRIGHT_TIMEOUT_CONFIG = {
|
|
|
935
978
|
playwrightActionTimeout: 5000,
|
|
936
979
|
playwrightNavigationTimeout: 10000,
|
|
937
980
|
playwrightLocatorTimeout: 15000,
|
|
981
|
+
// mutation observer
|
|
982
|
+
mutationsTimeout: 500,
|
|
983
|
+
mutationsInitTimeout: 1000,
|
|
984
|
+
// wait for navigation
|
|
985
|
+
waitForNavigationInitialTimeout: 2000,
|
|
986
|
+
waitForNavigationNavigationTimeout: 7000,
|
|
987
|
+
waitForNavigationGlobalTimeout: 15000,
|
|
938
988
|
};
|
|
939
989
|
|
|
940
990
|
({
|
|
@@ -946,10 +996,16 @@ const DEFAULT_PLAYWRIGHT_TIMEOUT_CONFIG = {
|
|
|
946
996
|
scenarioName: 'new recording',
|
|
947
997
|
scenarioId: undefined,
|
|
948
998
|
aiModel: 'azure-gpt4-mini',
|
|
999
|
+
activeParamSet: 0,
|
|
949
1000
|
// Browser Configuration
|
|
950
1001
|
resetBrowserBeforeReplay: true,
|
|
1002
|
+
// Script Configuration
|
|
1003
|
+
scriptTimeout: 30000,
|
|
951
1004
|
// UI Configuration
|
|
952
1005
|
hover_enabled: false,
|
|
1006
|
+
// Logging Configuration
|
|
1007
|
+
enableConsoleLogs: false,
|
|
1008
|
+
debugLevel: 'INFO',
|
|
953
1009
|
// Timeout Configuration (spread from PlaywrightTimeoutConfig)
|
|
954
1010
|
...DEFAULT_PLAYWRIGHT_TIMEOUT_CONFIG,
|
|
955
1011
|
});
|
|
@@ -957,130 +1013,10 @@ const DEFAULT_PLAYWRIGHT_TIMEOUT_CONFIG = {
|
|
|
957
1013
|
// Default logger instance
|
|
958
1014
|
const proboLogger = new ProboLogger('probolib');
|
|
959
1015
|
|
|
960
|
-
const SPECIAL_KEYS = [
|
|
961
|
-
'Enter', 'Tab', 'Escape', 'Backspace', 'Delete',
|
|
962
|
-
'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown',
|
|
963
|
-
'Home', 'End', 'PageUp', 'PageDown',
|
|
964
|
-
'Insert', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12',
|
|
965
|
-
'Shift', 'Control', 'Alt', 'Meta',
|
|
966
|
-
'CapsLock', 'NumLock', 'ScrollLock',
|
|
967
|
-
'Pause', 'PrintScreen', 'ContextMenu',
|
|
968
|
-
'AudioVolumeUp', 'AudioVolumeDown', 'AudioVolumeMute',
|
|
969
|
-
'MediaTrackNext', 'MediaTrackPrevious', 'MediaStop', 'MediaPlayPause',
|
|
970
|
-
'BrowserBack', 'BrowserForward', 'BrowserRefresh', 'BrowserFavorites'
|
|
971
|
-
];
|
|
972
|
-
/**
|
|
973
|
-
* Handle potential navigation exactly like Python's handle_potential_navigation
|
|
974
|
-
*/
|
|
975
|
-
async function handlePotentialNavigation(page, locator = null, value = null, options = {}) {
|
|
976
|
-
const { initialTimeout = 5000, navigationTimeout = 7000, globalTimeout = 15000, } = options;
|
|
977
|
-
const startTime = Date.now();
|
|
978
|
-
let navigationCount = 0;
|
|
979
|
-
let lastNavTime = null;
|
|
980
|
-
const onFrameNav = (frame) => {
|
|
981
|
-
if (frame === page.mainFrame()) {
|
|
982
|
-
navigationCount++;
|
|
983
|
-
lastNavTime = Date.now();
|
|
984
|
-
proboLogger.debug(`DEBUG_NAV[${((Date.now() - startTime) / 1000).toFixed(3)}s]: navigation detected (count=${navigationCount})`);
|
|
985
|
-
}
|
|
986
|
-
};
|
|
987
|
-
proboLogger.debug(`DEBUG_NAV[0.000s]: Starting navigation detection`);
|
|
988
|
-
page.on("framenavigated", onFrameNav);
|
|
989
|
-
try {
|
|
990
|
-
if (locator) {
|
|
991
|
-
proboLogger.debug(`DEBUG_NAV: Executing click(${value})`);
|
|
992
|
-
await clickAtPosition(page, locator, value);
|
|
993
|
-
}
|
|
994
|
-
// wait for any initial nav to fire
|
|
995
|
-
await page.waitForTimeout(initialTimeout);
|
|
996
|
-
proboLogger.debug(`DEBUG_NAV[${((Date.now() - startTime) / 1000).toFixed(3)}s]: After initial wait, count=${navigationCount}`);
|
|
997
|
-
if (navigationCount > 0) {
|
|
998
|
-
// loop until either per-nav or global timeout
|
|
999
|
-
while (true) {
|
|
1000
|
-
const now = Date.now();
|
|
1001
|
-
if (lastNavTime !== null &&
|
|
1002
|
-
now - lastNavTime > navigationTimeout) {
|
|
1003
|
-
proboLogger.debug(`DEBUG_NAV[${((now - startTime) / 1000).toFixed(3)}s]: per‐navigation timeout reached`);
|
|
1004
|
-
break;
|
|
1005
|
-
}
|
|
1006
|
-
if (now - startTime > globalTimeout) {
|
|
1007
|
-
proboLogger.debug(`DEBUG_NAV[${((now - startTime) / 1000).toFixed(3)}s]: overall timeout reached`);
|
|
1008
|
-
break;
|
|
1009
|
-
}
|
|
1010
|
-
await page.waitForTimeout(500);
|
|
1011
|
-
}
|
|
1012
|
-
// now wait for load + idle
|
|
1013
|
-
proboLogger.debug(`DEBUG_NAV: waiting for load state`);
|
|
1014
|
-
await page.waitForLoadState("load", { timeout: globalTimeout });
|
|
1015
|
-
proboLogger.debug(`DEBUG_NAV: waiting for networkidle`);
|
|
1016
|
-
try {
|
|
1017
|
-
// shorter idle‐wait so we don't hang the full globalTimeout here
|
|
1018
|
-
await page.waitForLoadState("networkidle", {
|
|
1019
|
-
timeout: navigationTimeout,
|
|
1020
|
-
});
|
|
1021
|
-
}
|
|
1022
|
-
catch (_a) {
|
|
1023
|
-
proboLogger.debug(`DEBUG_NAV: networkidle not reached in ${navigationTimeout}ms, proceeding anyway`);
|
|
1024
|
-
}
|
|
1025
|
-
await scrollToBottomRight(page);
|
|
1026
|
-
proboLogger.debug(`DEBUG_NAV: done`);
|
|
1027
|
-
return true;
|
|
1028
|
-
}
|
|
1029
|
-
proboLogger.debug(`DEBUG_NAV: no navigation detected`);
|
|
1030
|
-
return false;
|
|
1031
|
-
}
|
|
1032
|
-
finally {
|
|
1033
|
-
page.removeListener("framenavigated", onFrameNav);
|
|
1034
|
-
proboLogger.debug(`DEBUG_NAV: listener removed`);
|
|
1035
|
-
}
|
|
1036
|
-
}
|
|
1037
|
-
/**
|
|
1038
|
-
* Scroll entire page to bottom-right, triggering lazy-loaded content
|
|
1039
|
-
*/
|
|
1040
|
-
async function scrollToBottomRight(page) {
|
|
1041
|
-
const startTime = performance.now();
|
|
1042
|
-
proboLogger.debug(`Starting scroll to bottom-right`);
|
|
1043
|
-
let lastHeight = await page.evaluate(() => document.documentElement.scrollHeight);
|
|
1044
|
-
let lastWidth = await page.evaluate(() => document.documentElement.scrollWidth);
|
|
1045
|
-
let smoothingSteps = 0;
|
|
1046
|
-
while (true) {
|
|
1047
|
-
const initY = await page.evaluate(() => window.scrollY);
|
|
1048
|
-
const initX = await page.evaluate(() => window.scrollX);
|
|
1049
|
-
const clientHeight = await page.evaluate(() => document.documentElement.clientHeight);
|
|
1050
|
-
const maxHeight = await page.evaluate(() => document.documentElement.scrollHeight);
|
|
1051
|
-
const clientWidth = await page.evaluate(() => document.documentElement.clientWidth);
|
|
1052
|
-
const maxWidth = await page.evaluate(() => document.documentElement.scrollWidth);
|
|
1053
|
-
let currX = initX;
|
|
1054
|
-
while (currX < maxWidth - clientWidth) {
|
|
1055
|
-
let currY = initY;
|
|
1056
|
-
while (currY < maxHeight - clientHeight) {
|
|
1057
|
-
currY += clientHeight;
|
|
1058
|
-
await page.evaluate(([x, y]) => window.scrollTo(x, y), [currX, currY]);
|
|
1059
|
-
await page.waitForTimeout(50);
|
|
1060
|
-
smoothingSteps++;
|
|
1061
|
-
}
|
|
1062
|
-
currX += clientWidth;
|
|
1063
|
-
}
|
|
1064
|
-
proboLogger.debug(`performed ${smoothingSteps} smoothing steps while scrolling`);
|
|
1065
|
-
const newHeight = await page.evaluate(() => document.documentElement.scrollHeight);
|
|
1066
|
-
const newWidth = await page.evaluate(() => document.documentElement.scrollWidth);
|
|
1067
|
-
if (newHeight === lastHeight && newWidth === lastWidth)
|
|
1068
|
-
break;
|
|
1069
|
-
proboLogger.debug(`page dimensions updated, repeating scroll`);
|
|
1070
|
-
lastHeight = newHeight;
|
|
1071
|
-
lastWidth = newWidth;
|
|
1072
|
-
}
|
|
1073
|
-
if (smoothingSteps > 0) {
|
|
1074
|
-
await page.waitForTimeout(200);
|
|
1075
|
-
await page.evaluate('window.scrollTo(0, 0)');
|
|
1076
|
-
await page.waitForTimeout(50);
|
|
1077
|
-
}
|
|
1078
|
-
proboLogger.debug(`Scroll completed in ${(performance.now() - startTime).toFixed(3)}ms`);
|
|
1079
|
-
}
|
|
1080
1016
|
/**
|
|
1081
1017
|
* Wait for DOM mutations to settle using MutationObserver logic
|
|
1082
1018
|
*/
|
|
1083
|
-
async function waitForMutationsToSettle(page, mutationTimeout =
|
|
1019
|
+
async function waitForMutationsToSettle(page, mutationTimeout = DEFAULT_PLAYWRIGHT_TIMEOUT_CONFIG.mutationsTimeout, initTimeout = DEFAULT_PLAYWRIGHT_TIMEOUT_CONFIG.mutationsInitTimeout) {
|
|
1084
1020
|
const startTime = Date.now();
|
|
1085
1021
|
proboLogger.debug(`Starting mutation settlement (initTimeout=${initTimeout}, mutationTimeout=${mutationTimeout})`);
|
|
1086
1022
|
const result = await page.evaluate(async ({ mutationTimeout, initTimeout }) => {
|
|
@@ -1118,381 +1054,15 @@ async function waitForMutationsToSettle(page, mutationTimeout = 1500, initTimeou
|
|
|
1118
1054
|
: `No mutations observed. Took ${total}s`);
|
|
1119
1055
|
return result;
|
|
1120
1056
|
}
|
|
1121
|
-
/**
|
|
1122
|
-
* Get element text value: allTextContents + innerText fallback
|
|
1123
|
-
*/
|
|
1124
|
-
async function getElementValue(page, locator) {
|
|
1125
|
-
const texts = await locator.allTextContents();
|
|
1126
|
-
let allText = texts.join('').trim();
|
|
1127
|
-
if (!allText) {
|
|
1128
|
-
allText = await locator.evaluate((el) => el.innerText);
|
|
1129
|
-
}
|
|
1130
|
-
if (!allText) {
|
|
1131
|
-
allText = await locator.inputValue();
|
|
1132
|
-
}
|
|
1133
|
-
proboLogger.debug(`getElementValue: [${allText}]`);
|
|
1134
|
-
return allText;
|
|
1135
|
-
}
|
|
1136
|
-
/**
|
|
1137
|
-
* Select dropdown option: native <select>, <option>, child select, or ARIA listbox
|
|
1138
|
-
*/
|
|
1139
|
-
async function selectDropdownOption(page, locator, value) {
|
|
1140
|
-
const tagName = await (locator === null || locator === void 0 ? void 0 : locator.evaluate((el) => el.tagName.toLowerCase()));
|
|
1141
|
-
const role = await (locator === null || locator === void 0 ? void 0 : locator.getAttribute('role'));
|
|
1142
|
-
if (tagName === 'option' || role === 'option') {
|
|
1143
|
-
proboLogger.debug('selectDropdownOption: option role detected');
|
|
1144
|
-
await (locator === null || locator === void 0 ? void 0 : locator.click());
|
|
1145
|
-
}
|
|
1146
|
-
else if (tagName === 'select') {
|
|
1147
|
-
proboLogger.debug('selectDropdownOption: simple select tag detected');
|
|
1148
|
-
try {
|
|
1149
|
-
await (locator === null || locator === void 0 ? void 0 : locator.selectOption(value));
|
|
1150
|
-
}
|
|
1151
|
-
catch (_a) {
|
|
1152
|
-
proboLogger.debug('selectDropdownOption: manual change event fallback');
|
|
1153
|
-
const handle = locator ? await locator.elementHandle() : null;
|
|
1154
|
-
await page.evaluate(({ h, val }) => {
|
|
1155
|
-
const el = h;
|
|
1156
|
-
if (el) {
|
|
1157
|
-
el.value = Array.isArray(val) ? val[0] : val;
|
|
1158
|
-
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
1159
|
-
}
|
|
1160
|
-
}, { h: handle, val: value });
|
|
1161
|
-
}
|
|
1162
|
-
}
|
|
1163
|
-
else {
|
|
1164
|
-
proboLogger.debug('selectDropdownOption: custom dropdown path');
|
|
1165
|
-
let listbox = locator.locator('select');
|
|
1166
|
-
let count = await listbox.count();
|
|
1167
|
-
if (count > 1)
|
|
1168
|
-
throw new Error(`selectDropdownOption: ambiguous <select> count=${count}`);
|
|
1169
|
-
if (count === 1) {
|
|
1170
|
-
proboLogger.debug('selectDropdownOption: child <select> found');
|
|
1171
|
-
await listbox.selectOption(value);
|
|
1172
|
-
return;
|
|
1173
|
-
}
|
|
1174
|
-
await locator.click();
|
|
1175
|
-
let container = locator;
|
|
1176
|
-
count = 0;
|
|
1177
|
-
if (role !== 'listbox') {
|
|
1178
|
-
for (let i = 0; i < 7; i++) {
|
|
1179
|
-
listbox = container.getByRole('listbox');
|
|
1180
|
-
count = await listbox.count();
|
|
1181
|
-
if (count >= 1)
|
|
1182
|
-
break;
|
|
1183
|
-
proboLogger.debug(`selectDropdownOption: iteration #${i} no listbox found`);
|
|
1184
|
-
container = container.locator('xpath=..');
|
|
1185
|
-
}
|
|
1186
|
-
}
|
|
1187
|
-
else {
|
|
1188
|
-
listbox = container;
|
|
1189
|
-
count = await listbox.count();
|
|
1190
|
-
}
|
|
1191
|
-
if (count !== 1)
|
|
1192
|
-
throw new Error(`selectDropdownOption: found ${count} listbox locators`);
|
|
1193
|
-
const vals = Array.isArray(value) ? value : [value];
|
|
1194
|
-
for (const val of vals) {
|
|
1195
|
-
const option = listbox.getByRole('option').getByText(new RegExp(`^${val}$`, 'i'));
|
|
1196
|
-
const optCount = await option.count();
|
|
1197
|
-
if (optCount !== 1)
|
|
1198
|
-
throw new Error(`selectDropdownOption: ${optCount} options for '${val}'`);
|
|
1199
|
-
await option.click();
|
|
1200
|
-
}
|
|
1201
|
-
}
|
|
1202
|
-
}
|
|
1203
|
-
/**
|
|
1204
|
-
* Execute a given Playwright action, mirroring Python's _perform_action
|
|
1205
|
-
*/
|
|
1206
|
-
async function executePlaywrightAction(page, action, value, iframe_selector, element_css_selector) {
|
|
1207
|
-
proboLogger.info(`performing Action: ${action} Value: ${value}`);
|
|
1208
|
-
try {
|
|
1209
|
-
if (action === PlaywrightAction.VISIT_BASE_URL || action === PlaywrightAction.VISIT_URL) {
|
|
1210
|
-
await page.goto(value, { waitUntil: 'load' });
|
|
1211
|
-
await handlePotentialNavigation(page, null);
|
|
1212
|
-
}
|
|
1213
|
-
else {
|
|
1214
|
-
let locator = undefined;
|
|
1215
|
-
if (iframe_selector)
|
|
1216
|
-
locator = page.frameLocator(iframe_selector).locator(element_css_selector);
|
|
1217
|
-
else
|
|
1218
|
-
locator = page.locator(element_css_selector);
|
|
1219
|
-
// Fail fast: immediately validate that the element exists
|
|
1220
|
-
try {
|
|
1221
|
-
await locator.waitFor({ state: 'attached' });
|
|
1222
|
-
}
|
|
1223
|
-
catch (e) {
|
|
1224
|
-
throw new Error(`Element not found with selector: ${element_css_selector}${iframe_selector ? ` in iframe: ${iframe_selector}` : ''}`);
|
|
1225
|
-
}
|
|
1226
|
-
switch (action) {
|
|
1227
|
-
case PlaywrightAction.CLICK:
|
|
1228
|
-
await handlePotentialNavigation(page, locator, value);
|
|
1229
|
-
break;
|
|
1230
|
-
case PlaywrightAction.FILL_IN:
|
|
1231
|
-
// await locator.click();
|
|
1232
|
-
await locator.fill(value);
|
|
1233
|
-
break;
|
|
1234
|
-
case PlaywrightAction.TYPE_KEYS:
|
|
1235
|
-
// Check if the value contains any special keys
|
|
1236
|
-
const specialKey = SPECIAL_KEYS.find(key => value.includes(key));
|
|
1237
|
-
if (specialKey) {
|
|
1238
|
-
// If it's a single special key, just press it
|
|
1239
|
-
if (value === specialKey) {
|
|
1240
|
-
await (locator === null || locator === void 0 ? void 0 : locator.press(specialKey));
|
|
1241
|
-
}
|
|
1242
|
-
else {
|
|
1243
|
-
// Handle combinations like 'Control+A' or 'Shift+ArrowRight'
|
|
1244
|
-
const parts = value.split('+');
|
|
1245
|
-
if (parts.length === 2 && SPECIAL_KEYS.includes(parts[0]) && SPECIAL_KEYS.includes(parts[1])) {
|
|
1246
|
-
await (locator === null || locator === void 0 ? void 0 : locator.press(value));
|
|
1247
|
-
}
|
|
1248
|
-
else {
|
|
1249
|
-
// If it's a mix of special keys and text, use pressSequentially
|
|
1250
|
-
await (locator === null || locator === void 0 ? void 0 : locator.pressSequentially(value));
|
|
1251
|
-
}
|
|
1252
|
-
}
|
|
1253
|
-
}
|
|
1254
|
-
else {
|
|
1255
|
-
// No special keys, just type normally
|
|
1256
|
-
await (locator === null || locator === void 0 ? void 0 : locator.pressSequentially(value));
|
|
1257
|
-
}
|
|
1258
|
-
break;
|
|
1259
|
-
case PlaywrightAction.SELECT_DROPDOWN:
|
|
1260
|
-
await selectDropdownOption(page, locator, value);
|
|
1261
|
-
break;
|
|
1262
|
-
case PlaywrightAction.SELECT_MULTIPLE_DROPDOWN:
|
|
1263
|
-
let optsArr;
|
|
1264
|
-
if (value.startsWith('[')) {
|
|
1265
|
-
try {
|
|
1266
|
-
optsArr = JSON.parse(value);
|
|
1267
|
-
}
|
|
1268
|
-
catch (_a) {
|
|
1269
|
-
optsArr = value.slice(1, -1).split(',').map(o => o.trim());
|
|
1270
|
-
}
|
|
1271
|
-
}
|
|
1272
|
-
else {
|
|
1273
|
-
optsArr = value.split(',').map(o => o.trim());
|
|
1274
|
-
}
|
|
1275
|
-
await (locator === null || locator === void 0 ? void 0 : locator.selectOption(optsArr));
|
|
1276
|
-
break;
|
|
1277
|
-
case PlaywrightAction.CHECK_CHECKBOX:
|
|
1278
|
-
await (locator === null || locator === void 0 ? void 0 : locator.setChecked(value == true));
|
|
1279
|
-
break;
|
|
1280
|
-
case PlaywrightAction.SELECT_RADIO:
|
|
1281
|
-
case PlaywrightAction.TOGGLE_SWITCH:
|
|
1282
|
-
await (locator === null || locator === void 0 ? void 0 : locator.click());
|
|
1283
|
-
break;
|
|
1284
|
-
case PlaywrightAction.HOVER:
|
|
1285
|
-
if (await locator.isVisible()) {
|
|
1286
|
-
await (locator === null || locator === void 0 ? void 0 : locator.hover({ noWaitAfter: false }));
|
|
1287
|
-
await page.waitForTimeout(100); // short delay for hover to take effect
|
|
1288
|
-
}
|
|
1289
|
-
break;
|
|
1290
|
-
case PlaywrightAction.VALIDATE_EXACT_VALUE:
|
|
1291
|
-
const actualExact = await getElementValue(page, locator);
|
|
1292
|
-
proboLogger.debug(`actual value is [${actualExact}]`);
|
|
1293
|
-
if (actualExact !== value) {
|
|
1294
|
-
proboLogger.info(`Validation *FAIL* expected '${value}' but got '${actualExact}'`);
|
|
1295
|
-
return false;
|
|
1296
|
-
}
|
|
1297
|
-
proboLogger.info('Validation *PASS*');
|
|
1298
|
-
return true;
|
|
1299
|
-
case PlaywrightAction.VALIDATE_CONTAINS_VALUE:
|
|
1300
|
-
case PlaywrightAction.VALIDATE:
|
|
1301
|
-
const actualContains = await getElementValue(page, locator);
|
|
1302
|
-
proboLogger.debug(`actual value is [${actualContains}]`);
|
|
1303
|
-
if (!actualContains.includes(value)) {
|
|
1304
|
-
proboLogger.info(`Validation *FAIL* expected '${value}' to be contained in '${actualContains}'`);
|
|
1305
|
-
return false;
|
|
1306
|
-
}
|
|
1307
|
-
proboLogger.info('Validation *PASS*');
|
|
1308
|
-
return true;
|
|
1309
|
-
case PlaywrightAction.VALIDATE_URL:
|
|
1310
|
-
const currUrl = page.url();
|
|
1311
|
-
if (currUrl !== value) {
|
|
1312
|
-
proboLogger.info(`Validation *FAIL* expected url '${value}' while is '${currUrl}'`);
|
|
1313
|
-
return false;
|
|
1314
|
-
}
|
|
1315
|
-
proboLogger.info('Validation *PASS*');
|
|
1316
|
-
return true;
|
|
1317
|
-
case PlaywrightAction.SCROLL_TO_ELEMENT:
|
|
1318
|
-
// Restore exact scroll positions from recording
|
|
1319
|
-
const scrollData = JSON.parse(value);
|
|
1320
|
-
try {
|
|
1321
|
-
proboLogger.debug('🔄 Restoring scroll position for container:', locator, 'scrollTop:', scrollData.scrollTop, 'scrollLeft:', scrollData.scrollLeft);
|
|
1322
|
-
await locator.evaluate((el, scrollData) => {
|
|
1323
|
-
el.scrollTop = scrollData.scrollTop;
|
|
1324
|
-
el.scrollLeft = scrollData.scrollLeft;
|
|
1325
|
-
}, { scrollTop: scrollData.scrollTop, scrollLeft: scrollData.scrollLeft }, { timeout: 2000 });
|
|
1326
|
-
}
|
|
1327
|
-
catch (e) {
|
|
1328
|
-
proboLogger.error('🔄 Failed to restore scroll position for container:', locator, 'scrollTop:', scrollData.scrollTop, 'scrollLeft:', scrollData.scrollLeft, 'error:', e);
|
|
1329
|
-
}
|
|
1330
|
-
await page.waitForTimeout(500);
|
|
1331
|
-
break;
|
|
1332
|
-
case PlaywrightAction.EXTRACT_VALUE:
|
|
1333
|
-
const extractedValue = await getElementValue(page, locator);
|
|
1334
|
-
proboLogger.debug(`extracted value is [${extractedValue}]`);
|
|
1335
|
-
return extractedValue;
|
|
1336
|
-
default:
|
|
1337
|
-
throw new Error(`Unknown action: ${action}`);
|
|
1338
|
-
}
|
|
1339
|
-
}
|
|
1340
|
-
return true;
|
|
1341
|
-
}
|
|
1342
|
-
catch (e) {
|
|
1343
|
-
proboLogger.debug(`***ERROR failed to execute action ${action}: ${e}`);
|
|
1344
|
-
throw e;
|
|
1345
|
-
}
|
|
1346
|
-
}
|
|
1347
1057
|
/**
|
|
1348
1058
|
* Execute a given Playwright action using native Playwright functions where possible
|
|
1349
1059
|
*/
|
|
1350
|
-
async function executeCachedPlaywrightAction(page, action, value, iframe_selector, element_css_selector) {
|
|
1351
|
-
proboLogger.log(`performing Cached Action: ${action} Value: ${value} on iframe: ${iframe_selector} locator: ${element_css_selector}`);
|
|
1352
|
-
try {
|
|
1353
|
-
let locator = undefined;
|
|
1354
|
-
if (iframe_selector)
|
|
1355
|
-
locator = page.frameLocator(iframe_selector).locator(element_css_selector);
|
|
1356
|
-
else
|
|
1357
|
-
locator = page.locator(element_css_selector);
|
|
1358
|
-
switch (action) {
|
|
1359
|
-
case PlaywrightAction.VISIT_BASE_URL:
|
|
1360
|
-
case PlaywrightAction.VISIT_URL:
|
|
1361
|
-
await page.goto(value, { waitUntil: 'networkidle' });
|
|
1362
|
-
break;
|
|
1363
|
-
case PlaywrightAction.CLICK:
|
|
1364
|
-
await clickAtPosition(page, locator, value);
|
|
1365
|
-
await handlePotentialNavigation(page);
|
|
1366
|
-
break;
|
|
1367
|
-
case PlaywrightAction.FILL_IN:
|
|
1368
|
-
// await locator.click();
|
|
1369
|
-
await locator.fill(value);
|
|
1370
|
-
break;
|
|
1371
|
-
case PlaywrightAction.TYPE_KEYS:
|
|
1372
|
-
// Check if the value contains any special keys
|
|
1373
|
-
const specialKey = SPECIAL_KEYS.find(key => value.includes(key));
|
|
1374
|
-
if (specialKey) {
|
|
1375
|
-
// If it's a single special key, just press it
|
|
1376
|
-
if (value === specialKey) {
|
|
1377
|
-
await locator.press(specialKey);
|
|
1378
|
-
}
|
|
1379
|
-
else {
|
|
1380
|
-
// Handle combinations like 'Control+A' or 'Shift+ArrowRight'
|
|
1381
|
-
const parts = value.split('+');
|
|
1382
|
-
if (parts.length === 2 && SPECIAL_KEYS.includes(parts[0]) && SPECIAL_KEYS.includes(parts[1])) {
|
|
1383
|
-
await locator.press(value);
|
|
1384
|
-
}
|
|
1385
|
-
else {
|
|
1386
|
-
// If it's a mix of special keys and text, use pressSequentially
|
|
1387
|
-
await locator.pressSequentially(value);
|
|
1388
|
-
}
|
|
1389
|
-
}
|
|
1390
|
-
}
|
|
1391
|
-
else {
|
|
1392
|
-
// No special keys, just type normally
|
|
1393
|
-
await locator.pressSequentially(value);
|
|
1394
|
-
}
|
|
1395
|
-
break;
|
|
1396
|
-
case PlaywrightAction.SELECT_DROPDOWN:
|
|
1397
|
-
await locator.selectOption(value);
|
|
1398
|
-
break;
|
|
1399
|
-
case PlaywrightAction.SELECT_MULTIPLE_DROPDOWN:
|
|
1400
|
-
let optsArr;
|
|
1401
|
-
if (value.startsWith('[')) {
|
|
1402
|
-
try {
|
|
1403
|
-
optsArr = JSON.parse(value);
|
|
1404
|
-
}
|
|
1405
|
-
catch (_a) {
|
|
1406
|
-
optsArr = value.slice(1, -1).split(',').map(o => o.trim());
|
|
1407
|
-
}
|
|
1408
|
-
}
|
|
1409
|
-
else {
|
|
1410
|
-
optsArr = value.split(',').map(o => o.trim());
|
|
1411
|
-
}
|
|
1412
|
-
await locator.selectOption(optsArr);
|
|
1413
|
-
break;
|
|
1414
|
-
case PlaywrightAction.CHECK_CHECKBOX:
|
|
1415
|
-
await locator.setChecked(value.toLowerCase() === 'true');
|
|
1416
|
-
break;
|
|
1417
|
-
case PlaywrightAction.SELECT_RADIO:
|
|
1418
|
-
case PlaywrightAction.TOGGLE_SWITCH:
|
|
1419
|
-
await locator.click();
|
|
1420
|
-
break;
|
|
1421
|
-
case PlaywrightAction.HOVER:
|
|
1422
|
-
if (await locator.isVisible()) {
|
|
1423
|
-
await (locator === null || locator === void 0 ? void 0 : locator.hover({ noWaitAfter: false }));
|
|
1424
|
-
await page.waitForTimeout(100); // short delay for hover to take effect
|
|
1425
|
-
}
|
|
1426
|
-
break;
|
|
1427
|
-
case PlaywrightAction.VALIDATE_EXACT_VALUE:
|
|
1428
|
-
const actualExact = await getElementValue(page, locator);
|
|
1429
|
-
proboLogger.debug(`actual value is [${actualExact}]`);
|
|
1430
|
-
if (actualExact !== value) {
|
|
1431
|
-
proboLogger.info(`Validation *FAIL* expected '${value}' but got '${actualExact}'`);
|
|
1432
|
-
return false;
|
|
1433
|
-
}
|
|
1434
|
-
proboLogger.info('Validation *PASS*');
|
|
1435
|
-
return true;
|
|
1436
|
-
case PlaywrightAction.VALIDATE_CONTAINS_VALUE:
|
|
1437
|
-
case PlaywrightAction.VALIDATE:
|
|
1438
|
-
const actualContains = await getElementValue(page, locator);
|
|
1439
|
-
proboLogger.debug(`actual value is [${actualContains}]`);
|
|
1440
|
-
if (!actualContains.includes(value)) {
|
|
1441
|
-
proboLogger.info(`Validation *FAIL* expected '${value}' to be contained in '${actualContains}'`);
|
|
1442
|
-
return false;
|
|
1443
|
-
}
|
|
1444
|
-
proboLogger.info('Validation *PASS*');
|
|
1445
|
-
return true;
|
|
1446
|
-
case PlaywrightAction.VALIDATE_URL:
|
|
1447
|
-
const currUrl = page.url();
|
|
1448
|
-
if (currUrl !== value) {
|
|
1449
|
-
proboLogger.info(`Validation *FAIL* expected url '${value}' while is '${currUrl}'`);
|
|
1450
|
-
return false;
|
|
1451
|
-
}
|
|
1452
|
-
proboLogger.info('Validation *PASS*');
|
|
1453
|
-
return true;
|
|
1454
|
-
case PlaywrightAction.EXTRACT_VALUE:
|
|
1455
|
-
const extractedValue = await getElementValue(page, locator);
|
|
1456
|
-
proboLogger.debug(`extracted value is [${extractedValue}]`);
|
|
1457
|
-
return extractedValue;
|
|
1458
|
-
default:
|
|
1459
|
-
throw new Error(`Unknown action: ${action}`);
|
|
1460
|
-
}
|
|
1461
|
-
return true;
|
|
1462
|
-
}
|
|
1463
|
-
catch (e) {
|
|
1464
|
-
proboLogger.debug(`***ERROR failed to execute cached action ${action}: ${e}`);
|
|
1465
|
-
throw e;
|
|
1466
|
-
}
|
|
1467
|
-
}
|
|
1468
1060
|
var ClickPosition;
|
|
1469
1061
|
(function (ClickPosition) {
|
|
1470
1062
|
ClickPosition["LEFT"] = "LEFT";
|
|
1471
1063
|
ClickPosition["RIGHT"] = "RIGHT";
|
|
1472
1064
|
ClickPosition["CENTER"] = "CENTER";
|
|
1473
1065
|
})(ClickPosition || (ClickPosition = {}));
|
|
1474
|
-
/**
|
|
1475
|
-
* Click on an element at a specific position
|
|
1476
|
-
*/
|
|
1477
|
-
async function clickAtPosition(page, locator, clickPosition) {
|
|
1478
|
-
if (!clickPosition || clickPosition === ClickPosition.CENTER) {
|
|
1479
|
-
await locator.click({ noWaitAfter: false });
|
|
1480
|
-
return;
|
|
1481
|
-
}
|
|
1482
|
-
const boundingBox = await locator.boundingBox();
|
|
1483
|
-
if (boundingBox) {
|
|
1484
|
-
if (clickPosition === ClickPosition.LEFT) {
|
|
1485
|
-
// await page.mouse.click(boundingBox.x + 10, boundingBox.y + boundingBox.height/2);
|
|
1486
|
-
// mouse click returns immediately, use locator click instead (position relative to top left corner)
|
|
1487
|
-
await locator.click({ noWaitAfter: false, position: { x: 10, y: boundingBox.height / 2 } });
|
|
1488
|
-
}
|
|
1489
|
-
else if (clickPosition === ClickPosition.RIGHT) {
|
|
1490
|
-
// await page.mouse.click(boundingBox.x + boundingBox.width - 10, boundingBox.y + boundingBox.height/2);
|
|
1491
|
-
// mouse click returns immediately, use locator click instead (position relative to top left corner)
|
|
1492
|
-
await locator.click({ noWaitAfter: false, position: { x: boundingBox.width - 10, y: boundingBox.height / 2 } });
|
|
1493
|
-
}
|
|
1494
|
-
}
|
|
1495
|
-
}
|
|
1496
1066
|
/**
|
|
1497
1067
|
* Traverses up the DOM from the given locator to find the closest visible ancestor.
|
|
1498
1068
|
* Returns a Locator for the first visible element found, or null if none is visible up to <html>.
|
|
@@ -1531,9 +1101,9 @@ class Highlighter {
|
|
|
1531
1101
|
proboLogger.debug('Injecting highlighter script...');
|
|
1532
1102
|
await page.evaluate(highlighterCode);
|
|
1533
1103
|
// Verify the script was injected correctly
|
|
1534
|
-
const verified = await page.evaluate(`
|
|
1535
|
-
//console.log('ProboLabs global:', window.ProboLabs);
|
|
1536
|
-
typeof window.ProboLabs?.highlight?.execute === 'function'
|
|
1104
|
+
const verified = await page.evaluate(`
|
|
1105
|
+
//console.log('ProboLabs global:', window.ProboLabs);
|
|
1106
|
+
typeof window.ProboLabs?.highlight?.execute === 'function'
|
|
1537
1107
|
`);
|
|
1538
1108
|
proboLogger.debug('Script injection verified:', verified);
|
|
1539
1109
|
}
|
|
@@ -1697,7 +1267,12 @@ class Highlighter {
|
|
|
1697
1267
|
class ProboPlaywright {
|
|
1698
1268
|
constructor(config = DEFAULT_PLAYWRIGHT_TIMEOUT_CONFIG, page = null) {
|
|
1699
1269
|
this.page = null;
|
|
1700
|
-
this.
|
|
1270
|
+
this.lastNavigationTime = null;
|
|
1271
|
+
// Merge provided config with defaults to ensure all properties are defined
|
|
1272
|
+
this.config = {
|
|
1273
|
+
...DEFAULT_PLAYWRIGHT_TIMEOUT_CONFIG,
|
|
1274
|
+
...config
|
|
1275
|
+
};
|
|
1701
1276
|
this.setPage(page);
|
|
1702
1277
|
}
|
|
1703
1278
|
/**
|
|
@@ -1711,8 +1286,15 @@ class ProboPlaywright {
|
|
|
1711
1286
|
if (this.page) {
|
|
1712
1287
|
this.page.setDefaultNavigationTimeout(this.config.playwrightNavigationTimeout);
|
|
1713
1288
|
this.page.setDefaultTimeout(this.config.playwrightActionTimeout);
|
|
1289
|
+
// start the nav handler listener
|
|
1290
|
+
this.page.on("framenavigated", this.onFrameNav.bind(this));
|
|
1714
1291
|
}
|
|
1715
1292
|
}
|
|
1293
|
+
onFrameNav(frame) {
|
|
1294
|
+
// if (frame === this.page?.mainFrame()) {
|
|
1295
|
+
// this.lastNavigationTime = Date.now();
|
|
1296
|
+
// }
|
|
1297
|
+
}
|
|
1716
1298
|
/**
|
|
1717
1299
|
* Executes a single step in the test scenario with the specified action on the target element.
|
|
1718
1300
|
* Handles iframe navigation, element highlighting, and various Playwright actions like click, fill, validate, etc.
|
|
@@ -1731,6 +1313,7 @@ class ProboPlaywright {
|
|
|
1731
1313
|
if (action === PlaywrightAction.VISIT_URL || action === PlaywrightAction.VISIT_BASE_URL) {
|
|
1732
1314
|
try {
|
|
1733
1315
|
await this.page.goto(argument, { timeout: this.config.playwrightNavigationTimeout });
|
|
1316
|
+
// await handlePotentialNavigation(this.page);
|
|
1734
1317
|
}
|
|
1735
1318
|
catch (e) {
|
|
1736
1319
|
throw new Error(`Failed to navigate to ${argument}`);
|
|
@@ -1738,6 +1321,7 @@ class ProboPlaywright {
|
|
|
1738
1321
|
return;
|
|
1739
1322
|
}
|
|
1740
1323
|
// 2. Get the locator (iframe or not)
|
|
1324
|
+
const startTime = Date.now();
|
|
1741
1325
|
let locator;
|
|
1742
1326
|
if (iframeSelector && iframeSelector.length > 0) {
|
|
1743
1327
|
locator = this.page.frameLocator(iframeSelector).locator(elementSelector);
|
|
@@ -1745,12 +1329,13 @@ class ProboPlaywright {
|
|
|
1745
1329
|
else {
|
|
1746
1330
|
locator = this.page.locator(elementSelector);
|
|
1747
1331
|
}
|
|
1748
|
-
// Fail fast: immediately validate that the element exists
|
|
1332
|
+
// Fail fast: immediately validate that the element exists for non-wait actions
|
|
1333
|
+
const locator_timeout = (action === PlaywrightAction.WAIT_FOR) ? params.timeout || 10000 : this.config.playwrightLocatorTimeout;
|
|
1749
1334
|
try {
|
|
1750
|
-
await locator.waitFor({ state: 'attached', timeout:
|
|
1335
|
+
await locator.waitFor({ state: 'attached', timeout: locator_timeout });
|
|
1751
1336
|
}
|
|
1752
1337
|
catch (e) {
|
|
1753
|
-
throw new Error(`Element not found with selector: ${elementSelector}${iframeSelector ? ` in iframe: ${iframeSelector}` : ''}`);
|
|
1338
|
+
throw new Error(`Element not found with selector: ${elementSelector}${iframeSelector ? ` in iframe: ${iframeSelector}` : ''} after ${locator_timeout}ms`);
|
|
1754
1339
|
}
|
|
1755
1340
|
if (action === PlaywrightAction.HOVER) {
|
|
1756
1341
|
const visibleLocator = await findClosestVisibleElement(locator);
|
|
@@ -1770,6 +1355,7 @@ class ProboPlaywright {
|
|
|
1770
1355
|
case PlaywrightAction.CHECK_CHECKBOX:
|
|
1771
1356
|
case PlaywrightAction.SELECT_RADIO:
|
|
1772
1357
|
await this.robustClick(locator);
|
|
1358
|
+
// await handlePotentialNavigation(this.page);
|
|
1773
1359
|
break;
|
|
1774
1360
|
case PlaywrightAction.FILL_IN:
|
|
1775
1361
|
await this.robustFill(locator, argument);
|
|
@@ -1777,9 +1363,13 @@ class ProboPlaywright {
|
|
|
1777
1363
|
case PlaywrightAction.SELECT_DROPDOWN:
|
|
1778
1364
|
await locator.selectOption(argument);
|
|
1779
1365
|
break;
|
|
1780
|
-
case PlaywrightAction.VALIDATE:
|
|
1781
|
-
case PlaywrightAction.VALIDATE_EXACT_VALUE:
|
|
1782
1366
|
case PlaywrightAction.VALIDATE_CONTAINS_VALUE:
|
|
1367
|
+
const containerText = await this.getTextValue(locator);
|
|
1368
|
+
if (!matchRegex(containerText, argument)) {
|
|
1369
|
+
throw new Error(`Validation failed. Expected text "${containerText}" to match "${argument}".`);
|
|
1370
|
+
}
|
|
1371
|
+
break;
|
|
1372
|
+
case PlaywrightAction.VALIDATE_EXACT_VALUE:
|
|
1783
1373
|
const actualText = await this.getTextValue(locator);
|
|
1784
1374
|
if (actualText !== argument) {
|
|
1785
1375
|
throw new Error(`Validation failed. Expected text "${argument}", but got "${actualText}".`);
|
|
@@ -1813,7 +1403,48 @@ class ProboPlaywright {
|
|
|
1813
1403
|
break;
|
|
1814
1404
|
case PlaywrightAction.EXTRACT_VALUE:
|
|
1815
1405
|
let extractedText = await this.getTextValue(locator);
|
|
1816
|
-
return
|
|
1406
|
+
return extractedText;
|
|
1407
|
+
case PlaywrightAction.WAIT_FOR:
|
|
1408
|
+
const expectedText = argument;
|
|
1409
|
+
const pollingInterval = params.pollingInterval || 500; // Default 500ms
|
|
1410
|
+
const timeout = params.timeout || 10000; // Default 10 seconds
|
|
1411
|
+
let textMatches = false;
|
|
1412
|
+
let currentText = '';
|
|
1413
|
+
while (!textMatches && (Date.now() - startTime) < timeout) {
|
|
1414
|
+
try {
|
|
1415
|
+
// Check if element is visible first
|
|
1416
|
+
const isVisible = await locator.isVisible();
|
|
1417
|
+
if (isVisible) {
|
|
1418
|
+
// Get the current text content only if element is visible
|
|
1419
|
+
currentText = await this.getTextValue(locator);
|
|
1420
|
+
// Check if the text matches (using the same logic as VALIDATE_CONTAINS_VALUE)
|
|
1421
|
+
if (matchRegex(currentText, expectedText)) {
|
|
1422
|
+
textMatches = true;
|
|
1423
|
+
console.log(`✅ Wait for text completed successfully. Found: "${currentText}"`);
|
|
1424
|
+
}
|
|
1425
|
+
else {
|
|
1426
|
+
// Text doesn't match yet, wait for the polling interval
|
|
1427
|
+
if ((Date.now() - startTime) < timeout) {
|
|
1428
|
+
await this.page.waitForTimeout(pollingInterval);
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
else {
|
|
1433
|
+
// Element is not visible, wait for the polling interval
|
|
1434
|
+
if ((Date.now() - startTime) < timeout) {
|
|
1435
|
+
await this.page.waitForTimeout(pollingInterval);
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
catch (e) {
|
|
1440
|
+
throw new Error(`Wait for text failed while trying to extract text from selector: ${elementSelector}${iframeSelector ? ` in iframe: ${iframeSelector}` : ''}`);
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
// Timeout reached without a match
|
|
1444
|
+
if (!textMatches) {
|
|
1445
|
+
throw new Error(`Wait for text failed. Expected "${expectedText}" to match "${currentText}" after ${timeout}ms of polling every ${pollingInterval}ms`);
|
|
1446
|
+
}
|
|
1447
|
+
break;
|
|
1817
1448
|
default:
|
|
1818
1449
|
throw new Error(`Unhandled action: ${action}`);
|
|
1819
1450
|
}
|
|
@@ -1830,26 +1461,26 @@ class ProboPlaywright {
|
|
|
1830
1461
|
await locator.evaluate((el) => {
|
|
1831
1462
|
const overlay = el.ownerDocument.createElement('div');
|
|
1832
1463
|
overlay.id = 'highlight-overlay';
|
|
1833
|
-
overlay.style.cssText = `
|
|
1834
|
-
position: fixed;
|
|
1835
|
-
top: 0;
|
|
1836
|
-
left: 0;
|
|
1837
|
-
width: 100%;
|
|
1838
|
-
height: 100%;
|
|
1839
|
-
pointer-events: none;
|
|
1840
|
-
z-index: 2147483647;
|
|
1464
|
+
overlay.style.cssText = `
|
|
1465
|
+
position: fixed;
|
|
1466
|
+
top: 0;
|
|
1467
|
+
left: 0;
|
|
1468
|
+
width: 100%;
|
|
1469
|
+
height: 100%;
|
|
1470
|
+
pointer-events: none;
|
|
1471
|
+
z-index: 2147483647;
|
|
1841
1472
|
`;
|
|
1842
1473
|
el.ownerDocument.body.appendChild(overlay);
|
|
1843
1474
|
const bbox = el.getBoundingClientRect();
|
|
1844
1475
|
const highlight = el.ownerDocument.createElement('div');
|
|
1845
|
-
highlight.style.cssText = `
|
|
1846
|
-
position: fixed;
|
|
1847
|
-
left: ${bbox.x}px;
|
|
1848
|
-
top: ${bbox.y}px;
|
|
1849
|
-
width: ${bbox.width}px;
|
|
1850
|
-
height: ${bbox.height}px;
|
|
1851
|
-
border: 2px solid rgb(255, 0, 0);
|
|
1852
|
-
transition: all 0.2s ease-in-out;
|
|
1476
|
+
highlight.style.cssText = `
|
|
1477
|
+
position: fixed;
|
|
1478
|
+
left: ${bbox.x}px;
|
|
1479
|
+
top: ${bbox.y}px;
|
|
1480
|
+
width: ${bbox.width}px;
|
|
1481
|
+
height: ${bbox.height}px;
|
|
1482
|
+
border: 2px solid rgb(255, 0, 0);
|
|
1483
|
+
transition: all 0.2s ease-in-out;
|
|
1853
1484
|
`;
|
|
1854
1485
|
overlay.appendChild(highlight);
|
|
1855
1486
|
}, { timeout: 500 });
|
|
@@ -1863,19 +1494,19 @@ class ProboPlaywright {
|
|
|
1863
1494
|
if (overlay) {
|
|
1864
1495
|
const bbox = el.getBoundingClientRect();
|
|
1865
1496
|
const annotationEl = el.ownerDocument.createElement('div');
|
|
1866
|
-
annotationEl.style.cssText = `
|
|
1867
|
-
position: fixed;
|
|
1868
|
-
left: ${bbox.x}px;
|
|
1869
|
-
top: ${bbox.y - 25}px;
|
|
1870
|
-
padding: 2px 6px;
|
|
1871
|
-
background-color: rgba(255, 255, 0, 0.6);
|
|
1872
|
-
color: black;
|
|
1873
|
-
font-size: 16px;
|
|
1874
|
-
font-family: 'Courier New', Courier, monospace;
|
|
1875
|
-
font-weight: bold;
|
|
1876
|
-
border-radius: 3px;
|
|
1877
|
-
pointer-events: none;
|
|
1878
|
-
z-index: 2147483647;
|
|
1497
|
+
annotationEl.style.cssText = `
|
|
1498
|
+
position: fixed;
|
|
1499
|
+
left: ${bbox.x}px;
|
|
1500
|
+
top: ${bbox.y - 25}px;
|
|
1501
|
+
padding: 2px 6px;
|
|
1502
|
+
background-color: rgba(255, 255, 0, 0.6);
|
|
1503
|
+
color: black;
|
|
1504
|
+
font-size: 16px;
|
|
1505
|
+
font-family: 'Courier New', Courier, monospace;
|
|
1506
|
+
font-weight: bold;
|
|
1507
|
+
border-radius: 3px;
|
|
1508
|
+
pointer-events: none;
|
|
1509
|
+
z-index: 2147483647;
|
|
1879
1510
|
`;
|
|
1880
1511
|
annotationEl.textContent = annotation;
|
|
1881
1512
|
// If element is too close to top of window, position annotation below
|
|
@@ -2024,6 +1655,159 @@ class ProboPlaywright {
|
|
|
2024
1655
|
;
|
|
2025
1656
|
} /* class ProboPlaywright */
|
|
2026
1657
|
|
|
1658
|
+
/**
|
|
1659
|
+
* Global navigation tracker that monitors page navigation events
|
|
1660
|
+
* and provides methods to check if navigation has stabilized
|
|
1661
|
+
*
|
|
1662
|
+
* This is a singleton class - only one instance can exist at a time
|
|
1663
|
+
*/
|
|
1664
|
+
class NavTracker {
|
|
1665
|
+
/**
|
|
1666
|
+
* Private constructor - use getInstance() to get the singleton instance
|
|
1667
|
+
*/
|
|
1668
|
+
constructor(page, options = {}) {
|
|
1669
|
+
var _a;
|
|
1670
|
+
this.navigationCount = 0;
|
|
1671
|
+
this.lastNavTime = null;
|
|
1672
|
+
this.isListening = false;
|
|
1673
|
+
this.page = page;
|
|
1674
|
+
this.stabilizationTimeout = (_a = options.stabilizationTimeout) !== null && _a !== void 0 ? _a : 2000;
|
|
1675
|
+
this.instanceId = Math.random().toString(36).substr(2, 9);
|
|
1676
|
+
this.onFrameNavHandler = (frame) => {
|
|
1677
|
+
if (frame === this.page.mainFrame()) {
|
|
1678
|
+
this.navigationCount++;
|
|
1679
|
+
this.lastNavTime = Date.now();
|
|
1680
|
+
proboLogger.debug(`NavTracker[${this.instanceId}]: navigation detected (navigationCount=${this.navigationCount}, lastNavTime=${new Date(this.lastNavTime).toISOString().slice(11, 23)}, timeSinceLastNav=${Date.now() - this.lastNavTime}ms)`);
|
|
1681
|
+
}
|
|
1682
|
+
};
|
|
1683
|
+
// Auto-start the tracker when first instantiated
|
|
1684
|
+
this.start();
|
|
1685
|
+
}
|
|
1686
|
+
/**
|
|
1687
|
+
* Start listening for navigation events (private method)
|
|
1688
|
+
*/
|
|
1689
|
+
start() {
|
|
1690
|
+
if (this.isListening) {
|
|
1691
|
+
proboLogger.debug(`NavTracker[${this.instanceId}]: already listening, ignoring start()`);
|
|
1692
|
+
return;
|
|
1693
|
+
}
|
|
1694
|
+
this.page.on("framenavigated", this.onFrameNavHandler);
|
|
1695
|
+
this.isListening = true;
|
|
1696
|
+
proboLogger.debug(`NavTracker[${this.instanceId}]: started listening for navigation events`);
|
|
1697
|
+
}
|
|
1698
|
+
/**
|
|
1699
|
+
* Stop listening for navigation events (private method)
|
|
1700
|
+
*/
|
|
1701
|
+
stop() {
|
|
1702
|
+
if (!this.isListening) {
|
|
1703
|
+
proboLogger.debug(`NavTracker[${this.instanceId}]: not listening, ignoring stop()`);
|
|
1704
|
+
return;
|
|
1705
|
+
}
|
|
1706
|
+
this.page.removeListener("framenavigated", this.onFrameNavHandler);
|
|
1707
|
+
this.isListening = false;
|
|
1708
|
+
proboLogger.debug(`NavTracker[${this.instanceId}]: stopped listening for navigation events`);
|
|
1709
|
+
}
|
|
1710
|
+
/**
|
|
1711
|
+
* Check if navigation has stabilized (no navigation for stabilizationTimeout ms) (private method)
|
|
1712
|
+
*/
|
|
1713
|
+
hasNavigationStabilized() {
|
|
1714
|
+
if (this.lastNavTime === null) {
|
|
1715
|
+
// No navigation has occurred yet
|
|
1716
|
+
return true;
|
|
1717
|
+
}
|
|
1718
|
+
const timeSinceLastNav = Date.now() - this.lastNavTime;
|
|
1719
|
+
const isStabilized = timeSinceLastNav >= this.stabilizationTimeout;
|
|
1720
|
+
proboLogger.debug(`NavTracker[${this.instanceId}]: hasNavigationStabilized() - timeSinceLastNav=${timeSinceLastNav}ms, stabilizationTimeout=${this.stabilizationTimeout}ms, stabilized=${isStabilized}`);
|
|
1721
|
+
return isStabilized;
|
|
1722
|
+
}
|
|
1723
|
+
/**
|
|
1724
|
+
* Wait for navigation to stabilize
|
|
1725
|
+
* Waits a short time to catch any missed navigation events, then ensures
|
|
1726
|
+
* the latest navigation happened at least stabilizationTimeout ms ago
|
|
1727
|
+
*/
|
|
1728
|
+
async waitForNavigationToStabilize() {
|
|
1729
|
+
proboLogger.debug(`NavTracker[${this.instanceId}]: waiting for navigation to stabilize`);
|
|
1730
|
+
// Wait 100ms to catch any navigation events we might have missed
|
|
1731
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
1732
|
+
// Now wait until navigation has stabilized (no navigation for stabilizationTimeout ms)
|
|
1733
|
+
while (!this.hasNavigationStabilized()) {
|
|
1734
|
+
// Wait 100ms before checking again
|
|
1735
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
1736
|
+
}
|
|
1737
|
+
proboLogger.debug(`NavTracker[${this.instanceId}]: navigation has stabilized`);
|
|
1738
|
+
}
|
|
1739
|
+
// ============================================================================
|
|
1740
|
+
// SINGLETON METHODS
|
|
1741
|
+
// ============================================================================
|
|
1742
|
+
/**
|
|
1743
|
+
* Get the singleton instance of NavTracker
|
|
1744
|
+
* @param page The page to track (required for first creation)
|
|
1745
|
+
* @param options Optional configuration
|
|
1746
|
+
* @returns The singleton NavTracker instance
|
|
1747
|
+
*/
|
|
1748
|
+
static getInstance(page, options) {
|
|
1749
|
+
if (!NavTracker.instance) {
|
|
1750
|
+
if (!page) {
|
|
1751
|
+
throw new Error('NavTracker: Page is required for first instance creation');
|
|
1752
|
+
}
|
|
1753
|
+
NavTracker.instance = new NavTracker(page, options);
|
|
1754
|
+
proboLogger.debug(`NavTracker: created new singleton instance`);
|
|
1755
|
+
}
|
|
1756
|
+
else {
|
|
1757
|
+
proboLogger.debug(`NavTracker: returning existing singleton instance`);
|
|
1758
|
+
}
|
|
1759
|
+
return NavTracker.instance;
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
NavTracker.instance = null;
|
|
1763
|
+
|
|
1764
|
+
/**
|
|
1765
|
+
* Creates Probo fixtures that connect to an existing Chrome instance
|
|
1766
|
+
* instead of launching a new browser.
|
|
1767
|
+
*
|
|
1768
|
+
* @param config Configuration options for the fixtures
|
|
1769
|
+
* @returns Extended test function with Probo fixtures
|
|
1770
|
+
*/
|
|
1771
|
+
function createProboFixtures(config = {}) {
|
|
1772
|
+
const { debugPort = 9333, showBrowserConsole = false } = config;
|
|
1773
|
+
return test$1.extend({
|
|
1774
|
+
browser: async ({}, use) => {
|
|
1775
|
+
// Connect to your existing recorder app's browser
|
|
1776
|
+
const browser = await chromium.connectOverCDP(`http://localhost:${debugPort}`);
|
|
1777
|
+
// Get the existing browser context (the one from your recorder app)
|
|
1778
|
+
const context = browser.contexts()[0];
|
|
1779
|
+
if (!context) {
|
|
1780
|
+
throw new Error(`No browser context found at http://localhost:${debugPort}. Make sure your recorder app is running.`);
|
|
1781
|
+
}
|
|
1782
|
+
// Use the existing context
|
|
1783
|
+
await use(browser);
|
|
1784
|
+
// Don't close the browser - it's managed by your recorder app
|
|
1785
|
+
},
|
|
1786
|
+
// Override the page fixture to use existing page
|
|
1787
|
+
page: async ({ browser }, use) => {
|
|
1788
|
+
const context = browser.contexts()[0];
|
|
1789
|
+
const allPages = context.pages();
|
|
1790
|
+
// Find the main page (not chrome-extension pages)
|
|
1791
|
+
const mainPage = allPages.find(page => !page.url().startsWith('chrome-extension://'));
|
|
1792
|
+
if (!mainPage) {
|
|
1793
|
+
throw new Error('No main page found in existing browser context. Make sure your recorder app has opened a page.');
|
|
1794
|
+
}
|
|
1795
|
+
// Set up console logging if requested
|
|
1796
|
+
if (showBrowserConsole) {
|
|
1797
|
+
mainPage.on('console', msg => console.log('Browser console:', msg.text()));
|
|
1798
|
+
}
|
|
1799
|
+
// Use the existing page
|
|
1800
|
+
await use(mainPage);
|
|
1801
|
+
// Don't close the page - it's managed by your recorder app
|
|
1802
|
+
},
|
|
1803
|
+
});
|
|
1804
|
+
}
|
|
1805
|
+
/**
|
|
1806
|
+
* Default Probo fixtures with standard configuration
|
|
1807
|
+
* Connects to Chrome instance running on port 9333
|
|
1808
|
+
*/
|
|
1809
|
+
const test = createProboFixtures();
|
|
1810
|
+
|
|
2027
1811
|
const retryOptions = {
|
|
2028
1812
|
retries: 3,
|
|
2029
1813
|
minTimeout: 1000,
|
|
@@ -2033,7 +1817,7 @@ const retryOptions = {
|
|
|
2033
1817
|
}
|
|
2034
1818
|
};
|
|
2035
1819
|
class Probo {
|
|
2036
|
-
constructor({ scenarioName, token = '', apiUrl = '', enableConsoleLogs = false, logToConsole = true, logToFile = false, debugLevel = ProboLogLevel.INFO, aiModel = AIModel.AZURE_GPT4_MINI }) {
|
|
1820
|
+
constructor({ scenarioName, token = '', apiUrl = '', enableConsoleLogs = false, logToConsole = true, logToFile = false, debugLevel = ProboLogLevel.INFO, aiModel = AIModel.AZURE_GPT4_MINI, timeoutConfig = DEFAULT_PLAYWRIGHT_TIMEOUT_CONFIG }) {
|
|
2037
1821
|
// Configure logger transports and level
|
|
2038
1822
|
// configureLogger({ logToConsole, logToFile, level: debugLevel });
|
|
2039
1823
|
proboLogger.setLogLevel(debugLevel);
|
|
@@ -2052,6 +1836,7 @@ class Probo {
|
|
|
2052
1836
|
this.enableConsoleLogs = enableConsoleLogs;
|
|
2053
1837
|
this.scenarioName = scenarioName;
|
|
2054
1838
|
this.aiModel = aiModel;
|
|
1839
|
+
this.timeoutConfig = timeoutConfig;
|
|
2055
1840
|
// set the log level for the api client
|
|
2056
1841
|
apiLogger.setLogLevel(debugLevel);
|
|
2057
1842
|
proboLogger.info(`Initializing: scenario=${scenarioName}, apiUrl=${apiEndPoint}, ` +
|
|
@@ -2065,11 +1850,13 @@ class Probo {
|
|
|
2065
1850
|
}
|
|
2066
1851
|
return (_b = response === null || response === void 0 ? void 0 : response.result) === null || _b === void 0 ? void 0 : _b.answer;
|
|
2067
1852
|
}
|
|
2068
|
-
async runStep(page, stepPrompt, argument = null, options = { useCache: true, stepIdFromServer: undefined, aiModel: this.aiModel }) {
|
|
1853
|
+
async runStep(page, stepPrompt, argument = null, options = { useCache: true, stepIdFromServer: undefined, aiModel: this.aiModel, timeoutConfig: this.timeoutConfig }) {
|
|
2069
1854
|
// Use the aiModel from options if provided, otherwise use the one from constructor
|
|
2070
1855
|
const aiModelToUse = options.aiModel !== undefined ? options.aiModel : this.aiModel;
|
|
2071
1856
|
proboLogger.log(`runStep: ${options.stepIdFromServer ? '#' + options.stepIdFromServer + ' - ' : ''}${stepPrompt}, aiModel: ${aiModelToUse}, pageUrl: ${page.url()}`);
|
|
2072
1857
|
this.setupConsoleLogs(page);
|
|
1858
|
+
const navTracker = NavTracker.getInstance(page);
|
|
1859
|
+
await navTracker.waitForNavigationToStabilize();
|
|
2073
1860
|
// First check if the step exists in the database
|
|
2074
1861
|
let stepId;
|
|
2075
1862
|
if (options.useCache) {
|
|
@@ -2104,22 +1891,21 @@ class Probo {
|
|
|
2104
1891
|
switch (nextInstruction.what_to_do) {
|
|
2105
1892
|
case 'highlight_candidate_elements':
|
|
2106
1893
|
proboLogger.debug('Highlighting candidate elements:', nextInstruction.args.element_types);
|
|
2107
|
-
const
|
|
2108
|
-
const
|
|
1894
|
+
const elementTags = nextInstruction.args.element_types;
|
|
1895
|
+
const { candidates_screenshot_url, candidate_elements } = await this.findAndHighlightCandidateElements(page, elementTags);
|
|
1896
|
+
const cleaned_candidate_elements = candidate_elements.map((elementInfo) => {
|
|
2109
1897
|
elementInfo.element = null;
|
|
2110
1898
|
return elementInfo;
|
|
2111
1899
|
});
|
|
2112
|
-
proboLogger.debug(`Highlighted ${
|
|
2113
|
-
const candidate_elements_screenshot_url = await this.screenshot(page);
|
|
2114
|
-
// proboLogger.log('candidate_elements_screenshot_url:', candidate_elements_screenshot_url);
|
|
1900
|
+
proboLogger.debug(`Highlighted ${cleaned_candidate_elements.length} elements`);
|
|
2115
1901
|
const executed_instruction = {
|
|
2116
1902
|
what_to_do: 'highlight_candidate_elements',
|
|
2117
1903
|
args: {
|
|
2118
1904
|
element_types: nextInstruction.args.element_types
|
|
2119
1905
|
},
|
|
2120
1906
|
result: {
|
|
2121
|
-
highlighted_elements:
|
|
2122
|
-
candidate_elements_screenshot_url:
|
|
1907
|
+
highlighted_elements: cleaned_candidate_elements,
|
|
1908
|
+
candidate_elements_screenshot_url: candidates_screenshot_url
|
|
2123
1909
|
}
|
|
2124
1910
|
};
|
|
2125
1911
|
proboLogger.debug('Executed Instruction:', executed_instruction);
|
|
@@ -2140,7 +1926,7 @@ class Probo {
|
|
|
2140
1926
|
}
|
|
2141
1927
|
async _handleCachedStep(page, stepPrompt, argument) {
|
|
2142
1928
|
proboLogger.debug(`Checking if step exists in database: ${stepPrompt}`);
|
|
2143
|
-
const result = await this.apiClient.findStepByPrompt(stepPrompt, this.scenarioName);
|
|
1929
|
+
const result = await this.apiClient.findStepByPrompt(stepPrompt, this.scenarioName, page.url());
|
|
2144
1930
|
if (result) {
|
|
2145
1931
|
const actionArgument = argument !== null && argument !== void 0 ? argument : result.step.argument;
|
|
2146
1932
|
proboLogger.log(`Found existing step with ID: ${result.step.id} going to perform action: ${result.step.action} with value: ${actionArgument}`);
|
|
@@ -2154,7 +1940,24 @@ class Probo {
|
|
|
2154
1940
|
const iframe_selector = result.step.iframe_selector;
|
|
2155
1941
|
let returnValue;
|
|
2156
1942
|
try {
|
|
2157
|
-
|
|
1943
|
+
// Create ProboPlaywright instance with the page
|
|
1944
|
+
const proboPlaywright = new ProboPlaywright(this.timeoutConfig, page);
|
|
1945
|
+
// Call runStep with the cached action
|
|
1946
|
+
const runStepResult = await proboPlaywright.runStep({
|
|
1947
|
+
iframeSelector: iframe_selector,
|
|
1948
|
+
elementSelector: element_css_selector,
|
|
1949
|
+
action: result.step.action,
|
|
1950
|
+
argument: actionArgument,
|
|
1951
|
+
// Disable highlighting for cached steps since they're already validated
|
|
1952
|
+
highlightTimeout: 0
|
|
1953
|
+
});
|
|
1954
|
+
// Handle return value - runStep returns RunStepResult for EXTRACT_VALUE, void for others
|
|
1955
|
+
if (runStepResult) {
|
|
1956
|
+
returnValue = runStepResult;
|
|
1957
|
+
}
|
|
1958
|
+
else {
|
|
1959
|
+
returnValue = true;
|
|
1960
|
+
}
|
|
2158
1961
|
}
|
|
2159
1962
|
catch (error) {
|
|
2160
1963
|
proboLogger.error(`Error executing action for step ${result.step.id} going to reset the step`);
|
|
@@ -2174,40 +1977,44 @@ class Probo {
|
|
|
2174
1977
|
// not sure if this is needed
|
|
2175
1978
|
// await handlePotentialNavigation(page);
|
|
2176
1979
|
await waitForMutationsToSettle(page);
|
|
2177
|
-
const
|
|
2178
|
-
proboLogger.debug(`Taking initial html content from the page ${page.url()}`);
|
|
2179
|
-
const initial_html_content = await pRetry(async () => {
|
|
2180
|
-
try {
|
|
2181
|
-
return await page.content();
|
|
2182
|
-
}
|
|
2183
|
-
catch (error) {
|
|
2184
|
-
console.error('Error caught:', {
|
|
2185
|
-
name: error.name,
|
|
2186
|
-
message: error.message,
|
|
2187
|
-
code: error.code,
|
|
2188
|
-
constructor: error.constructor.name,
|
|
2189
|
-
prototype: Object.getPrototypeOf(error).constructor.name
|
|
2190
|
-
});
|
|
2191
|
-
throw error; // Re-throw to trigger retry
|
|
2192
|
-
}
|
|
2193
|
-
}, retryOptions);
|
|
1980
|
+
const { base_screenshot_url, base_html_content } = await pRetry(() => this.getInitialPageState(page), retryOptions);
|
|
2194
1981
|
return await this.apiClient.createStep({
|
|
2195
1982
|
stepIdFromServer,
|
|
2196
1983
|
scenarioName: this.scenarioName,
|
|
2197
1984
|
stepPrompt: stepPrompt,
|
|
2198
|
-
initial_screenshot_url,
|
|
2199
|
-
initial_html_content,
|
|
2200
|
-
use_cache: useCache
|
|
1985
|
+
initial_screenshot_url: base_screenshot_url,
|
|
1986
|
+
initial_html_content: base_html_content,
|
|
1987
|
+
use_cache: useCache,
|
|
1988
|
+
url: page.url()
|
|
2201
1989
|
});
|
|
2202
1990
|
}
|
|
2203
1991
|
setupConsoleLogs(page) {
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
1992
|
+
setupBrowserConsoleLogs(page, this.enableConsoleLogs, proboLogger);
|
|
1993
|
+
}
|
|
1994
|
+
async getInitialPageState(page) {
|
|
1995
|
+
const baseScreenshot = await this.screenshot(page);
|
|
1996
|
+
proboLogger.debug(`🔍 baseScreenshot: ${baseScreenshot}`);
|
|
1997
|
+
const baseHtmlContent = '';
|
|
1998
|
+
return {
|
|
1999
|
+
base_screenshot_url: baseScreenshot,
|
|
2000
|
+
base_html_content: baseHtmlContent
|
|
2001
|
+
};
|
|
2002
|
+
}
|
|
2003
|
+
async findAndHighlightCandidateElements(page, elementTags) {
|
|
2004
|
+
// 2. CANDIDATE ELEMENTS HANDLING: Find and cache candidates
|
|
2005
|
+
var _a;
|
|
2006
|
+
await this.highlighter.findAndCacheCandidateElements(page, elementTags);
|
|
2007
|
+
proboLogger.debug(`🔍 after findAndCacheCandidateElements`);
|
|
2008
|
+
// 3. CANDIDATE ELEMENTS HANDLING: Highlight candidates and take screenshot
|
|
2009
|
+
await this.highlighter.highlightCachedElements(page, 'candidates');
|
|
2010
|
+
proboLogger.debug(`🔍 after highlightCachedElements candidates`);
|
|
2011
|
+
await page.waitForTimeout(200);
|
|
2012
|
+
const candidatesScreenshot = await this.screenshot(page);
|
|
2013
|
+
proboLogger.debug(`🔍 candidatesScreenshot: ${candidatesScreenshot}`);
|
|
2014
|
+
return {
|
|
2015
|
+
candidates_screenshot_url: candidatesScreenshot,
|
|
2016
|
+
candidate_elements: (_a = await this.highlighter.getCandidatesCached(page)) !== null && _a !== void 0 ? _a : []
|
|
2017
|
+
};
|
|
2211
2018
|
}
|
|
2212
2019
|
async highlightElements(page, elementTags) {
|
|
2213
2020
|
return this.highlighter.highlightElements(page, elementTags);
|
|
@@ -2218,8 +2025,10 @@ class Probo {
|
|
|
2218
2025
|
async highlightElement(page, element_css_selector, iframe_selector, element_index) {
|
|
2219
2026
|
return this.highlighter.highlightElement(page, element_css_selector, iframe_selector, element_index);
|
|
2220
2027
|
}
|
|
2221
|
-
async waitForMutationsToSettle(page, timeout
|
|
2222
|
-
|
|
2028
|
+
async waitForMutationsToSettle(page, timeout, initTimeout) {
|
|
2029
|
+
const mutationTimeout = timeout !== null && timeout !== void 0 ? timeout : DEFAULT_PLAYWRIGHT_TIMEOUT_CONFIG.mutationsTimeout;
|
|
2030
|
+
const mutationInitTimeout = initTimeout !== null && initTimeout !== void 0 ? initTimeout : DEFAULT_PLAYWRIGHT_TIMEOUT_CONFIG.mutationsInitTimeout;
|
|
2031
|
+
return waitForMutationsToSettle(page, mutationTimeout, mutationInitTimeout);
|
|
2223
2032
|
}
|
|
2224
2033
|
async screenshot(page) {
|
|
2225
2034
|
proboLogger.debug(`taking screenshot of current page: ${page.url()}`);
|
|
@@ -2244,32 +2053,23 @@ class Probo {
|
|
|
2244
2053
|
proboLogger.debug('Highlighted element');
|
|
2245
2054
|
}
|
|
2246
2055
|
const pre_action_screenshot_url = await this.screenshot(page);
|
|
2247
|
-
const returnValue = await
|
|
2056
|
+
const returnValue = await new ProboPlaywright(this.timeoutConfig, page).runStep({
|
|
2057
|
+
iframeSelector: iframe_selector,
|
|
2058
|
+
elementSelector: element_css_selector,
|
|
2059
|
+
action: action,
|
|
2060
|
+
argument: value,
|
|
2061
|
+
highlightTimeout: 0
|
|
2062
|
+
});
|
|
2248
2063
|
let stepStatus;
|
|
2249
|
-
if ([PlaywrightAction.
|
|
2064
|
+
if ([PlaywrightAction.VALIDATE_EXACT_VALUE,
|
|
2250
2065
|
PlaywrightAction.VALIDATE_CONTAINS_VALUE, PlaywrightAction.VALIDATE_URL].includes(action))
|
|
2251
2066
|
stepStatus = returnValue;
|
|
2252
2067
|
else
|
|
2253
2068
|
stepStatus = true;
|
|
2254
2069
|
await this.unhighlightElements(page);
|
|
2255
|
-
proboLogger.debug('UnHighlighted element');
|
|
2256
2070
|
await waitForMutationsToSettle(page);
|
|
2257
2071
|
const post_action_screenshot_url = await this.screenshot(page);
|
|
2258
|
-
const post_html_content =
|
|
2259
|
-
try {
|
|
2260
|
-
return await page.content();
|
|
2261
|
-
}
|
|
2262
|
-
catch (error) {
|
|
2263
|
-
console.error('Error caught:', {
|
|
2264
|
-
name: error.name,
|
|
2265
|
-
message: error.message,
|
|
2266
|
-
code: error.code,
|
|
2267
|
-
constructor: error.constructor.name,
|
|
2268
|
-
prototype: Object.getPrototypeOf(error).constructor.name
|
|
2269
|
-
});
|
|
2270
|
-
throw error; // Re-throw to trigger retry
|
|
2271
|
-
}
|
|
2272
|
-
}, retryOptions);
|
|
2072
|
+
const post_html_content = '';
|
|
2273
2073
|
const executed_instruction = {
|
|
2274
2074
|
what_to_do: 'perform_action',
|
|
2275
2075
|
args: {
|
|
@@ -2291,7 +2091,10 @@ class Probo {
|
|
|
2291
2091
|
async askAIHelper(page, question) {
|
|
2292
2092
|
proboLogger.debug(`🔍 [askAI] Asking AI question: "${question}", scenarioName: ${this.scenarioName}, aiModel: ${this.aiModel}`);
|
|
2293
2093
|
try {
|
|
2294
|
-
|
|
2094
|
+
const navTracker = NavTracker.getInstance(page);
|
|
2095
|
+
await navTracker.waitForNavigationToStabilize();
|
|
2096
|
+
// Get current page and capture screenshot
|
|
2097
|
+
await waitForMutationsToSettle(page);
|
|
2295
2098
|
const screenshot = await this.screenshot(page);
|
|
2296
2099
|
proboLogger.debug(`📸 [askAI] Screenshot captured: ${screenshot}`);
|
|
2297
2100
|
proboLogger.debug(`📤 [askAI] Sending chat request to backend`);
|
|
@@ -2309,5 +2112,5 @@ class Probo {
|
|
|
2309
2112
|
}
|
|
2310
2113
|
// export const highlighterCode = '';
|
|
2311
2114
|
|
|
2312
|
-
export { Highlighter, PlaywrightAction, Probo, ProboLogLevel, ProboPlaywright,
|
|
2115
|
+
export { Highlighter, NavTracker, PlaywrightAction, Probo, ProboLogLevel, ProboPlaywright, createProboFixtures, findClosestVisibleElement, test };
|
|
2313
2116
|
//# sourceMappingURL=index.js.map
|