UNPKG

@appium/base-driver

Version:

Base driver class for Appium drivers

73 lines 3.12 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isW3cCaps = isW3cCaps; exports.fixCaps = fixCaps; const lodash_1 = __importDefault(require("lodash")); /** * Determine whether the given argument is valid * W3C capabilities instance. */ function isW3cCaps(caps) { if (!lodash_1.default.isPlainObject(caps)) { return false; } const c = caps; const isFirstMatchValid = () => lodash_1.default.isArray(c.firstMatch) && !lodash_1.default.isEmpty(c.firstMatch) && lodash_1.default.every(c.firstMatch, lodash_1.default.isPlainObject); const isAlwaysMatchValid = () => lodash_1.default.isPlainObject(c.alwaysMatch); if (lodash_1.default.has(c, 'firstMatch') && lodash_1.default.has(c, 'alwaysMatch')) { return isFirstMatchValid() && isAlwaysMatchValid(); } if (lodash_1.default.has(c, 'firstMatch')) { return isFirstMatchValid(); } if (lodash_1.default.has(c, 'alwaysMatch')) { return isAlwaysMatchValid(); } return false; } /** * Normalize capability values according to constraints (e.g. string 'true' → boolean). */ function fixCaps(oldCaps, desiredCapConstraints, log) { const caps = lodash_1.default.clone(oldCaps); const logCastWarning = (prefix) => log.warn(`${prefix}. This may cause unexpected behavior`); // boolean capabilities can be passed in as strings 'false' and 'true' // which we want to translate into boolean values const booleanCaps = lodash_1.default.keys(lodash_1.default.pickBy(desiredCapConstraints, (k) => k.isBoolean === true)); for (const cap of booleanCaps) { const value = oldCaps[cap]; if (!lodash_1.default.isString(value)) { continue; } if (!['true', 'false'].includes(value.toLowerCase())) { logCastWarning(`String capability '${cap}' ('${value}') cannot be converted to a boolean`); continue; } logCastWarning(`Capability '${cap}' changed from string '${value}' to boolean`); caps[cap] = value.toLowerCase() === 'true'; } // int capabilities are often sent in as strings by frameworks const intCaps = lodash_1.default.keys(lodash_1.default.pickBy(desiredCapConstraints, (k) => k.isNumber === true)); for (const cap of intCaps) { const value = oldCaps[cap]; if (!lodash_1.default.isString(value)) { continue; } const intValue = parseInt(value, 10); const floatValue = parseFloat(value); const newValue = floatValue !== intValue ? floatValue : intValue; if (Number.isNaN(newValue)) { logCastWarning(`String capability '${cap}' ('${value}') cannot be converted to a number`); continue; } logCastWarning(`Capability '${cap}' changed from string '${value}' to number ${newValue}`); caps[cap] = newValue; } return caps; } //# sourceMappingURL=capabilities.js.map