cli.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * Module dependencies
  3. */
  4. var opt = require('optimist');
  5. module.exports = new (function() {
  6. var _DEFAULTS_ = {};
  7. var _COMMANDS_ = {};
  8. function Command(name) {
  9. this.name = name;
  10. _DEFAULTS_[this.name] = {};
  11. }
  12. Command.prototype.demand = function(value) {
  13. if (!value && _DEFAULTS_[this.name].demand) {
  14. return _DEFAULTS_[this.name].demand;
  15. }
  16. _DEFAULTS_[this.name].demand = value;
  17. return this;
  18. };
  19. Command.prototype.description = function(value) {
  20. if (!value && _DEFAULTS_[this.name].description) {
  21. return _DEFAULTS_[this.name].alias;
  22. }
  23. _DEFAULTS_[this.name].description = value;
  24. return this;
  25. };
  26. Command.prototype.alias = function(value) {
  27. if (!value && _DEFAULTS_[this.name].alias) {
  28. return _DEFAULTS_[this.name].alias;
  29. }
  30. _DEFAULTS_[this.name].alias = value;
  31. return this;
  32. };
  33. Command.prototype.defaults = function(value) {
  34. if (!value && _DEFAULTS_[this.name]['default']) {
  35. return _DEFAULTS_[this.name]['default'];
  36. }
  37. _DEFAULTS_[this.name]['default'] = value;
  38. return this;
  39. };
  40. Command.prototype.isDefault = function(value) {
  41. return _DEFAULTS_[this.name]['default'] === value;
  42. };
  43. this.showHelp = function() {
  44. return opt.showHelp();
  45. };
  46. this.command = function(name) {
  47. if (_COMMANDS_[name]) {
  48. return _COMMANDS_[name];
  49. }
  50. _COMMANDS_[name] = new Command(name);
  51. return _COMMANDS_[name];
  52. };
  53. this.init = function() {
  54. var argv = opt.usage('Usage: $0 [source] [options]')
  55. .option('source', {
  56. string : true
  57. })
  58. .options(_DEFAULTS_).argv;
  59. return argv;
  60. };
  61. this.setup = function() {
  62. // CLI definitions
  63. // $ nightwatch -c
  64. // $ nightwatch --config
  65. this.command('config')
  66. .demand(true)
  67. .description('Path to configuration file')
  68. .alias('c')
  69. .defaults('./nightwatch.json');
  70. // $ nightwatch -o
  71. // $ nightwatch --output
  72. this.command('output')
  73. .description('Where to save the (JUnit XML) test reports.')
  74. .alias('o')
  75. .defaults('tests_output');
  76. // $ nightwatch -r
  77. // $ nightwatch --reporter
  78. this.command('reporter')
  79. .description('Name of a predefined reporter (e.g. junit) or path to a custom reporter file to use.')
  80. .alias('r')
  81. .defaults('junit');
  82. // $ nightwatch -e
  83. // $ nightwatch --env saucelabs
  84. this.command('env')
  85. .description('Testing environment to use.')
  86. .alias('e')
  87. .defaults('default');
  88. // $ nightwatch --verbose
  89. this.command('verbose')
  90. .description('Turns on selenium command logging during the session.');
  91. // $ nightwatch -t
  92. // $ nightwatch --test
  93. this.command('test')
  94. .description('Runs a single test.')
  95. .alias('t');
  96. // $ nightwatch --testcase
  97. this.command('testcase')
  98. .description('Used only together with --test. Runs the specified testcase from the current suite/module.');
  99. // $ nightwatch -g
  100. // $ nightwatch --group
  101. this.command('group')
  102. .description('Runs a group of tests (i.e. a folder)')
  103. .alias('g');
  104. // $ nightwatch -s
  105. // $ nightwatch --skipgroup
  106. this.command('skipgroup')
  107. .description('Skips one or several (comma separated) group of tests.')
  108. .alias('s');
  109. // $ nightwatch -f
  110. // $ nightwatch --filter
  111. this.command('filter')
  112. .description('Specify a filter (glob expression) as the file name format to use when loading the files.')
  113. .defaults('')
  114. .alias('f');
  115. // $ nightwatch -a
  116. // $ nightwatch --tag
  117. this.command('tag')
  118. .description('Only run tests with the given tag.')
  119. .defaults('')
  120. .alias('a');
  121. // $ nightwatch --skiptags
  122. this.command('skiptags')
  123. .description('Skips tests that have the specified tag or tags (comma separated).');
  124. // $ nightwatch --retries
  125. this.command('retries')
  126. .description('Retries failed or errored testcases up <n> times.');
  127. // $ nightwatch --suiteRetries
  128. this.command('suiteRetries')
  129. .description('Retries failed or errored testsuites up <n> times.');
  130. // $ nightwatch -h
  131. // $ nightwatch --help
  132. this.command('help')
  133. .description('Shows this help.')
  134. .alias('h');
  135. // $ nightwatch -v
  136. // $ nightwatch --version
  137. this.command('version')
  138. .alias('v')
  139. .description('Shows version information.');
  140. return this;
  141. };
  142. })();