feat: Add parseCapsArray to base driver
MODIFY
lib/basedriver/helpers.js
lib/basedriver/helpers.js
@@ -357,6 +357,28 @@ function duplicateKeys (input, firstKey, secondKey) {
return input;
}
+/**
+ * Takes a desired capability and tries to JSON.parse it as an array,
+ * and either returns the parsed array or a singleton array.
+ *
+ * @param {string|Array<String>} cap A desired capability
+ */
+function parseCapsArray (cap) {
+ let parsedCaps;
+ try {
+ parsedCaps = JSON.parse(cap);
+ if (_.isArray(parsedCaps)) {
+ return parsedCaps;
+ }
+ } catch (ign) {
+ logger.warn(`Failed to parse capability as JSON array`);
+ }
+ if (_.isString(cap)) {
+ return [cap];
+ }
+ throw new Error(`must provide a string or JSON Array; received ${cap}`);
+}
+
export {
- configureApp, isPackageOrBundle, getCoordDefault, getSwipeTouchDuration, duplicateKeys,
+ configureApp, isPackageOrBundle, getCoordDefault, getSwipeTouchDuration, duplicateKeys, parseCapsArray
};

MODIFY
test/basedriver/helpers-specs.js
test/basedriver/helpers-specs.js
-import { isPackageOrBundle, duplicateKeys } from '../../lib/basedriver/helpers';
+import { isPackageOrBundle, duplicateKeys, parseCapsArray } from '../../lib/basedriver/helpers';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
@@ -93,3 +93,16 @@ describe('helpers', function () {
});
});
});
+
+describe('parseCapsArray', function () {
+ it('should parse string into array', function () {
+ parseCapsArray('/tmp/my/app.zip').should.eql(['/tmp/my/app.zip']);
+ });
+ it('should parse array into array', function () {
+ parseCapsArray('["/tmp/my/app.zip"]').should.eql(['/tmp/my/app.zip']);
+ parseCapsArray('["/tmp/my/app.zip","/tmp/my/app2.zip"]').should.eql([
+ '/tmp/my/app.zip',
+ '/tmp/my/app2.zip'
+ ]);
+ });
+});

feat: Add parseCapsArray to base driver (#333)