@wdio/browser-runner
Version:
A WebdriverIO runner to run unit tests tests in the browser.
263 lines (258 loc) • 10.4 kB
JavaScript
var __typeError = (msg) => {
throw TypeError(msg);
};
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
// src/browser/driver.ts
import { commands } from "virtual:wdio";
import { webdriverMonad, sessionEnvironmentDetector } from "@wdio/utils";
import { getEnvironmentVars, initiateBidi, parseBidiMessage } from "webdriver";
import { MESSAGE_TYPES } from "@wdio/types";
import safeStringify from "safe-stringify";
import EventEmitter from "events";
// src/browser/utils.ts
function getCID() {
var _a;
const urlParamString = new URLSearchParams(window.location.search);
const cid = (
// initial request contains cid as query parameter
urlParamString.get("cid") || // if not provided check for document cookie, set by `@wdio/runner` package
((_a = (document.cookie.split(";") || []).find((c) => c.includes("WDIO_CID"))) == null ? void 0 : _a.trim().split("=").pop())
);
if (!cid) {
throw new Error('"cid" query parameter is missing');
}
return cid;
}
function sanitizeConsoleArgs(args) {
return args.map((arg) => {
if (arg === void 0) {
return "undefined";
}
try {
if (arg && typeof arg.selector === "string" && arg.error) {
return 'WebdriverIO.Element<"'.concat(arg.selector, '">');
}
if (arg && typeof arg.selector === "string" && typeof arg.length === "number") {
return "WebdriverIO.ElementArray<".concat(arg.length, 'x "').concat(arg.selector, '">');
}
if (arg && typeof arg.selector === "string") {
return 'WebdriverIO.Element<"'.concat(arg.selector, '">');
}
if (arg && typeof arg.sessionId === "string") {
return "WebdriverIO.Browser<".concat(arg.capabilities.browserName, ">");
}
} catch {
}
if (arg instanceof HTMLElement || arg && typeof arg === "object" && typeof arg.then === "function" || typeof arg === "function") {
return arg.toString();
}
if (arg instanceof Error) {
return arg.stack;
}
return arg;
});
}
// src/constants.ts
var WDIO_EVENT_NAME = "wdio:workerMessage";
// src/browser/driver.ts
var COMMAND_TIMEOUT = 30 * 1e3;
var CONSOLE_METHODS = ["log", "info", "warn", "error", "debug"];
var HIDE_REPORTER_FOR_COMMANDS = ["saveScreenshot", "savePDF"];
var mochaFramework = document.querySelector("mocha-framework");
var id = 0;
var browser;
var _commandMessages, _ProxyDriver_static, getMockedCommand_fn, handleServerMessage_fn, handleCommandResponse_fn, handleBrowserInitiation_fn, wrapConsolePrototype_fn, commandRequest_fn, consoleMessage_fn;
var ProxyDriver = class {
static async newSession(params, modifier, userPrototype, commandWrapper) {
var _a, _b, _c, _d;
const cid = getCID();
__privateMethod(this, _ProxyDriver_static, wrapConsolePrototype_fn).call(this, cid);
(_a = import.meta.hot) == null ? void 0 : _a.on(WDIO_EVENT_NAME, (payload) => {
try {
__privateMethod(this, _ProxyDriver_static, handleServerMessage_fn).call(this, payload);
} catch (err) {
console.error("Error in handling message: ".concat(err.stack));
}
});
const environment = sessionEnvironmentDetector({ capabilities: params.capabilities, requestedCapabilities: {} });
const environmentPrototype = getEnvironmentVars(environment);
const commandsProcessedInNodeWorld = [...commands, "debug", "saveScreenshot", "savePDF", "emulate", "restore"];
const protocolCommands = commandsProcessedInNodeWorld.reduce((prev, commandName) => {
prev[commandName] = {
value: __privateMethod(this, _ProxyDriver_static, getMockedCommand_fn).call(this, commandName)
};
return prev;
}, {});
delete userPrototype.debug;
delete userPrototype.saveScreenshot;
delete userPrototype.savePDF;
const bidiPrototype = {};
const webSocketUrl = "alwaysMatch" in params.capabilities ? (_b = params.capabilities.alwaysMatch) == null ? void 0 : _b.webSocketUrl : params.capabilities.webSocketUrl;
if (webSocketUrl) {
Object.assign(bidiPrototype, initiateBidi(webSocketUrl));
}
const ee = new EventEmitter();
const eventPrototype = {
emit: { value: ee.emit.bind(ee) },
on: { value: ee.on.bind(ee) },
once: { value: ee.once.bind(ee) },
removeListener: { value: ee.removeListener.bind(ee) },
removeAllListeners: { value: ee.removeAllListeners.bind(ee) },
off: { value: ee.off.bind(ee) }
};
const prototype = {
/**
* custom protocol commands that communicate with Vite
*/
...protocolCommands,
/**
* environment flags
*/
...environmentPrototype,
/**
* unmodified WebdriverIO commands
*/
...userPrototype,
/**
* Bidi commands
*/
...bidiPrototype,
/**
* event emitter commands
*/
...eventPrototype
};
globalThis.wdio = {
execute: (commandName, ...args) => {
return __privateMethod(this, _ProxyDriver_static, getMockedCommand_fn).call(this, commandName)(...args);
},
executeWithScope: (commandName, scope, ...args) => {
return __privateMethod(this, _ProxyDriver_static, getMockedCommand_fn).call(this, commandName, scope)(...args);
}
};
const monad = webdriverMonad(params, modifier, prototype);
const client = monad(window.__wdioEnv__.sessionId, commandWrapper);
if (params.capabilities.webSocketUrl && client._bidiHandler) {
if (await client._bidiHandler.connect()) {
(_c = client._bidiHandler.socket) == null ? void 0 : _c.on("message", parseBidiMessage.bind(client));
}
}
(_d = import.meta.hot) == null ? void 0 : _d.send(WDIO_EVENT_NAME, {
type: MESSAGE_TYPES.initiateBrowserStateRequest,
value: { cid }
});
browser = client;
return client;
}
};
_commandMessages = new WeakMap();
_ProxyDriver_static = new WeakSet();
getMockedCommand_fn = function(commandName, scope) {
const isDebugCommand = commandName === "debug";
return async (...args) => {
if (!import.meta.hot) {
throw new Error("Could not connect to testrunner");
}
id++;
console.log(...isDebugCommand ? ["[WDIO] %cDebug Mode Enabled", "background: #ea5906; color: #fff; padding: 3px; border-radius: 5px;"] : ["[WDIO] ".concat((/* @__PURE__ */ new Date()).toISOString(), " - id: ").concat(id, " - COMMAND: ").concat(commandName, "(").concat(args.join(", "), ")")]);
if (HIDE_REPORTER_FOR_COMMANDS.includes(commandName) && mochaFramework) {
mochaFramework.setAttribute("style", "display: none");
}
const cid = getCID();
import.meta.hot.send(WDIO_EVENT_NAME, __privateMethod(this, _ProxyDriver_static, commandRequest_fn).call(this, {
commandName,
cid,
id,
args,
scope
}));
return new Promise((resolve, reject) => {
let commandTimeout;
if (!isDebugCommand) {
commandTimeout = setTimeout(
() => reject(new Error('Command "'.concat(commandName, '" timed out'))),
COMMAND_TIMEOUT
);
}
__privateGet(this, _commandMessages).set(id, { resolve, reject, commandTimeout, commandName });
});
};
};
handleServerMessage_fn = function(payload) {
if (payload.type === MESSAGE_TYPES.commandResponseMessage) {
return __privateMethod(this, _ProxyDriver_static, handleCommandResponse_fn).call(this, payload.value);
}
if (payload.type === MESSAGE_TYPES.initiateBrowserStateResponse) {
return __privateMethod(this, _ProxyDriver_static, handleBrowserInitiation_fn).call(this, payload.value);
}
};
handleCommandResponse_fn = function(value) {
if (!value.id) {
return console.error("Message without id: ".concat(JSON.stringify(value)));
}
const commandMessage = __privateGet(this, _commandMessages).get(value.id);
if (!commandMessage) {
return console.error('Unknown command id "'.concat(value.id, '"'));
}
if (HIDE_REPORTER_FOR_COMMANDS.includes(commandMessage.commandName) && mochaFramework) {
mochaFramework.removeAttribute("style");
}
if (value.error) {
console.log("[WDIO] ".concat((/* @__PURE__ */ new Date()).toISOString(), " - id: ").concat(value.id, " - ERROR: ").concat(JSON.stringify(value.error.message)));
value.error.message = value.error.message || "unknown error";
return commandMessage.reject(value.error);
}
if (commandMessage.commandTimeout) {
clearTimeout(commandMessage.commandTimeout);
}
console.log("[WDIO] ".concat((/* @__PURE__ */ new Date()).toISOString(), " - id: ").concat(value.id, " - RESULT: ").concat(JSON.stringify(value.result)));
commandMessage.resolve(value.result);
__privateGet(this, _commandMessages).delete(value.id);
};
handleBrowserInitiation_fn = function(value) {
const cid = getCID();
if (!cid) {
return;
}
if (!browser) {
throw new Error("Could not connect to browser");
}
for (const commandName of value.customCommands) {
browser.addCommand(commandName, __privateMethod(this, _ProxyDriver_static, getMockedCommand_fn).call(this, commandName));
}
};
wrapConsolePrototype_fn = function(cid) {
for (const method of CONSOLE_METHODS) {
const origCommand = console[method].bind(console);
console[method] = (...args) => {
var _a;
(_a = import.meta.hot) == null ? void 0 : _a.send(WDIO_EVENT_NAME, __privateMethod(this, _ProxyDriver_static, consoleMessage_fn).call(this, {
name: "consoleEvent",
type: method,
args: JSON.parse(safeStringify(sanitizeConsoleArgs(args))),
cid
}));
origCommand(...args);
};
}
};
commandRequest_fn = function(value) {
return {
type: MESSAGE_TYPES.commandRequestMessage,
value
};
};
consoleMessage_fn = function(value) {
return {
type: MESSAGE_TYPES.consoleMessage,
value
};
};
__privateAdd(ProxyDriver, _ProxyDriver_static);
__privateAdd(ProxyDriver, _commandMessages, /* @__PURE__ */ new Map());
export {
ProxyDriver as default
};