-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathui-element.ts
269 lines (239 loc) · 7.65 KB
/
ui-element.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { Point } from "./point";
import { Direction } from "./direction";
import { INsCapabilities } from "./interfaces/ns-capabilities";
import { IRectangle } from "./interfaces/rectangle";
import { calculateOffset } from "./utils";
export class UIElement {
constructor(private _element: any,
private _driver: any,
private _wd: any,
private _webio: any,
private _args: INsCapabilities,
private _searchMethod: string,
private _searchParams: string,
private _index?: number
) { }
/**
* Click on element
*/
public async click() {
return await (await this.element()).click();
}
/**
* Tap on element
*/
public async tap() {
return await (await this.element()).tap();
}
/**
* Double tap on element
*/
public async doubleTap() {
return await this._driver.execute('mobile: doubleTap', { element: (await this.element()).value.ELEMENT });
}
/**
* Get location of element
*/
public async location() {
const location = await (await this.element()).getLocation();
const point = new Point(location.x, location.y);
return point;
}
/**
* Get size of element
*/
public async size() {
const size = await (await this.element()).getSize();
const point = new Point(size.height, size.width);
return point;
}
/**
* Get text of element
*/
public async text() {
return await (await this.element()).text();
}
/**
* Get web driver element
*/
public async element() {
this._element = await this.refetch();
return await this._element;
}
/**
* Shows if element is displayed. Returns true or false. If the element doesn't exist it will return false
*/
public async isDisplayed() {
const el = (await this.element());
return (await el) === null ? false : (await this._element.isDisplayed());
}
/**
* Returns true or false
*/
public async exists() {
return this.element() === null ? false : true;
}
/**
* Waits until the element exists not.
* @param wait
*/
public async waitForExistNot(wait: number = 3000) {
return await this._webio.waitForExist(this._searchParams, wait, true);
}
/**
* Wait until the elements appear
* @param wait
*/
public async waitForExist(wait: number = 3000) {
return this._webio.waitForExist(this._searchParams, wait, false);
}
/**
* Get attribute of element
* @param attr
*/
public async getAttribute(attr) {
return await (await this.element()).getAttribute(attr);
}
/**
* Get rectangle of element
*/
public async getRectangle() {
const location = await this.location();
const size = await this.size();
const rect = { x: location.x, y: location.y, width: size.y, height: size.x };
return rect;
}
/**
* Get rectangle of element in actual dimensions
*/
public async getActualRectangle() {
const actRect = await this.getRectangle();
if (this._args.isIOS) {
const density = this._args.device.config.density;
actRect.x *= density;
actRect.y *= density;
actRect.width *= density;
actRect.height *= density;
}
return actRect;
}
/**
* Scroll with offset from elemnt with minimum inertia
* @param direction
* @param yOffset
* @param xOffset
*/
public async scroll(direction: Direction, yOffset: number = 0, xOffset: number = 0) {
//await this._driver.execute("mobile: scroll", [{direction: 'up'}])
//await this._driver.execute('mobile: scroll', { direction: direction === 0 ? "down" : "up", element: this._element.ELEMENT });
const location = await this.location();
const size = await this.size();
const x = location.x === 0 ? 10 : location.x;
let y = (location.y + 15);
if (yOffset === 0) {
yOffset = location.y + size.y - 15;
}
if (direction === Direction.down) {
y = (location.y + size.y) - 15;
if (!this._webio.isIOS) {
if (yOffset === 0) {
yOffset = location.y + size.y - 15;
}
}
}
if (direction === Direction.up) {
if (yOffset === 0) {
yOffset = size.y - 15;
}
}
const endPoint = calculateOffset(direction, y, yOffset, x, xOffset, this._webio.isIOS, false);
if (direction === Direction.down) {
endPoint.point.y += location.y;
}
let action = new this._wd.TouchAction(this._driver);
action
.press({ x: x, y: y })
.wait(endPoint.duration)
.moveTo({ x: endPoint.point.x, y: endPoint.point.y })
.release();
await action.perform();
await this._driver.sleep(150);
}
/**
* Scroll with offset from elemnt with minimum inertia
* @param direction
* @param yOffset
* @param xOffset
*/
public async scrollTo(direction: Direction, elementToSearch, yOffset: number = 0, xOffset: number = 0) {
//await this._driver.execute("mobile: scroll", [{direction: 'up'}])
//await this._driver.execute('mobile: scroll', { direction: direction === 0 ? "down" : "up", element: this._element.ELEMENT });
let el: UIElement = null;
let retries = 7;
while (el === null && retries >= 0) {
try {
el = await elementToSearch();
if (!el || el === null || !(await el.isDisplayed())) {
el = null;
await this.scroll(direction, yOffset, xOffset);
}
} catch (error) {
await this.scroll(direction, yOffset, xOffset);
}
retries--;
}
return el;
}
/**
* Scroll with offset from elemnt with minimum inertia
* @param direction
* @param yOffset
* @param xOffset
*/
public async drag(direction: Direction, yOffset: number, xOffset: number = 0) {
const location = await this.location();
const x = location.x === 0 ? 10 : location.x;
const y = location.y === 0 ? 10 : location.y;
const endPoint = calculateOffset(direction, y, yOffset, x, xOffset, this._webio.isIOS, false);
let action = new this._wd.TouchAction(this._driver);
action
.press({ x: x, y: y })
.wait(endPoint.duration)
.moveTo({ x: endPoint.point.x, y: endPoint.point.y })
.release();
await action.perform();
await this._driver.sleep(150);
}
/**
* Click and hold over an element
*/
public async hold() {
let action = new this._wd.TouchAction(this._driver);
action
.longPress({ el: await this.element() })
.release();
await action.perform();
await this._driver.sleep(150);
}
/**
* Send keys to field or other UI component
* @param text
*/
public async sendKeys(text: string) {
await this._element.sendKeys(text);
}
public async log() {
console.dir(await this.element());
}
public async refetch() {
try {
if (this._index != null) {
return (await this._driver[this._searchMethod](this._searchParams, 1000))[this._index];
} else {
return await this._driver[this._searchMethod](this._searchParams, 1000);
}
} catch (error) {
return null;
}
}
}