/
element.ts
138 lines (117 loc) · 4.19 KB
/
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
import _ from 'lodash';
import {errors} from 'appium/driver';
import {FakeDriver} from '../driver';
import {Position, Rect, Size} from '@appium/types';
import {FakeElement} from '../fake-element';
import {mixin} from './mixin';
interface FakeDriverElementsMixin {
getElements(elementIds: string[]): FakeElement[];
getElement(elementId: string): FakeElement;
getName(elementId: string): Promise<string>;
elementDisplayed(elementId: string): Promise<boolean>;
elementEnabled(elementId: string): Promise<boolean>;
elementSelected(elementId: string): Promise<boolean>;
setValue(keys: string | string[], value: string): Promise<void>;
getText(elementId: string): Promise<string>;
clear(elementId: string): Promise<void>;
click(elementId: string): Promise<void>;
getAttribute(elementId: string, attributeName: string): Promise<string>;
getElementRect(elementId: string): Promise<Rect>;
getSize(elementId: string): Promise<Size>;
equalsElement(elementId: string, otherElementId: string): Promise<boolean>;
getCssProperty(elementId: string, propertyName: string): Promise<string>;
getLocation(elementId: string): Promise<Position>;
getLocationInView(elementId: string): Promise<Position>;
}
declare module '../driver' {
interface FakeDriver extends FakeDriverElementsMixin {}
}
const ElementsMixin: FakeDriverElementsMixin = {
getElements(this: FakeDriver, elementIds: string[]) {
for (const elId of elementIds) {
if (!_.has(this.elMap, elId)) {
throw new errors.StaleElementReferenceError();
}
}
return elementIds.map((e) => this.elMap[e]);
},
getElement(this: FakeDriver, elementId: string) {
return this.getElements([elementId])[0];
},
async getName(this: FakeDriver, elementId: string) {
const el = this.getElement(elementId);
return el.tagName;
},
async elementDisplayed(this: FakeDriver, elementId: string) {
const el = this.getElement(elementId);
return el.isVisible();
},
async elementEnabled(this: FakeDriver, elementId: string) {
const el = this.getElement(elementId);
return el.isEnabled();
},
async elementSelected(this: FakeDriver, elementId: string) {
const el = this.getElement(elementId);
return el.isSelected();
},
async setValue(this: FakeDriver, keys: string | string[], elementId: string) {
let value = keys;
if (keys instanceof Array) {
value = keys.join('');
}
const el = this.getElement(elementId);
if (el.type !== 'MockInputField') {
throw new errors.InvalidElementStateError();
}
el.setAttr('value', value);
},
async getText(this: FakeDriver, elementId: string) {
const el = this.getElement(elementId);
return el.getAttr('value');
},
async clear(this: FakeDriver, elementId: string) {
await this.setValue('', elementId);
},
/**
* This comment should be displayed instead of the one from ExternalDriver
*/
async click(this: FakeDriver, elementId: string) {
this.assertNoAlert();
const el = this.getElement(elementId);
if (!el.isVisible()) {
throw new errors.InvalidElementStateError();
}
el.click();
this.focusedElId = elementId;
},
async getAttribute(this: FakeDriver, attr: string, elementId: string) {
const el = this.getElement(elementId);
return el.getAttr(attr);
},
async getElementRect(this: FakeDriver, elementId: string) {
const el = this.getElement(elementId);
return el.getElementRect();
},
async getSize(this: FakeDriver, elementId: string) {
const el = this.getElement(elementId);
return el.getSize();
},
async equalsElement(this: FakeDriver, elementIdA: string, elementIdB: string) {
const el1 = this.getElement(elementIdA);
const el2 = this.getElement(elementIdB);
return el1.equals(el2);
},
async getCssProperty(this: FakeDriver, prop: string, elementId: string) {
this.assertWebviewContext();
const el = this.getElement(elementId);
return el.getCss(prop);
},
async getLocation(this: FakeDriver, elementId: string) {
const el = this.getElement(elementId);
return el.getLocation();
},
async getLocationInView(this: FakeDriver, elementId: string) {
return this.getLocation(elementId);
},
};
mixin(ElementsMixin);