forked from puppeteer/puppeteer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch: - adds Mouse class which holds mouse state and implements mouse primitives, such as moving, button down and button up. - implements high-level mouse api, such as `page.click` and `page.hover`. References puppeteer#40, References puppeteer#89
- Loading branch information
1 parent
eb2cb67
commit 98ee356
Showing
17 changed files
with
458 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
class Mouse { | ||
/** | ||
* @param {!Connection} client | ||
* @param {!Keyboard} keyboard | ||
*/ | ||
constructor(client, keyboard) { | ||
this._client = client; | ||
this._keyboard = keyboard; | ||
this._x = 0; | ||
this._y = 0; | ||
this._button = 'none'; | ||
} | ||
|
||
/** | ||
* @param {number} x | ||
* @param {number} y | ||
* @return {!Promise} | ||
*/ | ||
async move(x, y) { | ||
this._x = x; | ||
this._y = y; | ||
await this._client.send('Input.dispatchMouseEvent', { | ||
type: 'mouseMoved', | ||
button: this._button, | ||
x, y, | ||
modifiers: this._modifiersMask() | ||
}); | ||
} | ||
|
||
/** | ||
* @param {!Object=} options | ||
*/ | ||
async press(options) { | ||
await this.down(options); | ||
await this.up(options); | ||
} | ||
|
||
/** | ||
* @param {!Object=} options | ||
*/ | ||
async down(options) { | ||
if (!options) | ||
options = {}; | ||
this._button = (options.button || 'left'); | ||
await this._client.send('Input.dispatchMouseEvent', { | ||
type: 'mousePressed', | ||
button: this._button, | ||
x: this._x, | ||
y: this._y, | ||
modifiers: this._modifiersMask(), | ||
clickCount: (options.clickCount || 1) | ||
}); | ||
} | ||
|
||
/** | ||
* @param {!Object=} options | ||
*/ | ||
async up(options) { | ||
if (!options) | ||
options = {}; | ||
this._button = 'none'; | ||
await this._client.send('Input.dispatchMouseEvent', { | ||
type: 'mouseReleased', | ||
button: (options.button || 'left'), | ||
x: this._x, | ||
y: this._y, | ||
modifiers: this._modifiersMask(), | ||
clickCount: (options.clickCount || 1) | ||
}); | ||
} | ||
|
||
/** | ||
* @return {number} | ||
*/ | ||
_modifiersMask() { | ||
let modifiers = this._keyboard.modifiers(); | ||
let mask = 0; | ||
if (modifiers.Alt) | ||
mask += 1; | ||
if (modifiers.Control) | ||
mask += 2; | ||
if (modifiers.Meta) | ||
mask += 4; | ||
if (modifiers.Shift) | ||
mask += 8; | ||
return mask; | ||
} | ||
} | ||
|
||
module.exports = Mouse; |
Oops, something went wrong.