UNPKG

webdriverio-automation

Version:

WebdriverIO-Automation android ios project

134 lines (94 loc) 5.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "SevereServiceError", { enumerable: true, get: function () { return _SevereServiceError.default; } }); exports.multiremote = exports.attach = exports.remote = void 0; var _path = _interopRequireDefault(require("path")); var _webdriver = _interopRequireDefault(require("webdriver")); var _logger = _interopRequireDefault(require("@wdio/logger")); var _config = require("@wdio/config"); var _utils = require("@wdio/utils"); var _multiremote = _interopRequireDefault(require("./multiremote")); var _constants = require("./constants"); var _utils2 = require("./utils"); var _SevereServiceError = _interopRequireDefault(require("./utils/SevereServiceError")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } const log = (0, _logger.default)('webdriverio'); const remote = async function (params = {}, remoteModifier) { _logger.default.setLogLevelsConfig(params.logLevels, params.logLevel); const config = (0, _config.validateConfig)(_constants.WDIO_DEFAULTS, params, Object.keys(_webdriver.default.DEFAULTS)); const automationProtocol = await (0, _utils2.getAutomationProtocol)(config); const modifier = (client, options) => { Object.assign(options, Object.entries(config).reduce((a, [k, v]) => v == null ? a : _objectSpread(_objectSpread({}, a), {}, { [k]: v }), {})); if (typeof remoteModifier === 'function') { client = remoteModifier(client, options); } options.automationProtocol = automationProtocol; return client; }; if (params.user && params.key) { params = Object.assign({}, (0, _config.detectBackend)(params), params); } if (params.outputDir) { process.env.WDIO_LOG_PATH = _path.default.join(params.outputDir, 'wdio.log'); } const prototype = (0, _utils2.getPrototype)('browser'); log.info(`Initiate new session using the ${automationProtocol} protocol`); const ProtocolDriver = require(automationProtocol).default; await (0, _utils2.updateCapabilities)(params, automationProtocol); const instance = await ProtocolDriver.newSession(params, modifier, prototype, _utils.wrapCommand); if (params.runner && !(0, _utils2.isStub)(automationProtocol)) { const origAddCommand = instance.addCommand.bind(instance); instance.addCommand = (name, fn, attachToElement) => origAddCommand(name, (0, _utils.runFnInFiberContext)(fn), attachToElement); const origOverwriteCommand = instance.overwriteCommand.bind(instance); instance.overwriteCommand = (name, fn, attachToElement) => origOverwriteCommand(name, (0, _utils.runFnInFiberContext)(fn), attachToElement); } instance.addLocatorStrategy = (0, _utils2.addLocatorStrategyHandler)(instance); return instance; }; exports.remote = remote; const attach = function (params) { const prototype = (0, _utils2.getPrototype)('browser'); return _webdriver.default.attachToSession(params, null, prototype, _utils.wrapCommand); }; exports.attach = attach; const multiremote = async function (params = {}, config = {}) { const multibrowser = new _multiremote.default(); const browserNames = Object.keys(params); await Promise.all(browserNames.map(async browserName => { const instance = await remote(params[browserName]); return multibrowser.addInstance(browserName, instance); })); const prototype = (0, _utils2.getPrototype)('browser'); const sessionParams = (0, _utils2.isStub)(config.automationProtocol) ? undefined : { sessionId: '', isW3C: multibrowser.instances[browserNames[0]].isW3C, logLevel: multibrowser.instances[browserNames[0]].options.logLevel }; const ProtocolDriver = (0, _utils2.isStub)(config.automationProtocol) ? require(config.automationProtocol).default : _webdriver.default; const driver = ProtocolDriver.attachToSession(sessionParams, multibrowser.modifier.bind(multibrowser), prototype, _utils.wrapCommand); if (!(0, _utils2.isStub)(config.automationProtocol)) { const origAddCommand = driver.addCommand.bind(driver); driver.addCommand = (name, fn, attachToElement) => { origAddCommand(name, (0, _utils.runFnInFiberContext)(fn), attachToElement, Object.getPrototypeOf(multibrowser.baseInstance), multibrowser.instances); }; const origOverwriteCommand = driver.overwriteCommand.bind(driver); driver.overwriteCommand = (name, fn, attachToElement) => { origOverwriteCommand(name, (0, _utils.runFnInFiberContext)(fn), attachToElement, Object.getPrototypeOf(multibrowser.baseInstance), multibrowser.instances); }; } driver.addLocatorStrategy = (0, _utils2.addLocatorStrategyHandler)(driver); return driver; }; exports.multiremote = multiremote;