@selenium/atoms 0.0.0-stage → 4.51.0-nightly202609302012
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/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +64 -2
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/typescript/find-elements.d.ts +3 -0
- package/dist/typescript/find-elements.d.ts.map +1 -0
- package/dist/typescript/find-elements.js +306 -0
- package/dist/typescript/find-elements.js.map +1 -0
- package/dist/typescript/get-attribute.d.ts +13 -0
- package/dist/typescript/get-attribute.d.ts.map +1 -0
- package/dist/typescript/get-attribute.js +181 -0
- package/dist/typescript/get-attribute.js.map +1 -0
- package/dist/typescript/is-displayed.d.ts +3 -0
- package/dist/typescript/is-displayed.d.ts.map +1 -0
- package/dist/typescript/is-displayed.js +413 -0
- package/dist/typescript/is-displayed.js.map +1 -0
- package/index.ts +23 -0
- package/package.json +49 -4
- package/typescript/find-elements.ts +343 -0
- package/typescript/get-attribute.ts +199 -0
- package/typescript/is-displayed.ts +515 -0
package/index.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Licensed to the Software Freedom Conservancy (SFC) under one
|
|
2
|
+
// or more contributor license agreements. See the NOTICE file
|
|
3
|
+
// distributed with this work for additional information
|
|
4
|
+
// regarding copyright ownership. The SFC licenses this file
|
|
5
|
+
// to you under the Apache License, Version 2.0 (the
|
|
6
|
+
// "License"); you may not use this file except in compliance
|
|
7
|
+
// with the License. You may obtain a copy of the License at
|
|
8
|
+
//
|
|
9
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
//
|
|
11
|
+
// Unless required by applicable law or agreed to in writing,
|
|
12
|
+
// software distributed under the License is distributed on an
|
|
13
|
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
// KIND, either express or implied. See the License for the
|
|
15
|
+
// specific language governing permissions and limitations
|
|
16
|
+
// under the License.
|
|
17
|
+
|
|
18
|
+
// Specifiers carry the emitted .js extension because Node's ESM resolver does
|
|
19
|
+
// not probe for extensions. "module": "esnext" rewrites nothing, so what is
|
|
20
|
+
// written here is what consumers resolve against.
|
|
21
|
+
export { default as getAttribute } from './typescript/get-attribute.js'
|
|
22
|
+
export { default as findElements } from './typescript/find-elements.js'
|
|
23
|
+
export { default as isDisplayed } from './typescript/is-displayed.js'
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@selenium/atoms",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "4.51.0-nightly202609302012",
|
|
4
|
+
"description": "Browser injection atoms for Selenium WebDriver",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./get-attribute": {
|
|
15
|
+
"types": "./dist/typescript/get-attribute.d.ts",
|
|
16
|
+
"default": "./dist/typescript/get-attribute.js"
|
|
17
|
+
},
|
|
18
|
+
"./find-elements": {
|
|
19
|
+
"types": "./dist/typescript/find-elements.d.ts",
|
|
20
|
+
"default": "./dist/typescript/find-elements.js"
|
|
21
|
+
},
|
|
22
|
+
"./is-displayed": {
|
|
23
|
+
"types": "./dist/typescript/is-displayed.d.ts",
|
|
24
|
+
"default": "./dist/typescript/is-displayed.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"typesVersions": {
|
|
28
|
+
"*": {
|
|
29
|
+
"get-attribute": ["./dist/typescript/get-attribute.d.ts"],
|
|
30
|
+
"find-elements": ["./dist/typescript/find-elements.d.ts"],
|
|
31
|
+
"is-displayed": ["./dist/typescript/is-displayed.d.ts"]
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">= 22.12.0"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist/",
|
|
39
|
+
"index.ts",
|
|
40
|
+
"typescript/",
|
|
41
|
+
"LICENSE",
|
|
42
|
+
"NOTICE",
|
|
43
|
+
"README.md"
|
|
44
|
+
],
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "https://github.com/SeleniumHQ/selenium.git",
|
|
48
|
+
"directory": "javascript/atoms"
|
|
49
|
+
},
|
|
50
|
+
"license": "Apache-2.0"
|
|
51
|
+
}
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
// Licensed to the Software Freedom Conservancy (SFC) under one
|
|
2
|
+
// or more contributor license agreements. See the NOTICE file
|
|
3
|
+
// distributed with this work for additional information
|
|
4
|
+
// regarding copyright ownership. The SFC licenses this file
|
|
5
|
+
// to you under the Apache License, Version 2.0 (the
|
|
6
|
+
// "License"); you may not use this file except in compliance
|
|
7
|
+
// with the License. You may obtain a copy of the License at
|
|
8
|
+
//
|
|
9
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
//
|
|
11
|
+
// Unless required by applicable law or agreed to in writing,
|
|
12
|
+
// software distributed under the License is distributed on an
|
|
13
|
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
// KIND, either express or implied. See the License for the
|
|
15
|
+
// specific language governing permissions and limitations
|
|
16
|
+
// under the License.
|
|
17
|
+
|
|
18
|
+
function findElements(target: Record<string, unknown>, root?: Document | Element | ShadowRoot): Element[] {
|
|
19
|
+
type LocatorTarget = Record<string, unknown>
|
|
20
|
+
type Root = Document | Element | ShadowRoot
|
|
21
|
+
type Rect = { left: number; top: number; width: number; height: number }
|
|
22
|
+
type RelativeFilter = { kind: string; args: unknown[] }
|
|
23
|
+
|
|
24
|
+
const INVALID_SELECTOR = 'invalid selector'
|
|
25
|
+
const INVALID_ARGUMENT = 'invalid argument'
|
|
26
|
+
const NO_SUCH_ELEMENT = 'no such element'
|
|
27
|
+
|
|
28
|
+
function botError(code: string, message: string): Error {
|
|
29
|
+
const err = new Error(message)
|
|
30
|
+
;(err as unknown as Record<string, unknown>)['code'] = code
|
|
31
|
+
return err
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function cssEscapeId(s: string): string {
|
|
35
|
+
return s.replace(/[\s'"\\#.:;,!?+<>=~*^$|%&@`{}\-/\[\]()]/g, (c) => '\\' + c)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function classNameMany(target: string, root: Root): Element[] {
|
|
39
|
+
if (!target) {
|
|
40
|
+
throw botError(INVALID_SELECTOR, 'No class name specified')
|
|
41
|
+
}
|
|
42
|
+
target = target.trim()
|
|
43
|
+
if (target.indexOf(' ') !== -1) {
|
|
44
|
+
throw botError(INVALID_SELECTOR, 'Compound class names not permitted')
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
return Array.from(root.querySelectorAll('.' + target.replace(/\\/g, '\\\\').replace(/\./g, '\\.')))
|
|
48
|
+
} catch (_e) {
|
|
49
|
+
throw botError(INVALID_SELECTOR, 'An invalid or illegal class name was specified')
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function cssMany(target: string, root: Root): Element[] {
|
|
54
|
+
try {
|
|
55
|
+
return Array.from(root.querySelectorAll(target))
|
|
56
|
+
} catch (_e) {
|
|
57
|
+
throw botError(INVALID_SELECTOR, 'An invalid or illegal CSS selector was specified: ' + target)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function idMany(target: string, root: Root): Element[] {
|
|
62
|
+
if (!target) {
|
|
63
|
+
return []
|
|
64
|
+
}
|
|
65
|
+
if (!/^\d/.test(target)) {
|
|
66
|
+
try {
|
|
67
|
+
return Array.from(root.querySelectorAll('#' + cssEscapeId(target)))
|
|
68
|
+
} catch (_e) {
|
|
69
|
+
return []
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return Array.from(root.querySelectorAll('*')).filter(el => el.getAttribute('id') === target)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function getLinkText(el: Element): string {
|
|
76
|
+
const text = (el as HTMLElement).innerText !== undefined ? (el as HTMLElement).innerText : el.textContent || ''
|
|
77
|
+
return text.replace(/^[\s]+|[\s]+$/g, '')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function linkTextMany(target: string, root: Root, partial: boolean): Element[] {
|
|
81
|
+
return Array.from(root.querySelectorAll('a')).filter(el => {
|
|
82
|
+
const text = getLinkText(el)
|
|
83
|
+
return partial ? text.indexOf(target) !== -1 : text === target
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function nameMany(target: string, root: Root): Element[] {
|
|
88
|
+
return Array.from(root.querySelectorAll('[name="' + target.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"]'))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function tagNameMany(target: string, root: Root): Element[] {
|
|
92
|
+
if (target === '') {
|
|
93
|
+
throw botError(INVALID_SELECTOR, 'Unable to locate an element with the tagName ""')
|
|
94
|
+
}
|
|
95
|
+
if ('getElementsByTagName' in root) {
|
|
96
|
+
return Array.from(root.getElementsByTagName(target))
|
|
97
|
+
}
|
|
98
|
+
const elements = Array.from(root.querySelectorAll('*'))
|
|
99
|
+
if (target === '*') {
|
|
100
|
+
return elements
|
|
101
|
+
}
|
|
102
|
+
const html = 'http://www.w3.org/1999/xhtml'
|
|
103
|
+
const folded = target.toLowerCase()
|
|
104
|
+
return elements.filter(el =>
|
|
105
|
+
el.namespaceURI === html ? el.localName === folded : el.localName === target)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const DEFAULT_NS_RESOLVER = (function () {
|
|
109
|
+
const ns: Record<string, string> = { svg: 'http://www.w3.org/2000/svg' }
|
|
110
|
+
return (prefix: string): string | null => ns[prefix] || null
|
|
111
|
+
})()
|
|
112
|
+
|
|
113
|
+
const ORDERED_NODE_SNAPSHOT_TYPE = 7
|
|
114
|
+
|
|
115
|
+
function xpathMany(target: string, root: Root): Element[] {
|
|
116
|
+
const doc = (root as Document).documentElement ? (root as Document) : root.ownerDocument!
|
|
117
|
+
if (!doc.documentElement) {
|
|
118
|
+
return []
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
// Namespace prefixes require a colon in the XPath expression. Skip the
|
|
122
|
+
// expensive full-DOM scan when the expression contains no colon at all.
|
|
123
|
+
let resolver: ((prefix: string | null) => string | null) | null = null
|
|
124
|
+
if (target.indexOf(':') !== -1) {
|
|
125
|
+
const reversedNs: Record<string, string> = {}
|
|
126
|
+
const allNodes = doc.getElementsByTagName('*')
|
|
127
|
+
for (let i = 0; i < allNodes.length; i++) {
|
|
128
|
+
const n = allNodes[i]
|
|
129
|
+
const ns = n.namespaceURI
|
|
130
|
+
if (ns && !reversedNs[ns]) {
|
|
131
|
+
let prefix = n.lookupPrefix(ns)
|
|
132
|
+
if (!prefix) {
|
|
133
|
+
const m = ns.match('.*/(\\w+)/?$')
|
|
134
|
+
prefix = m ? m[1] : 'xhtml'
|
|
135
|
+
}
|
|
136
|
+
reversedNs[ns] = prefix!
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const namespaces: Record<string, string> = {}
|
|
140
|
+
for (const key in reversedNs) {
|
|
141
|
+
namespaces[reversedNs[key]] = key
|
|
142
|
+
}
|
|
143
|
+
resolver = (prefix: string | null): string | null => namespaces[prefix || ''] || null
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let result: XPathResult | null = null
|
|
147
|
+
try {
|
|
148
|
+
result = doc.evaluate(target, root, resolver, ORDERED_NODE_SNAPSHOT_TYPE, null)
|
|
149
|
+
} catch (te) {
|
|
150
|
+
if ((te as Error).name === 'TypeError') {
|
|
151
|
+
const fallback = doc.createNSResolver ? doc.createNSResolver(doc.documentElement!) : DEFAULT_NS_RESOLVER
|
|
152
|
+
result = doc.evaluate(target, root, fallback, ORDERED_NODE_SNAPSHOT_TYPE, null)
|
|
153
|
+
} else {
|
|
154
|
+
throw te
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (!result) {
|
|
159
|
+
return []
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const results: Element[] = []
|
|
163
|
+
for (let i = 0; i < result.snapshotLength; i++) {
|
|
164
|
+
const node = result.snapshotItem(i)
|
|
165
|
+
if (!node || node.nodeType !== Node.ELEMENT_NODE) {
|
|
166
|
+
throw botError(
|
|
167
|
+
INVALID_SELECTOR,
|
|
168
|
+
'The result of the xpath expression "' + target + '" is: ' + node + '. It should be an element.',
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
results.push(node as Element)
|
|
172
|
+
}
|
|
173
|
+
return results
|
|
174
|
+
} catch (ex) {
|
|
175
|
+
if ((ex as Error & { name?: string }).name === 'NS_ERROR_ILLEGAL_VALUE') {
|
|
176
|
+
return []
|
|
177
|
+
}
|
|
178
|
+
if ((ex as Error & { code?: string }).code === INVALID_SELECTOR) {
|
|
179
|
+
throw ex
|
|
180
|
+
}
|
|
181
|
+
throw botError(
|
|
182
|
+
INVALID_SELECTOR,
|
|
183
|
+
'Unable to locate an element with the xpath expression ' + target + ' because of the following error:\n' + ex,
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function getClientRect(element: Element): Rect {
|
|
189
|
+
const r = element.getBoundingClientRect()
|
|
190
|
+
return { left: r.left, top: r.top, width: r.width, height: r.height }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function resolveAnchor(selector: unknown): Element {
|
|
194
|
+
if (selector instanceof Element) {
|
|
195
|
+
return selector
|
|
196
|
+
}
|
|
197
|
+
if (typeof selector === 'function') {
|
|
198
|
+
return resolveAnchor((selector as () => unknown)())
|
|
199
|
+
}
|
|
200
|
+
if (selector && typeof selector === 'object') {
|
|
201
|
+
// Prefer an anchor inside the root, since a document search cannot see into a shadow root,
|
|
202
|
+
// but an anchor is only a reference point and may live anywhere on the page.
|
|
203
|
+
let found = root ? findElements(selector as LocatorTarget, root) : []
|
|
204
|
+
if (!found.length) {
|
|
205
|
+
found = findElements(selector as LocatorTarget)
|
|
206
|
+
}
|
|
207
|
+
if (!found.length) {
|
|
208
|
+
throw botError(NO_SUCH_ELEMENT, 'No element has been found by ' + JSON.stringify(selector))
|
|
209
|
+
}
|
|
210
|
+
return found[0]
|
|
211
|
+
}
|
|
212
|
+
throw botError(INVALID_ARGUMENT, 'Selector is of wrong type: ' + JSON.stringify(selector))
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function makeProximityFilter(selector: unknown, test: (anchor: Rect, candidate: Rect) => boolean) {
|
|
216
|
+
return (candidate: Element): boolean => test(getClientRect(resolveAnchor(selector)), getClientRect(candidate))
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const RELATIVE_STRATEGIES: Record<string, (...args: unknown[]) => (el: Element) => boolean> = {
|
|
220
|
+
above: (sel) => makeProximityFilter(sel, (a, c) => c.top + c.height <= a.top),
|
|
221
|
+
below: (sel) => makeProximityFilter(sel, (a, c) => c.top >= a.top + a.height),
|
|
222
|
+
left: (sel) => makeProximityFilter(sel, (a, c) => c.left + c.width <= a.left),
|
|
223
|
+
right: (sel) => makeProximityFilter(sel, (a, c) => c.left >= a.left + a.width),
|
|
224
|
+
near: (sel, distArg) => {
|
|
225
|
+
const distFromSelector = typeof (sel as Record<string, unknown>)['distance'] === 'number'
|
|
226
|
+
? ((sel as Record<string, unknown>)['distance'] as number)
|
|
227
|
+
: 0
|
|
228
|
+
const distance = typeof distArg === 'number' && distArg > 0 ? distArg : distFromSelector || 50
|
|
229
|
+
return (candidate: Element): boolean => {
|
|
230
|
+
const anchor = resolveAnchor(sel)
|
|
231
|
+
if (anchor === candidate) return false
|
|
232
|
+
const a = getClientRect(anchor)
|
|
233
|
+
const c = getClientRect(candidate)
|
|
234
|
+
const expanded = { left: a.left - distance, top: a.top - distance, width: a.width + distance * 2, height: a.height + distance * 2 }
|
|
235
|
+
return (
|
|
236
|
+
c.left < expanded.left + expanded.width &&
|
|
237
|
+
c.left + c.width > expanded.left &&
|
|
238
|
+
c.top < expanded.top + expanded.height &&
|
|
239
|
+
c.top + c.height > expanded.top
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
},
|
|
243
|
+
straightAbove: (sel) =>
|
|
244
|
+
makeProximityFilter(sel, (a, c) => c.left < a.left + a.width && c.left + c.width > a.left && c.top + c.height <= a.top),
|
|
245
|
+
straightBelow: (sel) =>
|
|
246
|
+
makeProximityFilter(sel, (a, c) => c.left < a.left + a.width && c.left + c.width > a.left && c.top >= a.top + a.height),
|
|
247
|
+
straightLeft: (sel) =>
|
|
248
|
+
makeProximityFilter(sel, (a, c) => c.top < a.top + a.height && c.top + c.height > a.top && c.left + c.width <= a.left),
|
|
249
|
+
straightRight: (sel) =>
|
|
250
|
+
makeProximityFilter(sel, (a, c) => c.top < a.top + a.height && c.top + c.height > a.top && c.left >= a.left + a.width),
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function sortByProximity(anchor: Element, elements: Element[]): Element[] {
|
|
254
|
+
const ar = getClientRect(anchor)
|
|
255
|
+
const acx = ar.left + Math.max(1, ar.width) / 2
|
|
256
|
+
const acy = ar.top + Math.max(1, ar.height) / 2
|
|
257
|
+
return elements.slice().sort((a, b) => {
|
|
258
|
+
const ra = getClientRect(a)
|
|
259
|
+
const rb = getClientRect(b)
|
|
260
|
+
const da = Math.sqrt(Math.pow(acx - (ra.left + Math.max(1, ra.width) / 2), 2) + Math.pow(acy - (ra.top + Math.max(1, ra.height) / 2), 2))
|
|
261
|
+
const db = Math.sqrt(Math.pow(acx - (rb.left + Math.max(1, rb.width) / 2), 2) + Math.pow(acy - (rb.top + Math.max(1, rb.height) / 2), 2))
|
|
262
|
+
return da - db
|
|
263
|
+
})
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function relativeMany(target: Record<string, unknown>, root: Root): Element[] {
|
|
267
|
+
if (!Object.prototype.hasOwnProperty.call(target, 'root') || !Object.prototype.hasOwnProperty.call(target, 'filters')) {
|
|
268
|
+
throw botError(INVALID_ARGUMENT, 'Locator not suitable for relative locators: ' + JSON.stringify(target))
|
|
269
|
+
}
|
|
270
|
+
const filters = target['filters']
|
|
271
|
+
if (!Array.isArray(filters)) {
|
|
272
|
+
throw botError(INVALID_ARGUMENT, 'Targets should be an array: ' + JSON.stringify(target))
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
let elements: Element[]
|
|
276
|
+
const rootTarget = target['root']
|
|
277
|
+
if (rootTarget instanceof Element) {
|
|
278
|
+
elements = [rootTarget]
|
|
279
|
+
} else {
|
|
280
|
+
elements = findElements(rootTarget as LocatorTarget, root)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (!elements.length) {
|
|
284
|
+
return []
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const matched = elements.filter(el => {
|
|
288
|
+
if (!el) return false
|
|
289
|
+
return (filters as RelativeFilter[]).every(filter => {
|
|
290
|
+
const strategy = RELATIVE_STRATEGIES[filter.kind]
|
|
291
|
+
if (!strategy) {
|
|
292
|
+
throw botError(INVALID_ARGUMENT, 'Cannot find filter suitable for ' + filter.kind)
|
|
293
|
+
}
|
|
294
|
+
return strategy(...filter.args)(el)
|
|
295
|
+
})
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
const finalFilter = filters[filters.length - 1] as RelativeFilter | undefined
|
|
299
|
+
if (!finalFilter || !RELATIVE_STRATEGIES[finalFilter.kind]) {
|
|
300
|
+
return matched
|
|
301
|
+
}
|
|
302
|
+
const lastAnchor = resolveAnchor(finalFilter.args[0])
|
|
303
|
+
return sortByProximity(lastAnchor, matched)
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const actualRoot: Root = root || document
|
|
307
|
+
const keys = Object.keys(target).filter(k => Object.prototype.hasOwnProperty.call(target, k))
|
|
308
|
+
if (!keys.length) {
|
|
309
|
+
throw botError(INVALID_ARGUMENT, 'Unsupported locator strategy: (empty)')
|
|
310
|
+
}
|
|
311
|
+
const key = keys[0]
|
|
312
|
+
const value = target[key]
|
|
313
|
+
|
|
314
|
+
switch (key) {
|
|
315
|
+
case 'className':
|
|
316
|
+
case 'class name':
|
|
317
|
+
return classNameMany(value as string, actualRoot)
|
|
318
|
+
case 'css':
|
|
319
|
+
case 'css selector':
|
|
320
|
+
return cssMany(value as string, actualRoot)
|
|
321
|
+
case 'id':
|
|
322
|
+
return idMany(value as string, actualRoot)
|
|
323
|
+
case 'linkText':
|
|
324
|
+
case 'link text':
|
|
325
|
+
return linkTextMany(value as string, actualRoot, false)
|
|
326
|
+
case 'partialLinkText':
|
|
327
|
+
case 'partial link text':
|
|
328
|
+
return linkTextMany(value as string, actualRoot, true)
|
|
329
|
+
case 'name':
|
|
330
|
+
return nameMany(value as string, actualRoot)
|
|
331
|
+
case 'tagName':
|
|
332
|
+
case 'tag name':
|
|
333
|
+
return tagNameMany(value as string, actualRoot)
|
|
334
|
+
case 'xpath':
|
|
335
|
+
return xpathMany(value as string, actualRoot)
|
|
336
|
+
case 'relative':
|
|
337
|
+
return relativeMany(value as Record<string, unknown>, actualRoot)
|
|
338
|
+
default:
|
|
339
|
+
throw botError(INVALID_ARGUMENT, 'Unsupported locator strategy: ' + key)
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
export default findElements;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// Licensed to the Software Freedom Conservancy (SFC) under one
|
|
2
|
+
// or more contributor license agreements. See the NOTICE file
|
|
3
|
+
// distributed with this work for additional information
|
|
4
|
+
// regarding copyright ownership. The SFC licenses this file
|
|
5
|
+
// to you under the Apache License, Version 2.0 (the
|
|
6
|
+
// "License"); you may not use this file except in compliance
|
|
7
|
+
// with the License. You may obtain a copy of the License at
|
|
8
|
+
//
|
|
9
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
//
|
|
11
|
+
// Unless required by applicable law or agreed to in writing,
|
|
12
|
+
// software distributed under the License is distributed on an
|
|
13
|
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
// KIND, either express or implied. See the License for the
|
|
15
|
+
// specific language governing permissions and limitations
|
|
16
|
+
// under the License.
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Get the value of the given property or attribute. If the "attribute" is for
|
|
20
|
+
* a boolean property, we return null in the case where the value is false. If
|
|
21
|
+
* the attribute name is "style" an attempt to convert that style into a string
|
|
22
|
+
* is done.
|
|
23
|
+
*
|
|
24
|
+
* @param element The element to use.
|
|
25
|
+
* @param attribute The name of the attribute to look up.
|
|
26
|
+
* @return The string value of the attribute or property, or null.
|
|
27
|
+
*/
|
|
28
|
+
function get(element: Element, attribute: string): string | null {
|
|
29
|
+
const PROPERTY_ALIASES: Record<string, string> = {
|
|
30
|
+
'class': 'className',
|
|
31
|
+
'readonly': 'readOnly',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const BOOLEAN_PROPERTIES: Set<string> = new Set([
|
|
35
|
+
'allowfullscreen',
|
|
36
|
+
'allowpaymentrequest',
|
|
37
|
+
'allowusermedia',
|
|
38
|
+
'async',
|
|
39
|
+
'autofocus',
|
|
40
|
+
'autoplay',
|
|
41
|
+
'checked',
|
|
42
|
+
'compact',
|
|
43
|
+
'complete',
|
|
44
|
+
'controls',
|
|
45
|
+
'declare',
|
|
46
|
+
'default',
|
|
47
|
+
'defaultchecked',
|
|
48
|
+
'defaultselected',
|
|
49
|
+
'defer',
|
|
50
|
+
'disabled',
|
|
51
|
+
'ended',
|
|
52
|
+
'formnovalidate',
|
|
53
|
+
'hidden',
|
|
54
|
+
'indeterminate',
|
|
55
|
+
'iscontenteditable',
|
|
56
|
+
'ismap',
|
|
57
|
+
'itemscope',
|
|
58
|
+
'loop',
|
|
59
|
+
'multiple',
|
|
60
|
+
'muted',
|
|
61
|
+
'nohref',
|
|
62
|
+
'nomodule',
|
|
63
|
+
'noresize',
|
|
64
|
+
'noshade',
|
|
65
|
+
'novalidate',
|
|
66
|
+
'nowrap',
|
|
67
|
+
'open',
|
|
68
|
+
'paused',
|
|
69
|
+
'playsinline',
|
|
70
|
+
'pubdate',
|
|
71
|
+
'readonly',
|
|
72
|
+
'required',
|
|
73
|
+
'reversed',
|
|
74
|
+
'scoped',
|
|
75
|
+
'seamless',
|
|
76
|
+
'seeking',
|
|
77
|
+
'selected',
|
|
78
|
+
'truespeed',
|
|
79
|
+
'typemustmatch',
|
|
80
|
+
'willvalidate',
|
|
81
|
+
]);
|
|
82
|
+
|
|
83
|
+
function getAttribute(element: Element, attributeName: string): string | null {
|
|
84
|
+
return element.getAttribute(attributeName.toLowerCase());
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getProperty(element: Element, propertyName: string): unknown {
|
|
88
|
+
return (element as unknown as Record<string, unknown>)[propertyName];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function isElement(node: unknown, tagName?: string): node is Element {
|
|
92
|
+
const elem = node as { nodeType?: number; tagName?: string } | null;
|
|
93
|
+
if (!elem || elem.nodeType !== 1) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
if (!tagName) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
const normalizedTag = tagName.toUpperCase();
|
|
100
|
+
if (node instanceof HTMLFormElement) {
|
|
101
|
+
return normalizedTag === 'FORM';
|
|
102
|
+
}
|
|
103
|
+
return typeof elem.tagName === 'string' && elem.tagName.toUpperCase() === normalizedTag;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function isSelectable(element: Element): boolean {
|
|
107
|
+
if (isElement(element, 'OPTION')) {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
if (isElement(element, 'INPUT')) {
|
|
111
|
+
const type = (element as HTMLInputElement).type.toLowerCase();
|
|
112
|
+
return type === 'checkbox' || type === 'radio';
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function isSelected(element: Element): boolean {
|
|
118
|
+
if (isElement(element, 'OPTION')) {
|
|
119
|
+
return (element as HTMLOptionElement).selected;
|
|
120
|
+
}
|
|
121
|
+
const type = (element as HTMLInputElement).type?.toLowerCase();
|
|
122
|
+
if (type === 'checkbox' || type === 'radio') {
|
|
123
|
+
return (element as HTMLInputElement).checked;
|
|
124
|
+
}
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function isObject(value: unknown): boolean {
|
|
129
|
+
return value !== null && (typeof value === 'object' || typeof value === 'function');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const name = attribute.toLowerCase();
|
|
133
|
+
|
|
134
|
+
if (name === 'style') {
|
|
135
|
+
const style = (element as HTMLElement).style;
|
|
136
|
+
if (!style) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
return typeof style === 'string' ? style : style.cssText;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if ((name === 'selected' || name === 'checked') && isSelectable(element)) {
|
|
143
|
+
return isSelected(element) ? 'true' : null;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const isLink = isElement(element, 'A');
|
|
147
|
+
const isImg = isElement(element, 'IMG');
|
|
148
|
+
|
|
149
|
+
if ((isImg && name === 'src') || (isLink && name === 'href')) {
|
|
150
|
+
const attrValue = getAttribute(element, name);
|
|
151
|
+
if (attrValue) {
|
|
152
|
+
return String(getProperty(element, name));
|
|
153
|
+
}
|
|
154
|
+
return attrValue;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (name === 'spellcheck') {
|
|
158
|
+
const attrValue = getAttribute(element, name);
|
|
159
|
+
if (attrValue !== null) {
|
|
160
|
+
const lower = attrValue.toLowerCase();
|
|
161
|
+
if (lower === 'false') {
|
|
162
|
+
return 'false';
|
|
163
|
+
}
|
|
164
|
+
if (lower === 'true') {
|
|
165
|
+
return 'true';
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return String(getProperty(element, name));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const propName = PROPERTY_ALIASES[name] || attribute;
|
|
172
|
+
|
|
173
|
+
if (BOOLEAN_PROPERTIES.has(name)) {
|
|
174
|
+
const hasAttr = element.getAttribute(name) !== null;
|
|
175
|
+
const propValue = getProperty(element, propName);
|
|
176
|
+
return hasAttr || !!propValue ? 'true' : null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (name === 'value' && isElement(element, 'LI')) {
|
|
180
|
+
const attrValue = element.getAttribute(name);
|
|
181
|
+
return attrValue != null ? attrValue : null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let property: unknown;
|
|
185
|
+
try {
|
|
186
|
+
property = getProperty(element, propName);
|
|
187
|
+
} catch (_e) {
|
|
188
|
+
// getAttribute below will be used as fallback
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (property == null || isObject(property)) {
|
|
192
|
+
const attrValue = element.getAttribute(name);
|
|
193
|
+
return attrValue != null ? attrValue : null;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return property != null ? String(property) : null;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export default get;
|