|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
18 | | -import { findElements } from './inject/find_element'; |
| 18 | +import { findElements as findElementsByInjected } from './inject/find_element'; |
| 19 | +import * as relativeLocator from './locators/strategies/relative'; |
19 | 20 |
|
20 | | -(globalThis as any).__findElements__ = (strategy: string, using: string) => { |
21 | | - return findElements(strategy, using); |
| 21 | +/** |
| 22 | + * Wrapper function for the find elements bundle. |
| 23 | + * Handles both standard locators (strategy/using format) and relative locators (RelativeBy format). |
| 24 | + * When used with execute_script, returns just the element array (not wrapped in a response object). |
| 25 | + */ |
| 26 | +(globalThis as any).__findElements__ = (locatorJson: any) => { |
| 27 | + try { |
| 28 | + // Parse if string |
| 29 | + const locator = typeof locatorJson === 'string' ? JSON.parse(locatorJson) : locatorJson; |
| 30 | + |
| 31 | + // Check if this is a RelativeBy locator |
| 32 | + if (locator && typeof locator === 'object' && 'relative' in locator) { |
| 33 | + // Use relative locator strategy |
| 34 | + const elements = relativeLocator.many(locator.relative, document); |
| 35 | + // When returning elements through execute_script, they are automatically wrapped by WebDriver |
| 36 | + // So we just need to return the element references |
| 37 | + return elements; |
| 38 | + } else if (locator && 'using' in locator && 'value' in locator) { |
| 39 | + // Standard WebDriver locator format (using, value) |
| 40 | + const responseStr = findElementsByInjected(locator.using, locator.value); |
| 41 | + const response = JSON.parse(responseStr); |
| 42 | + if (response.status === 0) { |
| 43 | + return response.value; |
| 44 | + } else { |
| 45 | + throw new Error(response.value?.message || 'Unknown error'); |
| 46 | + } |
| 47 | + } else { |
| 48 | + throw new Error('Invalid locator format: must be either {using, value} or {relative: {root, filters}}'); |
| 49 | + } |
| 50 | + } catch (e) { |
| 51 | + if (e instanceof Error) { |
| 52 | + throw e; |
| 53 | + } |
| 54 | + throw new Error(String(e)); |
| 55 | + } |
22 | 56 | }; |
0 commit comments