Allow to configure default delay time

This commit is contained in:
Konstantin Pschera 2020-02-19 18:36:37 +01:00
parent 33824aa1d7
commit 1ed4b70719
6 changed files with 80 additions and 18 deletions

View file

@ -270,6 +270,7 @@ await context.close();
- [browserContext.setCookies(cookies)](#browsercontextsetcookiescookies)
- [browserContext.setDefaultNavigationTimeout(timeout)](#browsercontextsetdefaultnavigationtimeouttimeout)
- [browserContext.setDefaultTimeout(timeout)](#browsercontextsetdefaulttimeouttimeout)
- [browserContext.setDefaultDelay(delay)](#browsercontextsetdefaultdelaydelay)
- [browserContext.setGeolocation(geolocation)](#browsercontextsetgeolocationgeolocation)
- [browserContext.setPermissions(origin, permissions[])](#browsercontextsetpermissionsorigin-permissions)
<!-- GEN:stop -->
@ -373,6 +374,11 @@ This setting will change the default maximum time for all the methods accepting
> **NOTE** [`page.setDefaultNavigationTimeout`](#pagesetdefaultnavigationtimeouttimeout), [`page.setDefaultTimeout`](#pagesetdefaulttimeouttimeout) and [`browserContext.setDefaultNavigationTimeout`](#browsercontextsetdefaultnavigationtimeouttimeout) take priority over [`browserContext.setDefaultTimeout`](#browserContextsetdefaulttimeouttimeout).
#### browserContext.setDefaultDelay(delay)
- `delay` <[number]> Delay time in milliseconds
This setting will change the default delay time for all the methods accepting `delay` option.
#### browserContext.setGeolocation(geolocation)
- `geolocation` <[Object]>
- `latitude` <[number]> Latitude between -90 and 90.
@ -513,6 +519,7 @@ page.removeListener('request', logRequest);
- [page.setContent(html[, options])](#pagesetcontenthtml-options)
- [page.setDefaultNavigationTimeout(timeout)](#pagesetdefaultnavigationtimeouttimeout)
- [page.setDefaultTimeout(timeout)](#pagesetdefaulttimeouttimeout)
- [page.setDefaultDelay(delay)](#pagesetdefaultdelaydelay)
- [page.setExtraHTTPHeaders(headers)](#pagesetextrahttpheadersheaders)
- [page.setOfflineMode(enabled)](#pagesetofflinemodeenabled)
- [page.setViewportSize(viewportSize)](#pagesetviewportsizeviewportsize)
@ -1347,6 +1354,11 @@ This setting will change the default maximum time for all the methods accepting
> **NOTE** [`page.setDefaultNavigationTimeout`](#pagesetdefaultnavigationtimeouttimeout) takes priority over [`page.setDefaultTimeout`](#pagesetdefaulttimeouttimeout).
#### page.setDefaultDelay(delay)
- `delay` <[number]> Delay time in milliseconds
This setting will change the default delay time for all the methods accepting `delay` option.
#### page.setExtraHTTPHeaders(headers)
- `headers` <[Object]> An object containing additional HTTP headers to be sent with every request. All header values must be strings.
- returns: <[Promise]>

View file

@ -22,6 +22,7 @@ import { helper } from './helper';
import * as platform from './platform';
import { Events } from './events';
import { TimeoutSettings } from './timeoutSettings';
import { DelaySettings } from './delaySettings';
export interface BrowserContextDelegate {
pages(): Promise<Page[]>;
@ -55,12 +56,14 @@ export class BrowserContext extends platform.EventEmitter {
private readonly _delegate: BrowserContextDelegate;
readonly _options: BrowserContextOptions;
readonly _timeoutSettings: TimeoutSettings;
readonly _delaySettings: DelaySettings;
private _closed = false;
constructor(delegate: BrowserContextDelegate, options: BrowserContextOptions) {
super();
this._delegate = delegate;
this._timeoutSettings = new TimeoutSettings();
this._delaySettings = new DelaySettings();
this._options = { ...options };
if (!this._options.viewport && this._options.viewport !== null)
this._options.viewport = { width: 1280, height: 720 };
@ -89,6 +92,10 @@ export class BrowserContext extends platform.EventEmitter {
this._timeoutSettings.setDefaultTimeout(timeout);
}
setDefaultDelay(delay: number) {
this._delaySettings.setDefaultDelay(delay === 0 ? null : delay);
}
async pages(): Promise<Page[]> {
return this._delegate.pages();
}

35
src/delaySettings.ts Normal file
View file

@ -0,0 +1,35 @@
/**
* Copyright 2019 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.
*/
export class DelaySettings {
private _parent: DelaySettings | undefined;
private _defaultDelay: number | null = null;
constructor(parent?: DelaySettings) {
this._parent = parent;
}
setDefaultDelay(delay: null | number) {
this._defaultDelay = delay;
}
delay(): null | number {
if (this._defaultDelay !== null) return this._defaultDelay;
if (this._parent) return this._parent.delay();
return null;
}
}

View file

@ -17,6 +17,7 @@
import { assert } from './helper';
import * as types from './types';
import * as keyboardLayout from './usKeyboardLayout';
import { Page } from './page';
export type Modifier = 'Alt' | 'Control' | 'Meta' | 'Shift';
export type Button = 'left' | 'right' | 'middle';
@ -58,11 +59,13 @@ export interface RawKeyboard {
export class Keyboard {
private _raw: RawKeyboard;
private _page: Page;
private _pressedModifiers = new Set<Modifier>();
private _pressedKeys = new Set<string>();
constructor(raw: RawKeyboard) {
constructor(raw: RawKeyboard, page: Page) {
this._raw = raw;
this._page = page
}
async down(key: string, options: { text?: string; } = { text: undefined }) {
@ -136,8 +139,8 @@ export class Keyboard {
await this._raw.sendText(text);
}
async type(text: string, options?: { delay?: number }) {
const delay = (options && options.delay) || undefined;
async type(text: string, options: { delay?: number } = {}) {
const { delay = this._page._delaySettings.delay() } = options;
for (const char of text) {
if (keyboardLayout.keyDefinitions[char]) {
await this.press(char, { delay });
@ -150,7 +153,7 @@ export class Keyboard {
}
async press(key: string, options: { delay?: number; text?: string; } = {}) {
const {delay = null} = options;
const { delay = this._page._delaySettings.delay() } = options;
await this.down(key, options);
if (delay)
await new Promise(f => setTimeout(f, options.delay));
@ -189,14 +192,16 @@ export interface RawMouse {
export class Mouse {
private _raw: RawMouse;
private _page: Page;
private _keyboard: Keyboard;
private _x = 0;
private _y = 0;
private _lastButton: 'none' | Button = 'none';
private _buttons = new Set<Button>();
constructor(raw: RawMouse, keyboard: Keyboard) {
constructor(raw: RawMouse, keyboard: Keyboard, page: Page) {
this._raw = raw;
this._page = page;
this._keyboard = keyboard;
}
@ -228,7 +233,7 @@ export class Mouse {
}
async click(x: number, y: number, options: ClickOptions = {}) {
const {delay = null} = options;
const { delay = this._page._delaySettings.delay() } = options;
if (delay !== null) {
await Promise.all([
this.move(x, y),
@ -246,7 +251,7 @@ export class Mouse {
}
async dblclick(x: number, y: number, options: MultiClickOptions = {}) {
const { delay = null } = options;
const { delay = this._page._delaySettings.delay() } = options;
if (delay !== null) {
await this.move(x, y);
await this.down({ ...options, clickCount: 1 });
@ -268,7 +273,7 @@ export class Mouse {
}
async tripleclick(x: number, y: number, options: MultiClickOptions = {}) {
const { delay = null } = options;
const { delay = this._page._delaySettings.delay() } = options;
if (delay !== null) {
await this.move(x, y);
await this.down({ ...options, clickCount: 1 });

View file

@ -23,6 +23,7 @@ import * as js from './javascript';
import * as network from './network';
import { Screenshotter } from './screenshotter';
import { TimeoutSettings } from './timeoutSettings';
import { DelaySettings } from './delaySettings';
import * as types from './types';
import { Events } from './events';
import { BrowserContext } from './browserContext';
@ -104,6 +105,7 @@ export class Page extends platform.EventEmitter {
readonly keyboard: input.Keyboard;
readonly mouse: input.Mouse;
readonly _timeoutSettings: TimeoutSettings;
readonly _delaySettings: DelaySettings;
readonly _delegate: PageDelegate;
readonly _state: PageState;
private _pageBindings = new Map<string, Function>();
@ -143,9 +145,10 @@ export class Page extends platform.EventEmitter {
hasTouch: null,
};
this.accessibility = new accessibility.Accessibility(delegate.getAccessibilityTree.bind(delegate));
this.keyboard = new input.Keyboard(delegate.rawKeyboard);
this.mouse = new input.Mouse(delegate.rawMouse, this.keyboard);
this.keyboard = new input.Keyboard(delegate.rawKeyboard, this);
this.mouse = new input.Mouse(delegate.rawMouse, this.keyboard, this);
this._timeoutSettings = new TimeoutSettings(browserContext._timeoutSettings);
this._delaySettings = new DelaySettings(browserContext._delaySettings);
this._screenshotter = new Screenshotter(this);
this._frameManager = new frames.FrameManager(this);
if (delegate.pdf)
@ -207,6 +210,10 @@ export class Page extends platform.EventEmitter {
this._timeoutSettings.setDefaultTimeout(timeout);
}
setDefaultDelay(delay: number) {
this._delaySettings.setDefaultDelay(delay === 0 ? null : delay);
}
async $(selector: string): Promise<dom.ElementHandle<Element> | null> {
return this.mainFrame().$(selector);
}

View file

@ -37,18 +37,14 @@ export class TimeoutSettings {
navigationTimeout(): number {
if (this._defaultNavigationTimeout !== null)
return this._defaultNavigationTimeout;
if (this._defaultTimeout !== null)
return this._defaultTimeout;
if (this._parent)
return this._parent.navigationTimeout();
if (this._defaultTimeout !== null) return this._defaultTimeout;
if (this._parent) return this._parent.navigationTimeout();
return DEFAULT_TIMEOUT;
}
timeout(): number {
if (this._defaultTimeout !== null)
return this._defaultTimeout;
if (this._parent)
return this._parent.timeout();
if (this._defaultTimeout !== null) return this._defaultTimeout;
if (this._parent) return this._parent.timeout();
return DEFAULT_TIMEOUT;
}
}