MCPcopy
hub / github.com/codeceptjs/CodeceptJS / selectElement

Function selectElement

lib/helper/extras/elementSelection.js:18–56  ·  view source on GitHub ↗
(els, locator, helper)

Source from the content-addressed store, hash-verified

16}
17
18function selectElement(els, locator, helper) {
19 const opts = store.currentStep?.opts
20 const rawIndex = opts?.elementIndex
21 const elementIndex = resolveElementIndex(rawIndex)
22
23 if (elementIndex != null) {
24 if (els.length === 1) return els[0]
25
26 if (!Number.isInteger(elementIndex) || elementIndex === 0) {
27 throw new Error(`elementIndex must be a non-zero integer or 'first'/'last', got: ${rawIndex}`)
28 }
29
30 let idx
31 if (elementIndex > 0) {
32 idx = elementIndex - 1
33 if (idx >= els.length) {
34 throw new Error(`elementIndex ${elementIndex} exceeds the number of elements found (${els.length}) for "${locator}"`)
35 }
36 } else {
37 idx = els.length + elementIndex
38 if (idx < 0) {
39 throw new Error(`elementIndex ${elementIndex} exceeds the number of elements found (${els.length}) for "${locator}"`)
40 }
41 }
42
43 output.debug(`[Elements] Using element #${elementIndex} out of ${els.length}`)
44 return els[idx]
45 }
46
47 if (isStrictStep(opts, helper)) {
48 if (els.length > 1) {
49 const webElements = Array.from(els).map(el => new WebElement(el, helper))
50 throw new MultipleElementsFound(locator, webElements)
51 }
52 }
53
54 if (els.length > 1) output.debug(`[Elements] Using first element out of ${els.length}`)
55 return els[0]
56}
57
58export { selectElement }

Callers 15

_locateElementMethod · 0.90
_locateCheckableMethod · 0.90
fillFieldMethod · 0.90
appendFieldMethod · 0.90
attachFileMethod · 0.90
selectOptionMethod · 0.90
proceedClickFunction · 0.90
clickMethod · 0.90
forceClickMethod · 0.90
doubleClickMethod · 0.90
rightClickMethod · 0.90
fillFieldMethod · 0.90

Calls 3

resolveElementIndexFunction · 0.85
isStrictStepFunction · 0.85
debugMethod · 0.80

Tested by

no test coverage detected