Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"gemini-configparser": "1.4.1",
"get-port": "5.1.1",
"import-meta-resolve": "4.0.0",
"load-esm": "1.0.2",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contains like 1 LOC:

module.exports = { loadEsm: (module) => import(module) };

In our project, typescript compiles all "import" to "require" and we can't prevent it
So, this package is used

"local-pkg": "0.4.3",
"lodash": "4.17.21",
"looks-same": "9.0.1",
Expand Down
9 changes: 7 additions & 2 deletions src/browser-installer/chrome/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { spawn, type ChildProcess } from "child_process";
import getPort from "get-port";
import waitPort from "wait-port";
import { pipeLogsWithPrefix } from "../../dev-server/utils";
import { DRIVER_WAIT_TIMEOUT } from "../constants";
import { DRIVER_WAIT_INTERVAL, DRIVER_WAIT_TIMEOUT } from "../constants";
import { getMilestone } from "../utils";
import { installChrome, resolveLatestChromeVersion } from "./browser";
import { installChromeDriver } from "./driver";
Expand Down Expand Up @@ -36,7 +36,12 @@ export const runChromeDriver = async (

process.once("exit", () => chromeDriver.kill());

await waitPort({ port: randomPort, output: "silent", timeout: DRIVER_WAIT_TIMEOUT });
await waitPort({
port: randomPort,
output: "silent",
timeout: DRIVER_WAIT_TIMEOUT,
interval: DRIVER_WAIT_INTERVAL,
});

return { gridUrl, process: chromeDriver, port: randomPort };
};
1 change: 1 addition & 0 deletions src/browser-installer/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const MIN_CHROMIUM_MAC_ARM_VERSION = 93;
export const MIN_CHROMIUM_VERSION = 73;
export const MIN_FIREFOX_VERSION = 60;
export const MIN_EDGEDRIVER_VERSION = 94;
export const DRIVER_WAIT_INTERVAL = 25; // 20ms

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are almost blocked while waiting for webdriver launch
Checking "if port is busy" more often saves some time by price of some CPU cycles

export const DRIVER_WAIT_TIMEOUT = 10 * 1000; // 10s
export const LINUX_UBUNTU_RELEASE_ID = "ubuntu";
export const LINUX_RUNTIME_LIBRARIES_PATH_ENV_NAME = "LD_LIBRARY_PATH";
Expand Down
9 changes: 7 additions & 2 deletions src/browser-installer/edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { spawn, type ChildProcess } from "child_process";
import getPort from "get-port";
import waitPort from "wait-port";
import { pipeLogsWithPrefix } from "../../dev-server/utils";
import { DRIVER_WAIT_TIMEOUT } from "../constants";
import { DRIVER_WAIT_INTERVAL, DRIVER_WAIT_TIMEOUT } from "../constants";

export { resolveEdgeVersion } from "./browser";
export { installEdgeDriver };
Expand All @@ -27,7 +27,12 @@ export const runEdgeDriver = async (

process.once("exit", () => edgeDriver.kill());

await waitPort({ port: randomPort, output: "silent", timeout: DRIVER_WAIT_TIMEOUT });
await waitPort({
port: randomPort,
output: "silent",
timeout: DRIVER_WAIT_TIMEOUT,
interval: DRIVER_WAIT_INTERVAL,
});

return { gridUrl, process: edgeDriver, port: randomPort };
};
9 changes: 7 additions & 2 deletions src/browser-installer/firefox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import waitPort from "wait-port";
import { installFirefox, resolveLatestFirefoxVersion } from "./browser";
import { installLatestGeckoDriver } from "./driver";
import { pipeLogsWithPrefix } from "../../dev-server/utils";
import { DRIVER_WAIT_TIMEOUT } from "../constants";
import { DRIVER_WAIT_INTERVAL, DRIVER_WAIT_TIMEOUT } from "../constants";
import { getUbuntuLinkerEnv, isUbuntu } from "../ubuntu-packages";

export { installFirefox, resolveLatestFirefoxVersion, installLatestGeckoDriver };
Expand Down Expand Up @@ -41,7 +41,12 @@ export const runGeckoDriver = async (

process.once("exit", () => geckoDriver.kill());

await waitPort({ port: randomPort, output: "silent", timeout: DRIVER_WAIT_TIMEOUT });
await waitPort({
port: randomPort,
output: "silent",
timeout: DRIVER_WAIT_TIMEOUT,
interval: DRIVER_WAIT_INTERVAL,
});

return { gridUrl, process: geckoDriver, port: randomPort };
};
9 changes: 7 additions & 2 deletions src/browser-installer/safari/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { spawn, type ChildProcess } from "child_process";
import getPort from "get-port";
import waitPort from "wait-port";
import { pipeLogsWithPrefix } from "../../dev-server/utils";
import { DRIVER_WAIT_TIMEOUT, SAFARIDRIVER_PATH } from "../constants";
import { DRIVER_WAIT_INTERVAL, DRIVER_WAIT_TIMEOUT, SAFARIDRIVER_PATH } from "../constants";

export { resolveSafariVersion } from "./browser";

Expand All @@ -26,7 +26,12 @@ export const runSafariDriver = async ({ debug = false }: { debug?: boolean } = {

process.once("exit", () => safariDriver.kill());

await waitPort({ port: randomPort, output: "silent", timeout: DRIVER_WAIT_TIMEOUT });
await waitPort({
port: randomPort,
output: "silent",
timeout: DRIVER_WAIT_TIMEOUT,
interval: DRIVER_WAIT_INTERVAL,
});

return { gridUrl, process: safariDriver, port: randomPort };
};
3 changes: 2 additions & 1 deletion src/browser-pool/basic-pool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import debug from "debug";
import _ from "lodash";

import { NewBrowser } from "../browser/new-browser";
import type { NewBrowser } from "../browser/new-browser";
import { CancelledError } from "./cancelled-error";
import { AsyncEmitter, MasterEvents } from "../events";
import { BrowserOpts, Pool } from "./types";
Expand Down Expand Up @@ -32,6 +32,7 @@ export class BasicPool implements Pool {
}

async getBrowser(id: string, opts: BrowserOpts = {}): Promise<NewBrowser> {
const { NewBrowser } = await import("../browser/new-browser");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"NewBrowser" takes some time to import
using lazy-load here (and in many other places) to reduce "time to require" and therefore - time to start workers, which is crucial

const browser = NewBrowser.create(this._config, { ...opts, id, wdPool: this._wdPool, emitter: this._emitter });

try {
Expand Down
3 changes: 2 additions & 1 deletion src/browser-pool/webdriver-pool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ChildProcess } from "child_process";
import { resolveBrowserVersion, runBrowserDriver } from "../browser-installer";
import { getNormalizedBrowserName } from "../utils/browser";
import type { SupportedBrowser } from "../browser-installer";

Expand Down Expand Up @@ -33,6 +32,7 @@ export class WebdriverPool {
);
}

const { resolveBrowserVersion } = await import("../browser-installer");
const browserVersionNormalized = browserVersion || (await resolveBrowserVersion(browserNameNormalized));

const wdProcesses = this.driverProcess.get(browserNameNormalized)?.get(browserVersionNormalized) ?? {};
Expand Down Expand Up @@ -76,6 +76,7 @@ export class WebdriverPool {
browserVersion: string,
{ debug = false } = {},
): Promise<WdProcess> {
const { runBrowserDriver } = await import("../browser-installer");
const driver = await runBrowserDriver(browserName, browserVersion, { debug });

if (!this.driverProcess.has(browserName)) {
Expand Down
6 changes: 4 additions & 2 deletions src/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const CUSTOM_SESSION_OPTS = [
"user",
"key",
"region",
];
] as const;

export type BrowserOpts = {
id: string;
Expand Down Expand Up @@ -122,7 +122,9 @@ export class Browser {
});
}

protected _getSessionOptsFromConfig(optNames = CUSTOM_SESSION_OPTS): Record<string, unknown> {
protected _getSessionOptsFromConfig(
optNames: ReadonlyArray<(typeof CUSTOM_SESSION_OPTS)[number]> = CUSTOM_SESSION_OPTS,
): Record<string, unknown> {
return optNames.reduce((options: Record<string, unknown>, optName) => {
if (optName === "transformRequest") {
options[optName] = (req: RequestOptions): RequestOptions => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageInfo, RefImageInfo } from "../../../../types";
import type { ImageInfo, RefImageInfo } from "../../../../types";

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"import" -> "import type" also saves some time


export class BaseStateError extends Error {
constructor(public stateName: string, public currImg: ImageInfo, public refImg: RefImageInfo) {
Expand Down
6 changes: 2 additions & 4 deletions src/browser/commands/assert-view/errors/image-diff-error.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ImageInfo, RefImageInfo } from "../../../../types";

import { Image } from "../../../../image";
import { BaseStateError } from "./base-state-error";

import type { ImageInfo, RefImageInfo } from "../../../../types";
import type { LooksSameOptions, LooksSameResult } from "looks-same";

interface DiffOptions extends LooksSameOptions {
Expand Down Expand Up @@ -110,6 +108,6 @@ export class ImageDiffError extends BaseStateError {
}

saveDiffTo(diffPath: string): Promise<null> {
return Image.buildDiff({ diff: diffPath, ...this.diffOpts });
return import("../../../../image").then(m => m.Image.buildDiff({ diff: diffPath, ...this.diffOpts }));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Image" is fat, and we dont need to requrie "Image" module every time

}
}
2 changes: 1 addition & 1 deletion src/browser/commands/moveCursorTo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainablePromiseElement } from "@testplane/webdriverio";
import type { ChainablePromiseElement } from "@testplane/webdriverio";
import type { Browser } from "../types";

type MoveToOptions = {
Expand Down
8 changes: 4 additions & 4 deletions src/browser/commands/openAndWait.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import _ from "lodash";
import { Matches } from "@testplane/webdriverio";
import PageLoader from "../../utils/page-loader";
import type { Matches } from "@testplane/webdriverio";
import type { Browser } from "../types";
import { BrowserConfig } from "../../config/browser-config";
import type { BrowserConfig } from "../../config/browser-config";

interface WaitOpts {
selector?: string | string[];
Expand All @@ -25,7 +24,7 @@ const is: Record<string, (match: Matches) => boolean> = {
};

const makeOpenAndWaitCommand = (config: BrowserConfig, session: WebdriverIO.Browser) =>
function openAndWait(
async function openAndWait(
this: WebdriverIO.Browser,
uri: string,
{
Expand All @@ -39,6 +38,7 @@ const makeOpenAndWaitCommand = (config: BrowserConfig, session: WebdriverIO.Brow
timeout = config.openAndWaitOpts?.timeout || config?.pageLoadTimeout || 0,
}: WaitOpts = {},
): Promise<string | void> {
const PageLoader = await import("../../utils/page-loader").then(m => m.default);
const isChrome = config.desiredCapabilities?.browserName === "chrome";
const isCDP = config.automationProtocol === "devtools";

Expand Down
2 changes: 1 addition & 1 deletion src/browser/commands/runStep.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from "lodash";
import { Browser } from "../browser";
import type { Browser } from "../browser";

const addRunStepCommand = (browser: Browser): void => {
const { publicAPI: session } = browser;
Expand Down
5 changes: 3 additions & 2 deletions src/browser/commands/switchToRepl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import repl from "node:repl";
import path from "node:path";
import type repl from "node:repl";
import { getEventListeners } from "node:events";
import chalk from "chalk";
import RuntimeConfig from "../../config/runtime-config";
Expand Down Expand Up @@ -60,7 +60,8 @@ export default (browser: Browser): void => {
const testCwd = path.dirname(session.executionContext.ctx.currentTest.file!);
process.chdir(testCwd);

const replServer = repl.start({ prompt: "> " });
const replServer = await import("node:repl").then(m => m.start({ prompt: "> " }));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that "node:repl" only imported here, we could save ~10ms by lazy-loading it


browser.applyState({ onReplMode: true });

runtimeCfg.extend({ replServer });
Expand Down
4 changes: 2 additions & 2 deletions src/browser/commands/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AssertViewOpts } from "../../config/types";
import { ChainablePromiseElement } from "@testplane/webdriverio";
import type { AssertViewOpts } from "../../config/types";
import type { ChainablePromiseElement } from "@testplane/webdriverio";

export type AssertViewCommandWithSelector = (
this: WebdriverIO.Browser,
Expand Down
11 changes: 7 additions & 4 deletions src/browser/existing-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import { NEW_ISSUE_LINK } from "../constants/help";
import type { Options } from "@testplane/wdio-types";
import { runWithoutHistory } from "./history";

const OPTIONAL_SESSION_OPTS = ["transformRequest", "transformResponse"];

interface SessionOptions {
sessionId: string;
sessionCaps?: WebdriverIO.Capabilities;
Expand Down Expand Up @@ -82,6 +80,8 @@ export class ExistingBrowser extends Browser {
this._startCollectingCustomCommands();
}

const isolationPromise = this._performIsolation({ sessionCaps, sessionOpts });

this._extendStacktrace();
this._addSteps();
this._addHistory();
Comment on lines 85 to 87

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods take ~40ms each
We could do it while performing isolation (CPU is idle, while waiting for browser to create new page...)

Expand All @@ -95,7 +95,10 @@ export class ExistingBrowser extends Browser {
"testplane: init browser",
async () => {
this._addCommands();
await this._performIsolation({ sessionCaps, sessionOpts });

await isolationPromise;

this._callstackHistory?.clear();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because order in "add history" and "perform isolation" becomes undefined, removing this step, if its completed


try {
this.config.prepareBrowser && this.config.prepareBrowser(this.publicAPI);
Expand Down Expand Up @@ -227,7 +230,7 @@ export class ExistingBrowser extends Browser {
const opts: AttachOptions = {
sessionId,
...sessionOpts,
...this._getSessionOptsFromConfig(OPTIONAL_SESSION_OPTS),
...this._getSessionOptsFromConfig(["transformRequest", "transformResponse"]),
...detectedSessionEnvFlags,
...this._config.sessionEnvFlags,
options: sessionOpts,
Expand Down
Loading