Skip to content

Commit bf36c75

Browse files
cp
1 parent 9e947ea commit bf36c75

File tree

2 files changed

+184
-50
lines changed

2 files changed

+184
-50
lines changed

javascript/webdriver-atoms-ts/src/find-element-wrapper.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,42 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
import { findElements } from './inject/find_element';
18+
import { findElements as findElementsByInjected } from './inject/find_element';
19+
import * as relativeLocator from './locators/strategies/relative';
1920

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+
}
2256
};

0 commit comments

Comments
 (0)