2017-07-18 01:49:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the 'License');
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an 'AS IS' BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-01-26 04:21:14 +00:00
|
|
|
const {assert} = require('./helper');
|
2017-10-23 19:43:45 +00:00
|
|
|
const keyDefinitions = require('./USKeyboardLayout');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} KeyDescription
|
|
|
|
* @property {number} keyCode
|
|
|
|
* @property {string} key
|
|
|
|
* @property {string} text
|
|
|
|
* @property {string} code
|
|
|
|
* @property {number} location
|
|
|
|
*/
|
2017-07-19 03:53:00 +00:00
|
|
|
|
2017-07-18 01:49:52 +00:00
|
|
|
class Keyboard {
|
|
|
|
/**
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {!Puppeteer.CDPSession} client
|
2017-07-18 01:49:52 +00:00
|
|
|
*/
|
|
|
|
constructor(client) {
|
|
|
|
this._client = client;
|
2017-07-25 21:35:03 +00:00
|
|
|
this._modifiers = 0;
|
2017-07-31 19:05:46 +00:00
|
|
|
this._pressedKeys = new Set();
|
2017-07-18 01:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} key
|
2018-08-24 22:17:36 +00:00
|
|
|
* @param {{text?: string}=} options
|
2017-07-18 01:49:52 +00:00
|
|
|
*/
|
2017-10-23 19:43:45 +00:00
|
|
|
async down(key, options = { text: undefined }) {
|
|
|
|
const description = this._keyDescriptionForString(key);
|
|
|
|
|
|
|
|
const autoRepeat = this._pressedKeys.has(description.code);
|
|
|
|
this._pressedKeys.add(description.code);
|
|
|
|
this._modifiers |= this._modifierBit(description.key);
|
|
|
|
|
|
|
|
const text = options.text === undefined ? description.text : options.text;
|
2017-07-18 01:49:52 +00:00
|
|
|
await this._client.send('Input.dispatchKeyEvent', {
|
|
|
|
type: text ? 'keyDown' : 'rawKeyDown',
|
2017-07-25 21:35:03 +00:00
|
|
|
modifiers: this._modifiers,
|
2017-10-23 19:43:45 +00:00
|
|
|
windowsVirtualKeyCode: description.keyCode,
|
|
|
|
code: description.code,
|
|
|
|
key: description.key,
|
|
|
|
text: text,
|
2017-07-31 19:05:46 +00:00
|
|
|
unmodifiedText: text,
|
2017-10-23 19:43:45 +00:00
|
|
|
autoRepeat,
|
|
|
|
location: description.location,
|
|
|
|
isKeypad: description.location === 3
|
2017-07-18 01:49:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-25 21:35:03 +00:00
|
|
|
* @param {string} key
|
|
|
|
* @return {number}
|
2017-07-18 01:49:52 +00:00
|
|
|
*/
|
2017-07-25 21:35:03 +00:00
|
|
|
_modifierBit(key) {
|
|
|
|
if (key === 'Alt')
|
|
|
|
return 1;
|
|
|
|
if (key === 'Control')
|
|
|
|
return 2;
|
|
|
|
if (key === 'Meta')
|
|
|
|
return 4;
|
|
|
|
if (key === 'Shift')
|
|
|
|
return 8;
|
|
|
|
return 0;
|
2017-07-18 01:49:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 19:43:45 +00:00
|
|
|
/**
|
|
|
|
* @param {string} keyString
|
|
|
|
* @return {KeyDescription}
|
|
|
|
*/
|
|
|
|
_keyDescriptionForString(keyString) {
|
|
|
|
const shift = this._modifiers & 8;
|
|
|
|
const description = {
|
|
|
|
key: '',
|
|
|
|
keyCode: 0,
|
|
|
|
code: '',
|
|
|
|
text: '',
|
|
|
|
location: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
const definition = keyDefinitions[keyString];
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(definition, `Unknown key: "${keyString}"`);
|
2017-10-23 19:43:45 +00:00
|
|
|
|
|
|
|
if (definition.key)
|
|
|
|
description.key = definition.key;
|
|
|
|
if (shift && definition.shiftKey)
|
|
|
|
description.key = definition.shiftKey;
|
|
|
|
|
|
|
|
if (definition.keyCode)
|
|
|
|
description.keyCode = definition.keyCode;
|
|
|
|
if (shift && definition.shiftKeyCode)
|
|
|
|
description.keyCode = definition.shiftKeyCode;
|
|
|
|
|
|
|
|
if (definition.code)
|
|
|
|
description.code = definition.code;
|
|
|
|
|
|
|
|
if (definition.location)
|
|
|
|
description.location = definition.location;
|
|
|
|
|
|
|
|
if (description.key.length === 1)
|
|
|
|
description.text = description.key;
|
|
|
|
|
|
|
|
if (definition.text)
|
|
|
|
description.text = definition.text;
|
|
|
|
if (shift && definition.shiftText)
|
|
|
|
description.text = definition.shiftText;
|
|
|
|
|
|
|
|
// if any modifiers besides shift are pressed, no text should be sent
|
|
|
|
if (this._modifiers & ~8)
|
|
|
|
description.text = '';
|
|
|
|
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
2017-07-18 01:49:52 +00:00
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
*/
|
2017-07-19 21:27:01 +00:00
|
|
|
async up(key) {
|
2017-10-23 19:43:45 +00:00
|
|
|
const description = this._keyDescriptionForString(key);
|
|
|
|
|
|
|
|
this._modifiers &= ~this._modifierBit(description.key);
|
2018-02-13 01:25:38 +00:00
|
|
|
this._pressedKeys.delete(description.code);
|
2017-07-18 01:49:52 +00:00
|
|
|
await this._client.send('Input.dispatchKeyEvent', {
|
|
|
|
type: 'keyUp',
|
2017-07-25 21:35:03 +00:00
|
|
|
modifiers: this._modifiers,
|
2017-10-23 19:43:45 +00:00
|
|
|
key: description.key,
|
|
|
|
windowsVirtualKeyCode: description.keyCode,
|
|
|
|
code: description.code,
|
|
|
|
location: description.location
|
2017-07-18 01:49:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} char
|
|
|
|
*/
|
|
|
|
async sendCharacter(char) {
|
2018-07-31 18:22:26 +00:00
|
|
|
await this._client.send('Input.insertText', {text: char});
|
2017-07-18 01:49:52 +00:00
|
|
|
}
|
2017-10-07 07:28:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} text
|
|
|
|
* @param {{delay: (number|undefined)}=} options
|
|
|
|
*/
|
|
|
|
async type(text, options) {
|
|
|
|
let delay = 0;
|
|
|
|
if (options && options.delay)
|
|
|
|
delay = options.delay;
|
|
|
|
for (const char of text) {
|
2017-10-23 19:43:45 +00:00
|
|
|
if (keyDefinitions[char])
|
|
|
|
await this.press(char, {delay});
|
|
|
|
else
|
|
|
|
await this.sendCharacter(char);
|
2017-10-07 07:28:24 +00:00
|
|
|
if (delay)
|
|
|
|
await new Promise(f => setTimeout(f, delay));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} key
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{delay?: number, text?: string}=} options
|
2017-10-07 07:28:24 +00:00
|
|
|
*/
|
2018-11-12 20:59:21 +00:00
|
|
|
async press(key, options = {}) {
|
|
|
|
const {delay = null} = options;
|
2017-10-07 07:28:24 +00:00
|
|
|
await this.down(key, options);
|
2018-11-12 20:59:21 +00:00
|
|
|
if (delay !== null)
|
2017-10-07 07:28:24 +00:00
|
|
|
await new Promise(f => setTimeout(f, options.delay));
|
|
|
|
await this.up(key);
|
|
|
|
}
|
2017-07-25 21:35:03 +00:00
|
|
|
}
|
2017-07-18 01:49:52 +00:00
|
|
|
|
2017-07-25 21:35:03 +00:00
|
|
|
class Mouse {
|
2017-07-18 01:49:52 +00:00
|
|
|
/**
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {Puppeteer.CDPSession} client
|
2017-07-25 21:35:03 +00:00
|
|
|
* @param {!Keyboard} keyboard
|
|
|
|
*/
|
|
|
|
constructor(client, keyboard) {
|
|
|
|
this._client = client;
|
|
|
|
this._keyboard = keyboard;
|
|
|
|
this._x = 0;
|
|
|
|
this._y = 0;
|
2018-08-06 18:31:33 +00:00
|
|
|
/** @type {'none'|'left'|'right'|'middle'} */
|
2017-07-25 21:35:03 +00:00
|
|
|
this._button = 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} x
|
|
|
|
* @param {number} y
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{steps?: number}=} options
|
2017-07-25 21:35:03 +00:00
|
|
|
*/
|
2017-08-29 21:13:38 +00:00
|
|
|
async move(x, y, options = {}) {
|
2018-11-12 20:59:21 +00:00
|
|
|
const {steps = 1} = options;
|
2017-08-29 21:13:38 +00:00
|
|
|
const fromX = this._x, fromY = this._y;
|
2017-07-25 21:35:03 +00:00
|
|
|
this._x = x;
|
|
|
|
this._y = y;
|
2017-08-29 21:13:38 +00:00
|
|
|
for (let i = 1; i <= steps; i++) {
|
|
|
|
await this._client.send('Input.dispatchMouseEvent', {
|
|
|
|
type: 'mouseMoved',
|
|
|
|
button: this._button,
|
|
|
|
x: fromX + (this._x - fromX) * (i / steps),
|
|
|
|
y: fromY + (this._y - fromY) * (i / steps),
|
|
|
|
modifiers: this._keyboard._modifiers
|
|
|
|
});
|
|
|
|
}
|
2017-07-25 21:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-25 23:05:23 +00:00
|
|
|
* @param {number} x
|
|
|
|
* @param {number} y
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{delay?: number, button?: "left"|"right"|"middle", clickCount?: number}=} options
|
2017-07-25 21:35:03 +00:00
|
|
|
*/
|
2017-08-21 23:32:39 +00:00
|
|
|
async click(x, y, options = {}) {
|
2018-11-12 20:59:21 +00:00
|
|
|
const {delay = null} = options;
|
2019-06-10 08:42:19 +00:00
|
|
|
if (delay !== null) {
|
|
|
|
await Promise.all([
|
|
|
|
this.move(x, y),
|
|
|
|
this.down(options),
|
|
|
|
]);
|
2018-11-12 20:59:21 +00:00
|
|
|
await new Promise(f => setTimeout(f, delay));
|
2019-06-10 08:42:19 +00:00
|
|
|
await this.up(options);
|
|
|
|
} else {
|
|
|
|
await Promise.all([
|
|
|
|
this.move(x, y),
|
|
|
|
this.down(options),
|
|
|
|
this.up(options),
|
|
|
|
]);
|
|
|
|
}
|
2017-07-26 00:03:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 21:35:03 +00:00
|
|
|
/**
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{button?: "left"|"right"|"middle", clickCount?: number}=} options
|
2017-07-18 01:49:52 +00:00
|
|
|
*/
|
2017-08-21 23:32:39 +00:00
|
|
|
async down(options = {}) {
|
2018-11-12 20:59:21 +00:00
|
|
|
const {button = 'left', clickCount = 1} = options;
|
|
|
|
this._button = button;
|
2017-07-25 21:35:03 +00:00
|
|
|
await this._client.send('Input.dispatchMouseEvent', {
|
|
|
|
type: 'mousePressed',
|
2018-11-12 20:59:21 +00:00
|
|
|
button,
|
2017-07-25 21:35:03 +00:00
|
|
|
x: this._x,
|
|
|
|
y: this._y,
|
|
|
|
modifiers: this._keyboard._modifiers,
|
2018-11-12 20:59:21 +00:00
|
|
|
clickCount
|
2017-07-25 21:35:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-12 20:59:21 +00:00
|
|
|
* @param {!{button?: "left"|"right"|"middle", clickCount?: number}=} options
|
2017-07-25 21:35:03 +00:00
|
|
|
*/
|
2017-08-21 23:32:39 +00:00
|
|
|
async up(options = {}) {
|
2018-11-12 20:59:21 +00:00
|
|
|
const {button = 'left', clickCount = 1} = options;
|
2017-07-25 21:35:03 +00:00
|
|
|
this._button = 'none';
|
|
|
|
await this._client.send('Input.dispatchMouseEvent', {
|
|
|
|
type: 'mouseReleased',
|
2018-11-12 20:59:21 +00:00
|
|
|
button,
|
2017-07-25 21:35:03 +00:00
|
|
|
x: this._x,
|
|
|
|
y: this._y,
|
|
|
|
modifiers: this._keyboard._modifiers,
|
2018-11-12 20:59:21 +00:00
|
|
|
clickCount
|
2017-07-25 21:35:03 +00:00
|
|
|
});
|
2017-07-18 01:49:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-02 02:03:51 +00:00
|
|
|
class Touchscreen {
|
|
|
|
/**
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {Puppeteer.CDPSession} client
|
2017-09-02 02:03:51 +00:00
|
|
|
* @param {Keyboard} keyboard
|
|
|
|
*/
|
|
|
|
constructor(client, keyboard) {
|
|
|
|
this._client = client;
|
|
|
|
this._keyboard = keyboard;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} x
|
|
|
|
* @param {number} y
|
|
|
|
*/
|
|
|
|
async tap(x, y) {
|
2017-10-26 21:53:50 +00:00
|
|
|
// Touches appear to be lost during the first frame after navigation.
|
|
|
|
// This waits a frame before sending the tap.
|
|
|
|
// @see https://crbug.com/613219
|
|
|
|
await this._client.send('Runtime.evaluate', {
|
|
|
|
expression: 'new Promise(x => requestAnimationFrame(() => requestAnimationFrame(x)))',
|
|
|
|
awaitPromise: true
|
|
|
|
});
|
|
|
|
|
2017-09-02 02:03:51 +00:00
|
|
|
const touchPoints = [{x: Math.round(x), y: Math.round(y)}];
|
|
|
|
await this._client.send('Input.dispatchTouchEvent', {
|
|
|
|
type: 'touchStart',
|
|
|
|
touchPoints,
|
|
|
|
modifiers: this._keyboard._modifiers
|
|
|
|
});
|
|
|
|
await this._client.send('Input.dispatchTouchEvent', {
|
|
|
|
type: 'touchEnd',
|
|
|
|
touchPoints: [],
|
|
|
|
modifiers: this._keyboard._modifiers
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { Keyboard, Mouse, Touchscreen};
|