webdriverio
Version:
Next-gen browser and mobile automation test framework for Node.js
70 lines (69 loc) • 2.17 kB
JavaScript
// src/protocol-stub.ts
import { capabilitiesEnvironmentDetector, resolveCustomCommandOptions } from "@wdio/utils";
var NOOP = () => {
};
var ProtocolStub = class {
static async newSession(options) {
const capabilities = emulateSessionCapabilities(options.capabilities);
const customCommands = [];
const overwrittenCommands = [];
const browser = {
options,
capabilities,
requestedCapabilities: capabilities,
customCommands,
// internally used to transfer custom commands to the actual protocol instance
overwrittenCommands,
// internally used to transfer overwritten commands to the actual protocol instance
commandList: [],
getWindowHandle: NOOP,
on: NOOP,
off: NOOP,
addCommand(name, fn, commandOptions) {
customCommands.push([
name,
fn,
resolveCustomCommandOptions("addCommand", commandOptions)
]);
},
overwriteCommand(name, fn, commandOptions) {
overwrittenCommands.push([
name,
fn,
resolveCustomCommandOptions("overwriteCommand", commandOptions)
]);
},
...capabilitiesEnvironmentDetector(capabilities)
};
return browser;
}
/**
* added just in case user wants to somehow reload webdriver before it was started.
*/
static reloadSession() {
throw new Error("Protocol Stub: Make sure to start the session before reloading it.");
}
static attachToSession(options, modifier) {
if (options || !modifier) {
throw new Error("You are trying to attach to a protocol stub, this should never occur, please file an issue.");
}
return modifier({
commandList: []
});
}
};
function emulateSessionCapabilities(caps) {
const capabilities = {};
Object.entries(caps).forEach(([key, value]) => {
const newKey = key.replace("appium:", "");
capabilities[newKey] = value;
});
const c = "alwaysMatch" in caps ? caps.alwaysMatch : caps;
if (c.browserName && c.browserName.toLowerCase() === "chrome") {
capabilities["goog:chromeOptions"] = {};
}
return capabilities;
}
export {
ProtocolStub as default
};