MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / relativeMany

Function relativeMany

javascript/atoms/typescript/find-elements.ts:251–289  ·  view source on GitHub ↗
(target: Record<string, unknown>, root: Root)

Source from the content-addressed store, hash-verified

249 }
250
251 function relativeMany(target: Record<string, unknown>, root: Root): Element[] {
252 if (!Object.prototype.hasOwnProperty.call(target, 'root') || !Object.prototype.hasOwnProperty.call(target, 'filters')) {
253 throw botError(INVALID_ARGUMENT, 'Locator not suitable for relative locators: ' + JSON.stringify(target))
254 }
255 const filters = target['filters']
256 if (!Array.isArray(filters)) {
257 throw botError(INVALID_ARGUMENT, 'Targets should be an array: ' + JSON.stringify(target))
258 }
259
260 let elements: Element[]
261 const rootTarget = target['root']
262 if (rootTarget instanceof Element) {
263 elements = [rootTarget]
264 } else {
265 elements = findElements(rootTarget as LocatorTarget, root)
266 }
267
268 if (!elements.length) {
269 return []
270 }
271
272 const matched = elements.filter(el => {
273 if (!el) return false
274 return (filters as RelativeFilter[]).every(filter => {
275 const strategy = RELATIVE_STRATEGIES[filter.kind]
276 if (!strategy) {
277 throw botError(INVALID_ARGUMENT, 'Cannot find filter suitable for ' + filter.kind)
278 }
279 return strategy(...filter.args)(el)
280 })
281 })
282
283 const finalFilter = filters[filters.length - 1] as RelativeFilter | undefined
284 if (!finalFilter || !RELATIVE_STRATEGIES[finalFilter.kind]) {
285 return matched
286 }
287 const lastAnchor = resolveAnchor(finalFilter.args[0])
288 return sortByProximity(lastAnchor, matched)
289 }
290
291 const actualRoot: Root = root || document
292 const keys = Object.keys(target).filter(k => Object.prototype.hasOwnProperty.call(target, k))

Callers 1

find-elements.tsFile · 0.85

Calls 5

botErrorFunction · 0.85
resolveAnchorFunction · 0.85
sortByProximityFunction · 0.85
filterMethod · 0.80
callMethod · 0.65

Tested by

no test coverage detected