forked from nightwatchjs/nightwatch
/
pressKeyCode.js
58 lines (52 loc) · 1.68 KB
/
pressKeyCode.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
const ProtocolAction = require('../_base-action.js');
/**
* Press a particular key on an Android Device.
*
* See [official Android Developers docs](https://developer.android.com/reference/android/view/KeyEvent.html) for reference of available Android key code values.
*
* @example
* module.exports = {
* 'press e with caps lock on (keycode 33 and metastate 1048576)': function (app) {
* app
* .appium.pressKeyCode(33, 1048576);
* },
*
* 'press g (keycode 35) with ES6 async/await': async function (app) {
* await app.appium.pressKeyCode(35);
* }
* };
*
* @syntax .appium.pressKeyCode(keycode, [callback])
* @syntax .appium.pressKeyCode(keycode, metastate, flags, [callback])
* @method pressKeyCode
* @param {number} keycode Key code to press on the device.
* @param {number} [metastate] Meta state to press the keycode with.
* @param {number} [flags] Flags for the keypress.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @see appium.longPressKeyCode
* @api protocol.appium
*/
module.exports = class Session extends ProtocolAction {
static get isTraceable() {
return true;
}
command(keycode, ...args) {
let metastate;
let flags;
let callback;
if (typeof keycode !== 'number') {
throw new Error('The first argument to pressKeyCode is mandatory and must be a number.');
}
if (typeof args[0] === 'function') {
callback = args[0];
} else {
[metastate, flags, callback] = args;
}
const opts = {
keycode,
...(metastate && {metastate}),
...(flags && {flags})
};
return this.transportActions.pressDeviceKeyCode(opts, callback);
}
};