Skip to content

Commit 734a96b

Browse files
committed
Allows --desired-caps to be passed a filename
The --desired-caps flag can now be passed the name of a JSON file as an argument. This also adds an alias for the flag, `-dc`, and fixes tests.
1 parent 00041de commit 734a96b

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

docs/en/writing-running-appium/default-capabilities-arg.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
Appium 1.5 does away with most CLI flags; the remainder can be converted into JSON and made part of the `--default-capabilities` flag. For example:
44

55
```
6+
# raw JSON as an argument
67
--default-capabilities '{"app": "myapp.app", "deviceName": "iPhone Simulator"}'
8+
# or the name of a JSON file
9+
--default-capabilities /path/to/file.json
710
```
811

9-
**Windows users** will need to escape the quotes: `--default-capabilities "{\"app\": \"myapp.app\"}"`
12+
**Windows users** will need to escape the quotes in JSON passed on the command line: `--default-capabilities "{\"app\": \"myapp.app\"}"`
1013

1114

1215

docs/en/writing-running-appium/server-args.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
## Appium server arguments
1+
# Appium server arguments
22

33
Many Appium 1.5 server arguments have been deprecated in favor of the [--default-capabilities flag](/docs/en/writing-running-appium/default-capabilities-arg.md).
44

55
Usage: `node . [flags]`
66

7-
### Server flags
7+
## Server flags
88
All flags are optional, but some are required in conjunction with certain others.
99

1010

@@ -43,7 +43,6 @@ All flags are optional, but some are required in conjunction with certain others
4343
|`--chromedriver-executable`|null|ChromeDriver executable full path||
4444
|`--show-config`|false|Show info about the appium server configuration and exit||
4545
|`--no-perms-check`|false|Bypass Appium's checks to ensure we can read/write necessary files||
46-
|`--command-timeout`|60|The default command timeout for the server to use for all sessions (in seconds and should be less than 2147483). Will still be overridden by newCommandTimeout cap||
4746
|`--strict-caps`|false|Cause sessions to fail if desired caps are sent in that Appium does not recognize as valid for the selected device||
4847
|`--isolate-sim-device`|false|Xcode 6 has a bug on some platforms where a certain simulator can only be launched without error if all other simulator devices are first deleted. This option causes Appium to delete all devices other than the one being used by Appium. Note that this is a permanent deletion, and you are responsible for using simctl or xcode to manage the categories of devices used with Appium.||
4948
|`--tmp`|null|Absolute path to directory Appium can use to manage temporary files, like built-in iOS apps it needs to move around. On *nix/Mac defaults to /tmp, on Windows defaults to C:\Windows\Temp||
@@ -52,7 +51,8 @@ All flags are optional, but some are required in conjunction with certain others
5251
|`--suppress-adb-kill-server`|false|(Android-only) If set, prevents Appium from killing the adb server instance||
5352
|`--async-trace`|false|Add long stack traces to log entries. Recommended for debugging only.||
5453
|`--webkit-debug-proxy-port`|27753|(IOS-only) Local port used for communication with ios-webkit-debug-proxy|`--webkit-debug-proxy-port 27753`|
55-
|`--default-capabilities`|{}|Set the default desired capabilities, which will be set on each session unless overridden by received capabilities. [More infomation](/docs/en/writing-running-appium/default-capabilities-arg.md). |`--default-capabilities '{"app": "myapp.app", "deviceName": "iPhone Simulator"}'`|
54+
|`-dc`, `--default-capabilities`|{}|Set the default desired capabilities, which will be set on each session unless overridden by received capabilities.|`--default-capabilities [ '{"app": "myapp.app", "deviceName": "iPhone Simulator"}' | /path/to/caps.json ]`|
55+
|`--command-timeout`|60|[DEPRECATED] No effect. This used to be the default command timeout for the server to use for all sessions (in seconds and should be less than 2147483). Use newCommandTimeout cap instead||
5656
|`-k`, `--keep-artifacts`|false|[DEPRECATED] - no effect, trace is now in tmp dir by default and is cleared before each run. Please also refer to the --trace-dir flag.||
5757
|`--platform-name`|null|[DEPRECATED] - Name of the mobile platform: iOS, Android, or FirefoxOS|`--platform-name iOS`|
5858
|`--platform-version`|null|[DEPRECATED] - Version of the mobile platform|`--platform-version 7.1`|

lib/parser.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'fs';
12
import path from 'path';
23
import _ from 'lodash';
34
import { ArgumentParser } from 'argparse';
@@ -334,12 +335,13 @@ const args = [
334335
help: '(IOS-only) Local port used for communication with ios-webkit-debug-proxy'
335336
}],
336337

337-
[['--default-capabilities'], {
338+
[['-dc', '--default-capabilities'], {
338339
dest: 'defaultCapabilities',
339340
defaultValue: {},
340-
type: JSON.parse,
341+
type: parseDefaultCaps,
341342
required: false,
342-
example: '{"app": "myapp.app", "deviceName": "iPhone Simulator"}',
343+
example: '[ \'{"app": "myapp.app", "deviceName": "iPhone Simulator"}\' ' +
344+
'| /path/to/caps.json ]',
343345
help: 'Set the default desired capabilities, which will be set on each ' +
344346
'session unless overridden by received capabilities.'
345347
}],
@@ -731,6 +733,21 @@ function updateParseArgsForDefaultCapabilities (parser) {
731733
};
732734
}
733735

736+
function parseDefaultCaps (caps) {
737+
try {
738+
if (fs.statSync(caps).isFile()) {
739+
caps = fs.readFileSync(caps, 'utf8');
740+
}
741+
} catch (err) {
742+
// not a file, or not readable
743+
}
744+
caps = JSON.parse(caps);
745+
if (!_.isPlainObject(caps)) {
746+
throw 'Invalid format for default capabilities';
747+
}
748+
return caps;
749+
}
750+
734751
function getParser () {
735752
let parser = new ArgumentParser({
736753
version: pkgObj.version,

test/fixtures/caps.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"a": "b"
3+
}

test/parser-specs.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const should = chai.should();
77

88
describe('Parser', () => {
99
let p = getParser();
10+
p.debug = true; // throw instead of exit on error; pass as option instead?
1011
it('should return an arg parser', () => {
1112
should.exist(p.parseArgs);
1213
p.parseArgs([]).should.have.property('port');
@@ -20,14 +21,26 @@ describe('Parser', () => {
2021
}
2122
});
2223
it('should throw an error with unknown argument', () => {
23-
(() => {p.parseArgs(['--apple']);}).should.throw;
24+
(() => {p.parseArgs(['--apple']);}).should.throw();
2425
});
25-
it('should parse default capabilities correctly', () => {
26+
it('should parse default capabilities correctly from a string', () => {
2627
let defaultCapabilities = {a: 'b'};
2728
let args = p.parseArgs(['--default-capabilities',
2829
JSON.stringify(defaultCapabilities)]);
2930
args.defaultCapabilities.should.eql(defaultCapabilities);
3031
});
32+
it('should parse default capabilities correctly from a file', () => {
33+
let defaultCapabilities = {a: 'b'};
34+
let args = p.parseArgs(['--default-capabilities',
35+
'test/fixtures/caps.json']);
36+
args.defaultCapabilities.should.eql(defaultCapabilities);
37+
});
38+
it('should throw an error with invalid arg to default capabilities', () => {
39+
(() => {p.parseArgs(['-dc', '42']);}).should.throw();
40+
(() => {p.parseArgs(['-dc', 'false']);}).should.throw();
41+
(() => {p.parseArgs(['-dc', 'null']);}).should.throw();
42+
(() => {p.parseArgs(['-dc', 'does/not/exist.json']);}).should.throw();
43+
});
3144
it('should parse args that are caps into default capabilities', () => {
3245
let defaultCapabilities = {localizableStringsDir: '/my/dir'};
3346
let args = p.parseArgs(['--localizable-strings-dir', '/my/dir']);

0 commit comments

Comments
 (0)