Skip to content

Commit

Permalink
chore: move shared utilities to src/utils (#3575)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman authored Aug 22, 2020
1 parent b909924 commit 655013d
Show file tree
Hide file tree
Showing 66 changed files with 341 additions and 324 deletions.
13 changes: 3 additions & 10 deletions src/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import { helper } from './helper';
import * as network from './network';
import * as path from 'path';
import { Page, PageBinding } from './page';
import { TimeoutSettings } from './timeoutSettings';
import { TimeoutSettings } from './utils/timeoutSettings';
import * as frames from './frames';
import * as types from './types';
import { Download } from './download';
import { Browser } from './browser';
import { EventEmitter } from 'events';
import { Progress } from './progress';
import { DebugController } from './debug/debugController';
import { isDebugMode } from './utils/utils';

export class Screencast {
readonly path: string;
Expand Down Expand Up @@ -68,7 +69,7 @@ export abstract class BrowserContext extends EventEmitter {
}

async _initialize() {
if (helper.isDebugMode())
if (isDebugMode())
new DebugController(this);
}

Expand Down Expand Up @@ -250,24 +251,16 @@ export function verifyGeolocation(geolocation?: types.Geolocation) {
return;
geolocation.accuracy = geolocation.accuracy || 0;
const { longitude, latitude, accuracy } = geolocation;
if (!helper.isNumber(longitude))
throw new Error(`geolocation.longitude: expected number, got ${typeof longitude}`);
if (longitude < -180 || longitude > 180)
throw new Error(`geolocation.longitude: precondition -180 <= LONGITUDE <= 180 failed.`);
if (!helper.isNumber(latitude))
throw new Error(`geolocation.latitude: expected number, got ${typeof latitude}`);
if (latitude < -90 || latitude > 90)
throw new Error(`geolocation.latitude: precondition -90 <= LATITUDE <= 90 failed.`);
if (!helper.isNumber(accuracy))
throw new Error(`geolocation.accuracy: expected number, got ${typeof accuracy}`);
if (accuracy < 0)
throw new Error(`geolocation.accuracy: precondition 0 <= ACCURACY failed.`);
}

export function verifyProxySettings(proxy: types.ProxySettings): types.ProxySettings {
let { server, bypass } = proxy;
if (!helper.isString(server))
throw new Error(`Invalid proxy.server: ` + server);
let url = new URL(server);
if (!['http:', 'https:', 'socks5:'].includes(url.protocol)) {
url = new URL('http://' + server);
Expand Down
2 changes: 1 addition & 1 deletion src/chromium/crBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { Browser, BrowserOptions } from '../browser';
import { assertBrowserContextIsNotOwned, BrowserContext, validateBrowserContextOptions, verifyGeolocation } from '../browserContext';
import { assert } from '../helper';
import { assert } from '../utils/utils';
import * as network from '../network';
import { Page, PageBinding, Worker } from '../page';
import { ConnectionTransport } from '../transport';
Expand Down
3 changes: 2 additions & 1 deletion src/chromium/crConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
* limitations under the License.
*/

import { assert, debugLogger } from '../helper';
import { assert } from '../utils/utils';
import { ConnectionTransport, ProtocolRequest, ProtocolResponse } from '../transport';
import { Protocol } from './protocol';
import { EventEmitter } from 'events';
import { rewriteErrorMessage } from '../utils/stackTrace';
import { debugLogger } from '../utils/debugLogger';

export const ConnectionEvents = {
Disconnected: Symbol('ConnectionEvents.Disconnected')
Expand Down
3 changes: 2 additions & 1 deletion src/chromium/crCoverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
*/

import { CRSession } from './crConnection';
import { assert, helper, RegisteredListener } from '../helper';
import { helper, RegisteredListener } from '../helper';
import { Protocol } from './protocol';
import * as types from '../types';
import * as sourceMap from '../utils/sourceMap';
import { assert } from '../utils/utils';

export class CRCoverage {
private _jsCoverage: JSCoverage;
Expand Down
4 changes: 2 additions & 2 deletions src/chromium/crInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as input from '../input';
import * as types from '../types';
import { CRSession } from './crConnection';
import { macEditingCommands } from '../macEditingCommands';
import { helper } from '../helper';
import { isString } from '../utils/utils';

function toModifiersMask(modifiers: Set<types.KeyboardModifier>): number {
let mask = 0;
Expand Down Expand Up @@ -51,7 +51,7 @@ export class RawKeyboardImpl implements input.RawKeyboard {
parts.push(code);
const shortcut = parts.join('+');
let commands = macEditingCommands[shortcut] || [];
if (helper.isString(commands))
if (isString(commands))
commands = [commands];
// remove the trailing : to match the Chromium command names.
return commands.map(c => c.substring(0, c.length - 1));
Expand Down
4 changes: 2 additions & 2 deletions src/chromium/crNetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

import { CRSession } from './crConnection';
import { Page } from '../page';
import { assert, helper, RegisteredListener } from '../helper';
import { helper, RegisteredListener } from '../helper';
import { Protocol } from './protocol';
import * as network from '../network';
import * as frames from '../frames';
import * as types from '../types';
import { CRPage } from './crPage';
import { headersObjectToArray } from '../converters';
import { assert, headersObjectToArray } from '../utils/utils';

export class CRNetworkManager {
private _client: CRSession;
Expand Down
4 changes: 2 additions & 2 deletions src/chromium/crPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import * as dom from '../dom';
import * as frames from '../frames';
import { helper, RegisteredListener, assert } from '../helper';
import { helper, RegisteredListener } from '../helper';
import * as network from '../network';
import { CRSession, CRConnection, CRSessionEvents } from './crConnection';
import { CRExecutionContext } from './crExecutionContext';
Expand All @@ -36,7 +36,7 @@ import * as types from '../types';
import { ConsoleMessage } from '../console';
import * as sourceMap from '../utils/sourceMap';
import { rewriteErrorMessage } from '../utils/stackTrace';
import { headersArrayToObject } from '../converters';
import { assert, headersArrayToObject } from '../utils/utils';


const UTILITY_WORLD_NAME = '__playwright_utility_world__';
Expand Down
2 changes: 1 addition & 1 deletion src/chromium/crPdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { assert } from '../helper';
import { assert } from '../utils/utils';
import * as types from '../types';
import { CRSession } from './crConnection';
import { readProtocolStream } from './crProtocolHelper';
Expand Down
2 changes: 1 addition & 1 deletion src/chromium/crProtocolHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Protocol } from './protocol';
import * as fs from 'fs';
import * as util from 'util';
import * as types from '../types';
import { mkdirIfNeeded } from '../helper';
import { mkdirIfNeeded } from '../utils/utils';

export function getExceptionMessage(exceptionDetails: Protocol.Runtime.ExceptionDetails): string {
if (exceptionDetails.exception)
Expand Down
33 changes: 0 additions & 33 deletions src/converters.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* limitations under the License.
*/

import { assert, debugLogger } from './helper';
import { assert } from './utils/utils';
import { debugLogger } from './utils/debugLogger';

type OnHandle = (accept: boolean, promptText?: string) => Promise<void>;

Expand Down
3 changes: 1 addition & 2 deletions src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import * as frames from './frames';
import { assert, helper } from './helper';
import { assert } from './utils/utils';
import InjectedScript from './injected/injectedScript';
import * as injectedScriptSource from './generated/injectedScriptSource';
import * as debugScriptSource from './generated/debugScriptSource';
Expand Down Expand Up @@ -429,7 +429,6 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {

async _fill(progress: Progress, value: string, options: types.NavigatingActionWaitOptions): Promise<'error:notconnected' | 'done'> {
progress.log(`elementHandle.fill("${value}")`);
assert(helper.isString(value), `value: expected string, got ${typeof value}`);
return this._page._frameManager.waitForSignalsCreatedBy(progress, options.noWaitAfter, async () => {
progress.log(' waiting for element to be visible, enabled and editable');
const poll = await this._evaluateHandleInUtility(([injected, node, value]) => {
Expand Down
2 changes: 1 addition & 1 deletion src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as fs from 'fs';
import * as util from 'util';
import { Page } from './page';
import { Readable } from 'stream';
import { assert, mkdirIfNeeded } from './helper';
import { assert, mkdirIfNeeded } from './utils/utils';

export class Download {
private _downloadsPath: string;
Expand Down
3 changes: 2 additions & 1 deletion src/firefox/ffBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

import { Browser, BrowserOptions } from '../browser';
import { assertBrowserContextIsNotOwned, BrowserContext, validateBrowserContextOptions, verifyGeolocation } from '../browserContext';
import { assert, helper, RegisteredListener } from '../helper';
import { helper, RegisteredListener } from '../helper';
import { assert } from '../utils/utils';
import * as network from '../network';
import { Page, PageBinding } from '../page';
import { ConnectionTransport } from '../transport';
Expand Down
3 changes: 2 additions & 1 deletion src/firefox/ffConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
*/

import { EventEmitter } from 'events';
import { assert, debugLogger } from '../helper';
import { assert } from '../utils/utils';
import { ConnectionTransport, ProtocolRequest, ProtocolResponse } from '../transport';
import { Protocol } from './protocol';
import { rewriteErrorMessage } from '../utils/stackTrace';
import { debugLogger } from '../utils/debugLogger';

export const ConnectionEvents = {
Disconnected: Symbol('Disconnected'),
Expand Down
3 changes: 2 additions & 1 deletion src/firefox/ffPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import * as dialog from '../dialog';
import * as dom from '../dom';
import * as frames from '../frames';
import { assert, helper, RegisteredListener } from '../helper';
import { helper, RegisteredListener } from '../helper';
import { assert } from '../utils/utils';
import { Page, PageBinding, PageDelegate, Worker } from '../page';
import { kScreenshotDuringNavigationError } from '../screenshotter';
import * as types from '../types';
Expand Down
19 changes: 8 additions & 11 deletions src/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { ConsoleMessage } from './console';
import * as dom from './dom';
import { assert, helper, RegisteredListener, debugLogger } from './helper';
import { helper, RegisteredListener } from './helper';
import * as js from './javascript';
import * as network from './network';
import { Page } from './page';
Expand All @@ -26,6 +26,8 @@ import * as types from './types';
import { BrowserContext } from './browserContext';
import { Progress, ProgressController } from './progress';
import { EventEmitter } from 'events';
import { assert, makeWaitForNextTask } from './utils/utils';
import { debugLogger } from './utils/debugLogger';

type ContextData = {
contextPromise: Promise<dom.FrameExecutionContext>;
Expand Down Expand Up @@ -134,7 +136,7 @@ export class FrameManager {
await barrier.waitFor();
this._signalBarriers.delete(barrier);
// Resolve in the next task, after all waitForNavigations.
await new Promise(helper.makeWaitForNextTask());
await new Promise(makeWaitForNextTask());
return result;
}

Expand Down Expand Up @@ -879,20 +881,15 @@ export class Frame extends EventEmitter {
}

async _waitForFunctionExpression<R>(expression: string, isFunction: boolean, arg: any, options: types.WaitForFunctionOptions = {}): Promise<js.SmartHandle<R>> {
const { polling = 'raf' } = options;
if (helper.isString(polling))
assert(polling === 'raf', 'Unknown polling option: ' + polling);
else if (helper.isNumber(polling))
assert(polling > 0, 'Cannot poll with non-positive interval: ' + polling);
else
throw new Error('Unknown polling option: ' + polling);
if (typeof options.pollingInterval === 'number')
assert(options.pollingInterval > 0, 'Cannot poll with non-positive interval: ' + options.pollingInterval);
const predicateBody = isFunction ? 'return (' + expression + ')(arg)' : 'return (' + expression + ')';
const task: dom.SchedulableTask<R> = injectedScript => injectedScript.evaluateHandle((injectedScript, { predicateBody, polling, arg }) => {
const innerPredicate = new Function('arg', predicateBody) as (arg: any) => R;
if (polling === 'raf')
if (typeof polling !== 'number')
return injectedScript.pollRaf((progress, continuePolling) => innerPredicate(arg) || continuePolling);
return injectedScript.pollInterval(polling, (progress, continuePolling) => innerPredicate(arg) || continuePolling);
}, { predicateBody, polling, arg });
}, { predicateBody, polling: options.pollingInterval, arg });
return this._page._runAbortableTask(
progress => this._scheduleRerunnableHandleTask(progress, 'main', task),
this._page._timeoutSettings.timeout(options));
Expand Down
Loading

0 comments on commit 655013d

Please sign in to comment.