diff --git a/src/chromium/Input.ts b/src/chromium/Input.ts index 7601fb9d27..9d6cf5543d 100644 --- a/src/chromium/Input.ts +++ b/src/chromium/Input.ts @@ -69,69 +69,42 @@ export class RawKeyboardImpl implements input.RawKeyboard { } } -export class Mouse implements input.MouseOperations { +export class RawMouseImpl implements input.RawMouse { private _client: CDPSession; - private _keyboard: input.Keyboard; - private _x = 0; - private _y = 0; - private _button: 'none' | input.Button = 'none'; - constructor(client: CDPSession, keyboard: input.Keyboard) { + constructor(client: CDPSession) { this._client = client; - this._keyboard = keyboard; } - async move(x: number, y: number, options: { steps?: number; } = {}) { - const {steps = 1} = options; - const fromX = this._x, fromY = this._y; - this._x = x; - this._y = y; - 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: toModifiersMask(this._keyboard._modifiers()) - }); - } + async move(x: number, y: number, button: input.Button | 'none', buttons: Set, modifiers: Set): Promise { + await this._client.send('Input.dispatchMouseEvent', { + type: 'mouseMoved', + button, + x, + y, + modifiers: toModifiersMask(modifiers) + }); } - async down(options: { button?: input.Button; clickCount?: number; } = {}) { - const {button = 'left', clickCount = 1} = options; - this._button = button; + async down(x: number, y: number, button: input.Button, buttons: Set, modifiers: Set, clickCount: number): Promise { await this._client.send('Input.dispatchMouseEvent', { type: 'mousePressed', button, - x: this._x, - y: this._y, - modifiers: toModifiersMask(this._keyboard._modifiers()), + x, + y, + modifiers: toModifiersMask(modifiers), clickCount }); } - async up(options: { button?: input.Button; clickCount?: number; } = {}) { - const {button = 'left', clickCount = 1} = options; - this._button = 'none'; + async up(x: number, y: number, button: input.Button, buttons: Set, modifiers: Set, clickCount: number): Promise { await this._client.send('Input.dispatchMouseEvent', { type: 'mouseReleased', button, - x: this._x, - y: this._y, - modifiers: toModifiersMask(this._keyboard._modifiers()), + x, + y, + modifiers: toModifiersMask(modifiers), clickCount }); } - - async click(x: number, y: number, options?: input.ClickOptions) { - await new input.MouseClicker(this).click(x, y, options); - } - - async dblclick(x: number, y: number, options?: input.ClickOptions) { - await new input.MouseClicker(this).dblclick(x, y, options); - } - - async tripleclick(x: number, y: number, options?: input.ClickOptions) { - await new input.MouseClicker(this).tripleclick(x, y, options); - } } diff --git a/src/chromium/Page.ts b/src/chromium/Page.ts index 8f4745c560..7391613023 100644 --- a/src/chromium/Page.ts +++ b/src/chromium/Page.ts @@ -36,7 +36,7 @@ import { PDF } from './features/pdf'; import { Workers } from './features/workers'; import { Frame } from './Frame'; import { FrameManager, FrameManagerEvents } from './FrameManager'; -import { Mouse, RawKeyboardImpl } from './Input'; +import { RawMouseImpl, RawKeyboardImpl } from './Input'; import { createJSHandle, ElementHandle, JSHandle } from './JSHandle'; import { NetworkManagerEvents, Response } from './NetworkManager'; import { Protocol } from './protocol'; @@ -62,7 +62,7 @@ export class Page extends EventEmitter { _client: CDPSession; private _target: Target; private _keyboard: input.Keyboard; - private _mouse: Mouse; + private _mouse: input.Mouse; private _timeoutSettings: TimeoutSettings; private _frameManager: FrameManager; private _emulationManager: EmulationManager; @@ -94,7 +94,7 @@ export class Page extends EventEmitter { this._client = client; this._target = target; this._keyboard = new input.Keyboard(new RawKeyboardImpl(client)); - this._mouse = new Mouse(client, this._keyboard); + this._mouse = new input.Mouse(new RawMouseImpl(client), this._keyboard); this._timeoutSettings = new TimeoutSettings(); this.accessibility = new Accessibility(client); this._frameManager = new FrameManager(client, this, ignoreHTTPSErrors, this._timeoutSettings); @@ -680,7 +680,7 @@ export class Page extends EventEmitter { return this._closed; } - get mouse(): Mouse { + get mouse(): input.Mouse { return this._mouse; } diff --git a/src/chromium/api.ts b/src/chromium/api.ts index 46c3657e1b..30620936d7 100644 --- a/src/chromium/api.ts +++ b/src/chromium/api.ts @@ -17,8 +17,7 @@ export { PDF } from './features/pdf'; export { Permissions } from './features/permissions'; export { Worker, Workers } from './features/workers'; export { Frame } from './Frame'; -export { Mouse } from './Input'; -export { Keyboard } from '../input'; +export { Keyboard, Mouse } from '../input'; export { ElementHandle, JSHandle } from './JSHandle'; export { Request, Response } from './NetworkManager'; export { ConsoleMessage, FileChooser, Page } from './Page'; diff --git a/src/firefox/Input.ts b/src/firefox/Input.ts index 94dba466ce..6b97476ea5 100644 --- a/src/firefox/Input.ts +++ b/src/firefox/Input.ts @@ -31,6 +31,26 @@ function toModifiersMask(modifiers: Set): number { return mask; } +function toButtonNumber(button: input.Button): number { + if (button === 'left') + return 0; + if (button === 'middle') + return 1; + if (button === 'right') + return 2; +} + +function toButtonsMask(buttons: Set): number { + let mask = 0; + if (buttons.has('left')) + mask |= 1; + if (buttons.has('right')) + mask |= 2; + if (buttons.has('middle')) + mask |= 4; + return mask; +} + export class RawKeyboardImpl implements input.RawKeyboard { private _client: JugglerSession; @@ -73,100 +93,45 @@ export class RawKeyboardImpl implements input.RawKeyboard { } } -export class Mouse implements input.MouseOperations { - _client: JugglerSession; - _keyboard: input.Keyboard; - _x: number; - _y: number; - _buttons: number; +export class RawMouseImpl implements input.RawMouse { + private _client: JugglerSession; - constructor(client: JugglerSession, keyboard: input.Keyboard) { + constructor(client: JugglerSession) { this._client = client; - this._keyboard = keyboard; - this._x = 0; - this._y = 0; - this._buttons = 0; } - async move(x: number, y: number, options: { steps?: number; } | undefined = {}) { - const {steps = 1} = options; - const fromX = this._x, fromY = this._y; - this._x = x; - this._y = y; - for (let i = 1; i <= steps; i++) { - await this._client.send('Page.dispatchMouseEvent', { - type: 'mousemove', - button: 0, - x: fromX + (this._x - fromX) * (i / steps), - y: fromY + (this._y - fromY) * (i / steps), - modifiers: toModifiersMask(this._keyboard._modifiers()), - buttons: this._buttons, - }); - } + async move(x: number, y: number, button: input.Button | 'none', buttons: Set, modifiers: Set): Promise { + await this._client.send('Page.dispatchMouseEvent', { + type: 'mousemove', + button: 0, + buttons: toButtonsMask(buttons), + x, + y, + modifiers: toModifiersMask(modifiers) + }); } - async down(options: { button?: string; clickCount?: number; } | undefined = {}) { - const { - button = 'left', - clickCount = 1 - } = options; - if (button === 'left') - this._buttons |= 1; - if (button === 'right') - this._buttons |= 2; - if (button === 'middle') - this._buttons |= 4; + async down(x: number, y: number, button: input.Button, buttons: Set, modifiers: Set, clickCount: number): Promise { await this._client.send('Page.dispatchMouseEvent', { type: 'mousedown', - button: this._buttonNameToButton(button), - x: this._x, - y: this._y, - modifiers: toModifiersMask(this._keyboard._modifiers()), - clickCount, - buttons: this._buttons, + button: toButtonNumber(button), + buttons: toButtonsMask(buttons), + x, + y, + modifiers: toModifiersMask(modifiers), + clickCount }); } - _buttonNameToButton(buttonName: string): number { - if (buttonName === 'left') - return 0; - if (buttonName === 'middle') - return 1; - if (buttonName === 'right') - return 2; - } - - async up(options: { button?: string; clickCount?: number; } | undefined = {}) { - const { - button = 'left', - clickCount = 1 - } = options; - if (button === 'left') - this._buttons &= ~1; - if (button === 'right') - this._buttons &= ~2; - if (button === 'middle') - this._buttons &= ~4; + async up(x: number, y: number, button: input.Button, buttons: Set, modifiers: Set, clickCount: number): Promise { await this._client.send('Page.dispatchMouseEvent', { type: 'mouseup', - button: this._buttonNameToButton(button), - x: this._x, - y: this._y, - modifiers: toModifiersMask(this._keyboard._modifiers()), - clickCount: clickCount, - buttons: this._buttons, + button: toButtonNumber(button), + buttons: toButtonsMask(buttons), + x, + y, + modifiers: toModifiersMask(modifiers), + clickCount }); } - - async click(x: number, y: number, options?: input.ClickOptions) { - await new input.MouseClicker(this).click(x, y, options); - } - - async dblclick(x: number, y: number, options?: input.ClickOptions) { - await new input.MouseClicker(this).dblclick(x, y, options); - } - - async tripleclick(x: number, y: number, options?: input.ClickOptions) { - await new input.MouseClicker(this).tripleclick(x, y, options); - } } diff --git a/src/firefox/Page.ts b/src/firefox/Page.ts index 493a9f21fb..3ade807478 100644 --- a/src/firefox/Page.ts +++ b/src/firefox/Page.ts @@ -11,7 +11,7 @@ import { Events } from './events'; import { Accessibility } from './features/accessibility'; import { Interception } from './features/interception'; import { FrameManager, FrameManagerEvents, normalizeWaitUntil, Frame } from './FrameManager'; -import { Mouse, RawKeyboardImpl } from './Input'; +import { RawMouseImpl, RawKeyboardImpl } from './Input'; import { createHandle, ElementHandle, JSHandle } from './JSHandle'; import { NavigationWatchdog } from './NavigationWatchdog'; import { NetworkManager, NetworkManagerEvents, Request, Response } from './NetworkManager'; @@ -25,7 +25,7 @@ export class Page extends EventEmitter { private _session: JugglerSession; private _target: Target; private _keyboard: input.Keyboard; - private _mouse: Mouse; + private _mouse: input.Mouse; readonly accessibility: Accessibility; readonly interception: Interception; private _closed: boolean; @@ -60,7 +60,7 @@ export class Page extends EventEmitter { this._session = session; this._target = target; this._keyboard = new input.Keyboard(new RawKeyboardImpl(session)); - this._mouse = new Mouse(session, this._keyboard); + this._mouse = new input.Mouse(new RawMouseImpl(session), this._keyboard); this.accessibility = new Accessibility(session); this._closed = false; this._pageBindings = new Map(); @@ -348,7 +348,7 @@ export class Page extends EventEmitter { return this._keyboard; } - get mouse(){ + get mouse(): input.Mouse { return this._mouse; } diff --git a/src/firefox/api.ts b/src/firefox/api.ts index fc6a3c35ba..3897ef4585 100644 --- a/src/firefox/api.ts +++ b/src/firefox/api.ts @@ -10,8 +10,7 @@ export { Accessibility } from './features/accessibility'; export { Interception } from './features/interception'; export { Permissions } from './features/permissions'; export { Frame } from './FrameManager'; -export { Mouse } from './Input'; -export { Keyboard } from '../input'; +export { Mouse, Keyboard } from '../input'; export { ElementHandle, JSHandle } from './JSHandle'; export { Request, Response } from './NetworkManager'; export { ConsoleMessage, Page } from './Page'; diff --git a/src/input.ts b/src/input.ts index 09fd7b4971..3f23766bda 100644 --- a/src/input.ts +++ b/src/input.ts @@ -172,33 +172,66 @@ export class Keyboard { } } -export interface MouseOperations { - move(x: number, y: number, options?: { steps?: number; }): Promise; - down(options?: { button?: Button; clickCount?: number; }): Promise; - up(options?: { button?: Button; clickCount?: number; }): Promise; +export interface RawMouse { + move(x: number, y: number, button: Button | 'none', buttons: Set