-
Notifications
You must be signed in to change notification settings - Fork 108
/
general.js
226 lines (205 loc) · 6.73 KB
/
general.js
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
import { errors } from 'appium-base-driver';
import _ from 'lodash';
import js2xml from "js2xmlparser2";
import log from '../logger';
import { fs, util } from 'appium-support';
import { exec } from 'teen_process';
import utils from '../utils';
import { openUrl } from 'node-simctl';
let commands = {}, helpers = {}, extensions = {};
commands.active = async function () {
if (this.isWebContext()) {
return await this.executeAtom('active_element', []);
} else {
return await this.uiAutoClient.sendCommand("au.getActiveElement()");
}
};
commands.getDeviceTime = async function () {
log.info('Attempting to capture iOS device date and time');
if (this.isRealDevice()) {
try {
let idevicedate = await fs.which('idevicedate');
log.info(`Found idevicedate: '${idevicedate}'`);
let {stdout} = await exec(idevicedate, ['-u', this.opts.udid]);
stdout = stdout.trim();
try {
let date = new Date(stdout);
let dateStr = date.toString();
if (dateStr === 'Invalid Date') {
throw new Error('Unable to parse idevicedate output');
}
return dateStr;
} catch (err) {
// sometimes `idevicedate` returns an un-parsable format
// in which case, we just want to return the output and
// let the client deal with it
log.debug(`Unable to parse date '${stdout}'. Returning as is`);
return stdout;
}
} catch (err) {
log.errorAndThrow('Could not capture device date and time using libimobiledevice idevicedate. ' +
'Libimobiledevice is probably not installed');
}
} else {
log.warn('On simulator. Assuming device time is the same as host time');
return new Date().toString();
}
};
commands.hideKeyboard = async function (strategy, ...possibleKeys) {
possibleKeys.pop(); // last parameter is the session id
let cmd;
let key = _.find(possibleKeys, (k) => {return k;});
if (key) {
strategy = strategy || 'pressKey';
cmd = `au.hideKeyboard('${strategy}', '${key}')`;
} else {
strategy = strategy || 'default';
cmd = `au.hideKeyboard('${strategy}')`;
}
await this.uiAutoClient.sendCommand(cmd);
};
commands.getPageSource = async function () {
if (this.isWebContext()) {
const script = 'return document.documentElement.outerHTML';
return await this.executeAtom('execute_script', [script, []]);
} else {
return await this.getNativePageSource();
}
};
helpers.getNativePageSource = async function () {
let jsonSource = await this.getSourceForElementForXML();
if (typeof jsonSource === "string") {
jsonSource = JSON.parse(jsonSource);
}
let xmlSource = js2xml("AppiumAUT", jsonSource, {
wrapArray: {enabled: false, elementName: "element"},
declaration: {include: true},
prettyPrinting: {indentString: " "}
});
return xmlSource;
};
commands.background = async function (secs) {
await this.uiAutoClient.sendCommand(`au.background(${secs})`);
};
commands.lock = async function (secs) {
if (!secs) {
log.debug('No seconds parameter. Using 0 seconds');
secs = 0;
}
await this.uiAutoClient.sendCommand(`au.lock(${secs})`);
};
commands.closeApp = async function () {
let appName = this.opts.app || this.opts.bundleId;
try {
await this.stop();
log.info(`Successfully closed the '${appName}' app.`);
} catch (err) {
log.warn(`Something went wrong while closing the '${appName}' app.`);
throw err;
}
};
commands.launchApp = async function () {
let appName = this.opts.app || this.opts.bundleId;
try {
await this.start();
log.info(`Successfully launched the '${appName}' app.`);
} catch (err) {
log.warn(`Something went wrong while launching the '${appName}' app.`);
throw err;
}
};
commands.removeApp = async function (bundleId) {
if (this.isRealDevice()) {
await this.realDevice.remove(bundleId);
} else {
await this.sim.removeApp(bundleId);
}
};
commands.keys = async function (keys) {
if (this.isWebContext()) {
let el = await this.active();
if (_.isUndefined(el.ELEMENT)) {
throw new errors.NoSuchElementError();
}
await this.setValue(keys, el.ELEMENT);
} else {
if (_.isArray(keys)) {
keys = keys.join('');
}
if (!_.isString(keys)) {
keys = keys.toString();
}
keys = util.escapeSpecialChars(keys, "'");
let command = `au.sendKeysToActiveElement('${keys}')`;
await this.uiAutoClient.sendCommand(command);
}
};
commands.setGeoLocation = async function (location) {
// TODO: check the hasOptions bit, the method signature should be location, option
//let hasOptions = altitude !== null || horizontalAccuracy !== null || verticalAccuracy !== null || course !== null || speed !== null;
//if (hasOptions) {
//let options = {};
//if (altitude !== null) {
//options.altitude = altitude;
//}
//if (horizontalAccuracy !== null) {
//options.horizontalAccuracy = horizontalAccuracy;
//}
//if (verticalAccuracy !== null) {
//options.verticalAccuracy = verticalAccuracy;
//}
//if (course !== null) {
//options.course = course;
//}
//if (speed !== null) {
//options.speed = speed;
//}
//await this.uiAutoClient.sendCommand(
//`target.setLocationWithOptions(${JSON.stringify(coordinates)}, ${JSON.stringify(options)})`);
//} else {
await this.uiAutoClient.sendCommand(`target.setLocation(${JSON.stringify(location)})`);
//}
};
commands.getWindowSize = async function (windowHandle="current") {
if (windowHandle !== "current") {
throw new errors.NotYetImplementedError("Currently only getting current window size is supported.");
}
if (this.isWebContext()) {
return await this.executeAtom('get_window_size', []);
} else {
return await this.uiAutoClient.sendCommand("au.getWindowSize()");
}
};
// For W3C
commands.getWindowRect = async function () {
const {width, height} = await this.getWindowSize();
return {
width,
height,
x: 0,
y: 0
};
};
commands.getStrings = async function (language, stringFile = null) {
log.debug(`Gettings strings for language '${language}' and string file '${stringFile}'`);
let strings = await utils.parseLocalizableStrings(_.defaults({language, stringFile}, this.opts));
if (strings && strings.length >= 1) {
strings = strings[0];
}
return strings;
};
commands.setUrl = async function (url) {
log.debug(`Attempting to set url '${url}'`);
if (!this.isWebContext()) {
// use xcrun to open deeplink
await openUrl(this.opts.udid || this.sim.udid, url);
return;
}
this.setCurrentUrl(url);
// make sure to clear out any leftover web frames
this.curWebFrames = [];
await this.remote.navToUrl(url);
};
Object.assign(extensions, commands, helpers);
export { commands, helpers };
export default extensions;