playwright/src/webkit/Input.ts

138 lines
4.2 KiB
TypeScript
Raw Normal View History

2019-11-19 03:18:28 +01:00
/**
* Copyright 2017 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* 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.
*/
import * as input from '../input';
import { TargetSession } from './Connection';
2019-11-19 03:18:28 +01:00
function toModifiersMask(modifiers: Set<input.Modifier>): number {
// From Source/WebKit/Shared/WebEvent.h
let mask = 0;
if (modifiers.has('Shift'))
mask |= 1;
if (modifiers.has('Control'))
mask |= 2;
if (modifiers.has('Alt'))
mask |= 4;
if (modifiers.has('Meta'))
mask |= 8;
return mask;
}
2019-11-19 03:18:28 +01:00
export class RawKeyboardImpl implements input.RawKeyboard {
2019-11-19 03:18:28 +01:00
private _session: TargetSession;
constructor(session: TargetSession) {
this._session = session;
}
async keydown(modifiers: Set<input.Modifier>, code: string, keyCode: number, key: string, location: number, autoRepeat: boolean, text: string | undefined): Promise<void> {
2019-11-19 03:18:28 +01:00
await this._session.send('Input.dispatchKeyEvent', {
type: 'keyDown',
modifiers: toModifiersMask(modifiers),
windowsVirtualKeyCode: keyCode,
code,
key,
text,
2019-11-19 03:18:28 +01:00
unmodifiedText: text,
autoRepeat,
isKeypad: location === input.keypadLocation
2019-11-19 03:18:28 +01:00
});
}
async keyup(modifiers: Set<input.Modifier>, code: string, keyCode: number, key: string, location: number): Promise<void> {
2019-11-19 03:18:28 +01:00
await this._session.send('Input.dispatchKeyEvent', {
type: 'keyUp',
modifiers: toModifiersMask(modifiers),
key,
windowsVirtualKeyCode: keyCode,
code,
isKeypad: location === input.keypadLocation
2019-11-19 03:18:28 +01:00
});
}
async sendText(text: string): Promise<void> {
await this._session.send('Page.insertText', { text });
}
2019-11-19 03:18:28 +01:00
}
export class Mouse implements input.MouseOperations {
2019-11-19 03:18:28 +01:00
private _client: TargetSession;
private _keyboard: input.Keyboard;
2019-11-19 03:18:28 +01:00
private _x = 0;
private _y = 0;
private _button: 'none' | input.Button = 'none';
2019-11-19 03:18:28 +01:00
constructor(client: TargetSession, keyboard: input.Keyboard) {
2019-11-19 03:18:28 +01:00
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: 'move',
button: this._button,
x: fromX + (this._x - fromX) * (i / steps),
y: fromY + (this._y - fromY) * (i / steps),
modifiers: toModifiersMask(this._keyboard._modifiers())
2019-11-19 03:18:28 +01:00
});
}
}
async down(options: { button?: input.Button; clickCount?: number; } = {}) {
2019-11-19 03:18:28 +01:00
const {button = 'left', clickCount = 1} = options;
this._button = button;
await this._client.send('Input.dispatchMouseEvent', {
type: 'down',
button,
x: this._x,
y: this._y,
modifiers: toModifiersMask(this._keyboard._modifiers()),
2019-11-19 03:18:28 +01:00
clickCount
});
}
async up(options: { button?: input.Button; clickCount?: number; } = {}) {
2019-11-19 03:18:28 +01:00
const {button = 'left', clickCount = 1} = options;
this._button = 'none';
await this._client.send('Input.dispatchMouseEvent', {
type: 'up',
button,
x: this._x,
y: this._y,
modifiers: toModifiersMask(this._keyboard._modifiers()),
2019-11-19 03:18:28 +01:00
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);
}
2019-11-19 03:18:28 +01:00
}