-
Notifications
You must be signed in to change notification settings - Fork 8
/
mouse-actions-api.function.ts
186 lines (167 loc) · 7.1 KB
/
mouse-actions-api.function.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import {Button, By, ILocation, ThenableWebDriver, WebElement} from "selenium-webdriver";
import {TestExecutionContext} from "@sakuli/core";
import {stripIndents} from "common-tags";
import 'selenium-webdriver/lib/input'
import {MouseActionApi} from "./mouse-action-api.interface";
import {SahiElementQueryOrWebElement} from "../../sahi-element.interface";
import {runActionsWithComboKeys} from "../run-actions-with-combo.keys.function";
import {AccessorUtil} from "../../accessor";
import {positionalInfo} from "../../relations/positional-info.function";
import { scrollIntoViewIfNeeded } from "../utils/scroll-into-view-if-needed.function";
export function mouseActionApi(
webDriver: ThenableWebDriver,
accessorUtil: AccessorUtil,
ctx: TestExecutionContext
): MouseActionApi {
const toElement = (origin: WebElement) => ({origin, x: 0, y: 0});
function _xy(): Promise<never> {
throw Error('Not yet implemented due to api incompatibility');
}
async function _click(query: SahiElementQueryOrWebElement, combo: string = ""): Promise<void> {
const e = await accessorUtil.fetchElement(query);
await scrollIntoViewIfNeeded(e);
return runActionsWithComboKeys(
webDriver.actions({bridge: true}),
e,
combo,
a => a.click(e)
).perform();
}
async function _mouseDown(query: SahiElementQueryOrWebElement, isRight: boolean = false, combo: string = ''): Promise<void> {
const e = await accessorUtil.fetchElement(query);
const mouseButton = isRight ? Button.RIGHT : Button.LEFT;
await scrollIntoViewIfNeeded(e);
return runActionsWithComboKeys(
webDriver.actions({bridge: true}),
e, combo,
a => a.move(toElement(e)).press(mouseButton)
).perform();
}
async function _mouseUp(query: SahiElementQueryOrWebElement, isRight: boolean = false, combo: string = ''): Promise<void> {
const e = await accessorUtil.fetchElement(query);
const mouseButton = isRight ? Button.RIGHT : Button.LEFT;
await scrollIntoViewIfNeeded(e);
return runActionsWithComboKeys(
webDriver.actions({bridge: true}),
e, combo,
a => a.move(toElement(e)).release(mouseButton)
).perform();
}
async function _rightClick(query: SahiElementQueryOrWebElement, combo: string = ''): Promise<void> {
const e = await accessorUtil.fetchElement(query);
await scrollIntoViewIfNeeded(e);
return runActionsWithComboKeys(
webDriver.actions({bridge: true}),
e, combo,
a => a.contextClick(e)
).perform();
}
async function _mouseOver(query: SahiElementQueryOrWebElement, combo: string = '') {
const e = await accessorUtil.fetchElement(query);
await scrollIntoViewIfNeeded(e);
return runActionsWithComboKeys(
webDriver.actions({bridge: true}),
e, combo,
a => a.move({origin: e, x: 1, y: 1}).move(toElement(e))
).perform();
}
async function _check(query: SahiElementQueryOrWebElement) {
const e = await accessorUtil.fetchElement(query);
const tagName = await e.getTagName();
const type = await e.getAttribute("type");
if (tagName.toLocaleLowerCase() === 'input' && (type === "checkbox" || type === "radio")) {
await scrollIntoViewIfNeeded(e);
if (!(await e.getAttribute("checked"))) {
await e.click();
}
}
}
async function _uncheck(query: SahiElementQueryOrWebElement) {
const e = await accessorUtil.fetchElement(query);
const tagName = await e.getTagName();
const type = await e.getAttribute("type");
if (tagName.toLocaleLowerCase() === 'input' && type === "checkbox") {
await scrollIntoViewIfNeeded(e);
if ((await e.getAttribute("checked"))) {
await e.click();
}
}
}
async function _dragDrop(eSource: SahiElementQueryOrWebElement, eTarget: SahiElementQueryOrWebElement): Promise<void> {
const [src, target] = await Promise.all([
accessorUtil.fetchElement(eSource),
accessorUtil.fetchElement(eTarget)
]);
return webDriver.actions({bridge: true})
.dragAndDrop(src, target).perform();
/*
return webDriver.actions({bridge: true})
.move({origin: src, x: 1, y: 1})
.press()
.move({origin: target, x: 1, y: 1})
.release()
.perform()
*/
}
async function _dragDropXY(q: SahiElementQueryOrWebElement, x: number, y: number, $isRelative: boolean = false): Promise<void> {
const e = await accessorUtil.fetchElement(q);
let location: ILocation = {x, y};
if ($isRelative) {
const pi = await positionalInfo(e);
location = {x: x + pi.location.x, y: y + pi.location.y};
}
ctx.logger.info(`Drag to: ${JSON.stringify(location)}`);
return webDriver.actions({bridge: true}).dragAndDrop(e, location).perform();
}
async function _setSelected(query: SahiElementQueryOrWebElement, optionToSelect: string | number | string[] | number[], isMultiple: boolean = false): Promise<void> {
const e = await accessorUtil.fetchElement(query);
await scrollIntoViewIfNeeded(e);
const options = await e.findElements(By.css('option'));
const valuesOrIndices = typeof optionToSelect === 'string' || typeof optionToSelect === 'number' ? [optionToSelect] : optionToSelect;
const isNumberArray = (arr: (number | string)[]): arr is number[] => arr.every(n => typeof n === 'number');
for (const [i, option] of options.entries()) {
if (!isMultiple) {
await setElementSelect(option, false)
}
if (isNumberArray(valuesOrIndices)) {
if (valuesOrIndices.includes(i)) {
//await setElementSelect(option, true);
await option.click()
}
} else {
const value = await option.getAttribute('value');
const text = await option.getText();
if (valuesOrIndices.includes(value) || valuesOrIndices.includes(text)) {
//await setElementSelect(option, true);
await option.click();
}
}
}
}
async function setElementSelect(e: WebElement, selected: boolean = true) {
await webDriver.executeAsyncScript(stripIndents`
const e = arguments[0];
const selected = arguments[1];
const done = arguments[arguments.length - 1];
if(selected) {
e.setAttribute('selected', true)
} else {
e.removeAttribute('selected');
}
done(e.innerText);
`, e, selected);
}
return ({
_xy,
_click,
_rightClick,
_mouseDown,
_mouseUp,
_mouseOver,
_check,
_uncheck,
_setSelected,
_dragDrop,
_dragDropXY,
})
}