Last active
November 10, 2024 15:10
-
-
Save synergychen/2f52147f04b8a335eb01b336b21dcc7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class UIElementHelper { | |
| constructor(options = {}) { | |
| this.options = options; | |
| this.defaultTimeout = options.timeout || 5000; | |
| this.defaultDelay = options.delay || 0; | |
| this.defaultSelector = options.selector || '*'; | |
| this.enableHighlight = options.highlight || false; | |
| } | |
| async find({ | |
| match = '', | |
| selector = this.defaultSelector, | |
| timeout = this.defaultTimeout, | |
| }) { | |
| const startTime = Date.now(); | |
| while (Date.now() - startTime < timeout) { | |
| const elements = Array.from(document.querySelectorAll(selector)); | |
| const element = match | |
| ? elements.find((el) => | |
| el.textContent.toLowerCase().includes(match.toLowerCase()) | |
| ) | |
| : elements[0]; | |
| if (element) { | |
| console.log(`Element found with match "${match}"`); | |
| return element; | |
| } | |
| await new Promise((resolve) => setTimeout(resolve, 100)); | |
| } | |
| const errorMessage = `Element with match "${ | |
| match || 'any content' | |
| }" not found within timeout`; | |
| console.error(errorMessage); | |
| throw new Error(errorMessage); | |
| } | |
| async click({ | |
| match = '', | |
| selector = this.defaultSelector, | |
| timeout = this.defaultTimeout, | |
| delay = this.defaultDelay, | |
| }) { | |
| const element = await this.find({ match, selector, timeout }); | |
| if (this.enableHighlight) { | |
| await this.highlight(element); | |
| await new Promise((resolve) => setTimeout(resolve, 2000)); | |
| } | |
| await new Promise((resolve) => setTimeout(resolve, 100)); | |
| console.log( | |
| `Clicking element with match "${match}" and selector "${selector}"` | |
| ); | |
| element.click(); | |
| await new Promise((resolve) => setTimeout(resolve, delay)); | |
| } | |
| async clickAll(elements) { | |
| for (const elOptions of elements) { | |
| console.log(`Processing click for element with options:`, elOptions); | |
| await this.click(elOptions); | |
| } | |
| } | |
| async highlight(element) { | |
| const originalStyle = element.style.border; | |
| if (this.enableHighlight) { | |
| element.style.border = '3px solid yellow'; | |
| console.log(`Highlighting element temporarily`); | |
| await new Promise((resolve) => setTimeout(resolve, 1000)); | |
| element.style.border = originalStyle; | |
| await new Promise((resolve) => setTimeout(resolve, 1000)); | |
| } | |
| } | |
| async fill({ | |
| match = '', | |
| selector = this.defaultSelector, | |
| text, | |
| timeout = this.defaultTimeout, | |
| delay = this.defaultDelay, | |
| }) { | |
| console.log( | |
| `Attempting to fill editable title with text "${text}" for element with match "${match}"` | |
| ); | |
| const element = await this.find({ match, selector, timeout }); | |
| if (this.enableHighlight) await this.highlight(element); | |
| await new Promise((resolve) => setTimeout(resolve, delay)); | |
| element.value = text; | |
| element.dispatchEvent(new Event('input', { bubbles: true })); | |
| console.log(`Filled element with text "${text}"`); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment