|
| 1 | +import * as path from 'path'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as optimist from 'optimist'; |
| 4 | + |
| 5 | +/** |
| 6 | + * The command line interface for interacting with the Protractor runner. |
| 7 | + * It takes care of parsing command line options. |
| 8 | + * |
| 9 | + * Values from command line options override values from the config. |
| 10 | + */ |
| 11 | + |
| 12 | +var args: Array<string> = []; |
| 13 | + |
| 14 | +process.argv.slice(2).forEach(function(arg: string) { |
| 15 | + var flag: string = arg.split('=')[0]; |
| 16 | + |
| 17 | + switch (flag) { |
| 18 | + case 'debug': |
| 19 | + args.push('--nodeDebug'); |
| 20 | + args.push('true'); |
| 21 | + break; |
| 22 | + case '-d': |
| 23 | + case '--debug': |
| 24 | + case '--debug-brk': |
| 25 | + args.push('--v8Debug'); |
| 26 | + args.push('true'); |
| 27 | + break; |
| 28 | + default: |
| 29 | + args.push(arg); |
| 30 | + break; |
| 31 | + } |
| 32 | +}); |
| 33 | + |
| 34 | +export interface ProtractorArgs { |
| 35 | + _: Array<string>; |
| 36 | + help?: boolean; |
| 37 | + version?: boolean; |
| 38 | + capabilities?: Object; |
| 39 | + specs?: string|Array<string>; |
| 40 | + exclude?: string|Array<string>; |
| 41 | + elementExplorer?: boolean; |
| 42 | +} |
| 43 | +; |
| 44 | + |
| 45 | +optimist |
| 46 | + .usage( |
| 47 | + 'Usage: protractor [configFile] [options]\n' + |
| 48 | + 'configFile defaults to protractor.conf.js\n' + |
| 49 | + 'The [options] object will override values from the config file.\n' + |
| 50 | + 'See the reference config for a full list of options.') |
| 51 | + .describe('help', 'Print Protractor help menu') |
| 52 | + .describe('version', 'Print Protractor version') |
| 53 | + .describe('browser', 'Browsername, e.g. chrome or firefox') |
| 54 | + .describe('seleniumAddress', 'A running selenium address to use') |
| 55 | + .describe('seleniumSessionId', 'Attaching an existing session id') |
| 56 | + .describe( |
| 57 | + 'seleniumServerJar', 'Location of the standalone selenium jar file') |
| 58 | + .describe( |
| 59 | + 'seleniumPort', 'Optional port for the selenium standalone server') |
| 60 | + .describe('baseUrl', 'URL to prepend to all relative paths') |
| 61 | + .describe('rootElement', 'Element housing ng-app, if not html or body') |
| 62 | + .describe('specs', 'Comma-separated list of files to test') |
| 63 | + .describe('exclude', 'Comma-separated list of files to exclude') |
| 64 | + .describe('verbose', 'Print full spec names') |
| 65 | + .describe('stackTrace', 'Print stack trace on error') |
| 66 | + .describe('params', 'Param object to be passed to the tests') |
| 67 | + .describe('framework', 'Test framework to use: jasmine, mocha, or custom') |
| 68 | + .describe('resultJsonOutputFile', 'Path to save JSON test result') |
| 69 | + .describe('troubleshoot', 'Turn on troubleshooting output') |
| 70 | + .describe('elementExplorer', 'Interactively test Protractor commands') |
| 71 | + .describe( |
| 72 | + 'debuggerServerPort', |
| 73 | + 'Start a debugger server at specified port instead of repl') |
| 74 | + .alias('browser', 'capabilities.browserName') |
| 75 | + .alias('name', 'capabilities.name') |
| 76 | + .alias('platform', 'capabilities.platform') |
| 77 | + .alias('platform-version', 'capabilities.version') |
| 78 | + .alias('tags', 'capabilities.tags') |
| 79 | + .alias('build', 'capabilities.build') |
| 80 | + .alias('grep', 'jasmineNodeOpts.grep') |
| 81 | + .alias('invert-grep', 'jasmineNodeOpts.invertGrep') |
| 82 | + .alias('explorer', 'elementExplorer') |
| 83 | + .string('capabilities.tunnel-identifier') |
| 84 | + .check(function(arg: ProtractorArgs) { |
| 85 | + if (arg._.length > 1) { |
| 86 | + throw 'Error: more than one config file specified'; |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | +var argv: ProtractorArgs = optimist.parse(args); |
| 91 | + |
| 92 | +if (argv.help) { |
| 93 | + optimist.showHelp(); |
| 94 | + process.exit(0); |
| 95 | +} |
| 96 | + |
| 97 | +if (argv.version) { |
| 98 | + console.log( |
| 99 | + 'Version ' + require(path.join(__dirname, '../package.json')).version); |
| 100 | + process.exit(0); |
| 101 | +} |
| 102 | + |
| 103 | +// WebDriver capabilities properties require dot notation, but optimist parses |
| 104 | +// that into an object. Re-flatten it. |
| 105 | +function flattenObject(obj: any, prefix?: string, out?: any): any { |
| 106 | + prefix = prefix || ''; |
| 107 | + out = out || {}; |
| 108 | + for (var prop in obj) { |
| 109 | + if (obj.hasOwnProperty(prop)) { |
| 110 | + typeof obj[prop] === 'object' ? |
| 111 | + flattenObject(obj[prop], prefix + prop + '.', out) : |
| 112 | + out[prefix + prop] = obj[prop]; |
| 113 | + } |
| 114 | + } |
| 115 | + return out; |
| 116 | +}; |
| 117 | + |
| 118 | +if (argv.capabilities) { |
| 119 | + argv.capabilities = flattenObject(argv.capabilities); |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Helper to resolve comma separated lists of file pattern strings relative to |
| 124 | + * the cwd. |
| 125 | + * |
| 126 | + * @private |
| 127 | + * @param {Array} list |
| 128 | + */ |
| 129 | +function processFilePatterns_(list: string): Array<string> { |
| 130 | + return list.split(',').map(function(spec) { |
| 131 | + return path.resolve(process.cwd(), spec); |
| 132 | + }); |
| 133 | +}; |
| 134 | + |
| 135 | +if (argv.specs) { |
| 136 | + argv.specs = processFilePatterns_(<string>argv.specs); |
| 137 | +} |
| 138 | +if (argv.exclude) { |
| 139 | + argv.exclude = processFilePatterns_(<string>argv.exclude); |
| 140 | +} |
| 141 | + |
| 142 | +// Use default configuration, if it exists. |
| 143 | +var configFile: string = argv._[0]; |
| 144 | +if (!configFile) { |
| 145 | + if (fs.existsSync('./protractor.conf.js')) { |
| 146 | + configFile = './protractor.conf.js'; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +if (!configFile && !argv.elementExplorer && args.length < 3) { |
| 151 | + console.log( |
| 152 | + '**you must either specify a configuration file ' + |
| 153 | + 'or at least 3 options. See below for the options:\n'); |
| 154 | + optimist.showHelp(); |
| 155 | + process.exit(1); |
| 156 | +} |
| 157 | + |
| 158 | +// Run the launcher |
| 159 | +require('./launcher').init(configFile, argv); |
0 commit comments