chore: Tune handling of default capabilities
MODIFY
lib/utils.js
lib/utils.js
@@ -68,25 +68,29 @@ function parseCapsForInnerDriver (jsonwpCapabilities, w3cCapabilities, constrain
if (!_.isEmpty(defaultCapabilities)) {
if (hasW3CCaps) {
- const {firstMatch = [], alwaysMatch = {}} = w3cCapabilities;
for (const [defaultCapKey, defaultCapValue] of _.toPairs(defaultCapabilities)) {
let isCapAlreadySet = false;
- for (const firstMatchEntry of firstMatch) {
- if (_.has(removeW3CPrefixes(firstMatchEntry), removeW3CPrefix(defaultCapKey))) {
+ // Check if the key is already present in firstMatch entries
+ for (const firstMatchEntry of (w3cCapabilities.firstMatch || [])) {
+ if (_.isPlainObject(firstMatchEntry)
+ && _.has(removeW3CPrefixes(firstMatchEntry), removeW3CPrefix(defaultCapKey))) {
isCapAlreadySet = true;
break;
}
}
- isCapAlreadySet = isCapAlreadySet || _.has(removeW3CPrefixes(alwaysMatch), removeW3CPrefix(defaultCapKey));
+ // Check if the key is already present in alwaysMatch entries
+ isCapAlreadySet = isCapAlreadySet || (_.isPlainObject(w3cCapabilities.alwaysMatch)
+ && _.has(removeW3CPrefixes(w3cCapabilities.alwaysMatch), removeW3CPrefix(defaultCapKey)));
if (isCapAlreadySet) {
+ // Skip if the key is already present in the provided caps
continue;
}
// Only add the default capability if it is not overridden
- if (_.isEmpty(firstMatch)) {
+ if (_.isEmpty(w3cCapabilities.firstMatch)) {
w3cCapabilities.firstMatch = [{[defaultCapKey]: defaultCapValue}];
} else {
- firstMatch[0][defaultCapKey] = defaultCapValue;
+ w3cCapabilities.firstMatch[0][defaultCapKey] = defaultCapValue;
}
}
}

MODIFY
test/utils-specs.js
test/utils-specs.js
@@ -33,10 +33,47 @@ describe('utils', function () {
protocol.should.equal('W3C');
});
it('should include default capabilities in results', function () {
- let {desiredCaps, processedJsonwpCapabilities, processedW3CCapabilities} = parseCapsForInnerDriver(BASE_CAPS, W3C_CAPS, {}, {foo: 'bar'});
- desiredCaps.should.deep.equal({foo: 'bar', ...BASE_CAPS});
- processedJsonwpCapabilities.should.deep.equal({foo: 'bar', ...BASE_CAPS});
- processedW3CCapabilities.alwaysMatch.should.deep.equal({'appium:foo': 'bar', ...insertAppiumPrefixes(BASE_CAPS)});
+ const defaultCaps = {
+ foo: 'bar',
+ baz: 'bla',
+ };
+ const {
+ desiredCaps,
+ processedJsonwpCapabilities,
+ processedW3CCapabilities
+ } = parseCapsForInnerDriver(BASE_CAPS, W3C_CAPS, {}, defaultCaps);
+ desiredCaps.should.deep.equal({
+ ...defaultCaps,
+ ...BASE_CAPS,
+ });
+ processedJsonwpCapabilities.should.deep.equal({
+ ...defaultCaps,
+ ...BASE_CAPS
+ });
+ processedW3CCapabilities.alwaysMatch.should.deep.equal({
+ ...insertAppiumPrefixes(defaultCaps),
+ ...insertAppiumPrefixes(BASE_CAPS)
+ });
+ });
+ it('should include default capabilities into incomplete W3C caps', function () {
+ const defaultCaps = {
+ foo: 'bar',
+ baz: 'bla',
+ };
+ const {
+ desiredCaps,
+ processedJsonwpCapabilities,
+ processedW3CCapabilities
+ } = parseCapsForInnerDriver({}, {
+ alwaysMatch: {},
+ }, {}, defaultCaps);
+ desiredCaps.should.deep.equal({
+ ...defaultCaps,
+ });
+ processedJsonwpCapabilities.should.deep.equal(defaultCaps);
+ processedW3CCapabilities.alwaysMatch.should.deep.equal(
+ insertAppiumPrefixes(defaultCaps)
+ );
});
it('should rewrite default capabilities in results', function () {
const baseCapsWithDefault = Object.assign({}, BASE_CAPS, {

chore: Tune handling of default capabilities (#13887)