Skip to content

Commit

Permalink
fix: Only strip known prefixes from capability names (appium#14492)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Jun 30, 2020
1 parent 19067c5 commit 93004b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
22 changes: 11 additions & 11 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ function parseCapsForInnerDriver (jsonwpCapabilities, w3cCapabilities, constrain
// Check if the key is already present in firstMatch entries
for (const firstMatchEntry of (w3cCapabilities.firstMatch || [])) {
if (_.isPlainObject(firstMatchEntry)
&& _.has(removeW3CPrefixes(firstMatchEntry), removeW3CPrefix(defaultCapKey))) {
&& _.has(removeAppiumPrefixes(firstMatchEntry), removeAppiumPrefix(defaultCapKey))) {
isCapAlreadySet = true;
break;
}
}
// Check if the key is already present in alwaysMatch entries
isCapAlreadySet = isCapAlreadySet || (_.isPlainObject(w3cCapabilities.alwaysMatch)
&& _.has(removeW3CPrefixes(w3cCapabilities.alwaysMatch), removeW3CPrefix(defaultCapKey)));
&& _.has(removeAppiumPrefixes(w3cCapabilities.alwaysMatch), removeAppiumPrefix(defaultCapKey)));
if (isCapAlreadySet) {
// Skip if the key is already present in the provided caps
continue;
Expand All @@ -95,15 +95,15 @@ function parseCapsForInnerDriver (jsonwpCapabilities, w3cCapabilities, constrain
}
}
if (hasJSONWPCaps) {
jsonwpCapabilities = Object.assign({}, removeW3CPrefixes(defaultCapabilities), jsonwpCapabilities);
jsonwpCapabilities = Object.assign({}, removeAppiumPrefixes(defaultCapabilities), jsonwpCapabilities);
}
}

// Get MJSONWP caps
if (hasJSONWPCaps) {
protocol = MJSONWP;
desiredCaps = jsonwpCapabilities;
processedJsonwpCapabilities = removeW3CPrefixes({...desiredCaps});
processedJsonwpCapabilities = {...jsonwpCapabilities};
}

// Get W3C caps
Expand All @@ -129,7 +129,7 @@ function parseCapsForInnerDriver (jsonwpCapabilities, w3cCapabilities, constrain
}

if (hasJSONWPCaps && !isFixingNeededForW3cCaps) {
const differingKeys = _.difference(_.keys(processedJsonwpCapabilities), _.keys(removeW3CPrefixes(desiredCaps)));
const differingKeys = _.difference(_.keys(removeAppiumPrefixes(processedJsonwpCapabilities)), _.keys(removeAppiumPrefixes(desiredCaps)));
if (!_.isEmpty(differingKeys)) {
logger.info(`The following capabilities were provided in the JSONWP desired capabilities that are missing ` +
`in W3C capabilities: ${JSON.stringify(differingKeys)}`);
Expand Down Expand Up @@ -233,21 +233,21 @@ function insertAppiumPrefixes (caps) {
return prefixedCaps;
}

function removeW3CPrefixes (caps) {
function removeAppiumPrefixes (caps) {
if (!_.isPlainObject(caps)) {
return caps;
}

const fixedCaps = {};
for (let [name, value] of _.toPairs(caps)) {
fixedCaps[removeW3CPrefix(name)] = value;
fixedCaps[removeAppiumPrefix(name)] = value;
}
return fixedCaps;
}

function removeW3CPrefix (key) {
const colonPos = key.indexOf(':');
return colonPos > 0 && key.length > colonPos ? key.substring(colonPos + 1) : key;
function removeAppiumPrefix (key) {
const prefix = `${W3C_APPIUM_PREFIX}:`;
return _.startsWith(key, prefix) ? key.substring(prefix.length) : key;
}

function getPackageVersion (pkgName) {
Expand Down Expand Up @@ -293,5 +293,5 @@ const rootDir = findRoot(__dirname);

export {
inspectObject, parseCapsForInnerDriver, insertAppiumPrefixes, rootDir,
getPackageVersion, pullSettings,
getPackageVersion, pullSettings, removeAppiumPrefixes,
};
20 changes: 18 additions & 2 deletions test/utils-specs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import {
parseCapsForInnerDriver, insertAppiumPrefixes, pullSettings } from '../lib/utils';
parseCapsForInnerDriver, insertAppiumPrefixes, pullSettings,
removeAppiumPrefixes,
} from '../lib/utils';
import { BASE_CAPS, W3C_CAPS } from './helpers';
import _ from 'lodash';

Expand Down Expand Up @@ -88,7 +90,7 @@ describe('utils', function () {
'appium:foo2': 'bar2',
});
desiredCaps.should.deep.equal({foo: 'baz', foo2: 'baz2', ...BASE_CAPS});
processedJsonwpCapabilities.should.deep.equal({foo: 'baz', foo2: 'baz2', ...BASE_CAPS});
processedJsonwpCapabilities.should.deep.equal({foo: 'baz', foo2: 'bar2', 'appium:foo2': 'baz2', ...BASE_CAPS});
processedW3CCapabilities.alwaysMatch.should.deep.equal({'appium:foo': 'baz', 'appium:foo2': 'baz2', ...insertAppiumPrefixes(BASE_CAPS)});
});
it('should reject if W3C caps are not passing constraints', function () {
Expand Down Expand Up @@ -172,6 +174,20 @@ describe('utils', function () {
});
});

describe('removeAppiumPrefixes()', function () {
it('should remove appium prefixes from cap names', function () {
removeAppiumPrefixes({
'appium:cap1': 'value1',
'ms:cap2': 'value2',
someCap: 'someCap',
}).should.eql({
'cap1': 'value1',
'ms:cap2': 'value2',
someCap: 'someCap',
});
});
});

describe('insertAppiumPrefixes()', function () {
it('should apply prefixes to non-standard capabilities', function () {
insertAppiumPrefixes({
Expand Down

0 comments on commit 93004b7

Please sign in to comment.