├── .npmignore ├── .travis.yml ├── examples ├── tests │ ├── github.js │ └── npm.js ├── nightwatch.json └── gulpfile.js ├── .gitignore ├── lib ├── background.js └── helper.js ├── package.json ├── test └── test.js ├── LICENSE ├── README.md └── index.js /.npmignore: -------------------------------------------------------------------------------- 1 | examples 2 | test 3 | .* 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "6" 5 | - "8" 6 | -------------------------------------------------------------------------------- /examples/tests/github.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'Demo: GitHub URL Test' : function (browser) { 3 | browser 4 | .url('https://github.com') 5 | .assert.urlEquals('https://github.com/') 6 | .end(); 7 | }, 8 | 'Demo: GitHub Title Test' : function (browser) { 9 | browser 10 | .url('https://github.com') 11 | .assert.title('How people build software · GitHub') 12 | .end(); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | -------------------------------------------------------------------------------- /examples/tests/npm.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'Demo test Npmjs' : function (browser) { 3 | browser 4 | .url('https://www.npmjs.com') 5 | .waitForElementVisible('body', 1000) 6 | .setValue('input[type=search]', 'nightwatch') 7 | .waitForElementVisible('input[type=submit]', 1000) 8 | .click('input[type=submit]') 9 | .pause(1000) 10 | .assert.elementPresent('a[href="/packages/nightwatch"]', 'The Night Watch') 11 | .end(); 12 | } 13 | }; 14 | 15 | module.exports = { 16 | tags: [ 'sandbox' ], 17 | 'Demo test Npmjs' : function (browser) { 18 | browser 19 | .url('https://www.npmjs.com') 20 | .assert.title('npm') 21 | .end(); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /lib/background.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | var nightwatch = require('nightwatch'); 3 | var originalArgv = JSON.parse(process.argv[2]); 4 | var log = require('fancy-log'); 5 | 6 | nightwatch.cli(function(argv) { 7 | for (var key in originalArgv) { 8 | if (key === 'env' && originalArgv[key].indexOf(',') > -1 && argv['parallel-mode'] === true) { 9 | continue; 10 | } 11 | argv[key] = originalArgv[key]; 12 | } 13 | 14 | if (argv.test) { 15 | argv.test = path.resolve(argv.test); 16 | } 17 | 18 | const runner = nightwatch.CliRunner(argv); 19 | runner.setup() 20 | .startWebDriver() 21 | .then(() => { 22 | return runner.runTests(); 23 | }) 24 | .then(() => { 25 | return runner.stopWebDriver(); 26 | }).catch(err => { 27 | log.error(err); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-nightwatch", 3 | "version": "1.2.0", 4 | "description": "gulp plugin for Nightwatch.js", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/tatsuyafw/gulp-nightwatch.git" 12 | }, 13 | "keywords": [ 14 | "gulpplugin", 15 | "nightwatch" 16 | ], 17 | "author": "Tatsuya Hoshino", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/tatsuyafw/gulp-nightwatch/issues" 21 | }, 22 | "homepage": "https://github.com/tatsuyafw/gulp-nightwatch", 23 | "dependencies": { 24 | "fancy-log": "^1.3.2", 25 | "minimist": "^1.1.1", 26 | "nightwatch": "^1.3.1", 27 | "plugin-error": "^1.0.1", 28 | "shell-quote": "^1.4.3", 29 | "through": "^2.3.8" 30 | }, 31 | "devDependencies": { 32 | "chai": "^2.1.2", 33 | "mocha": "^5.0.1" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var nightwatchPlugin = require('../'); 2 | var expect = require('chai').expect; 3 | var PluginError = require('plugin-error'); 4 | require('mocha'); 5 | 6 | describe('gulp-nightwatch', function() { 7 | describe('nightwatchPlugin', function() { 8 | it('should exist', function() { 9 | expect(nightwatchPlugin).to.exist; 10 | }); 11 | 12 | describe('options', function() { 13 | describe('with configFile option', function() { 14 | it ('should throw if configFile is not string', function() { 15 | expect(function() { 16 | nightwatchPlugin({ configFile: {} }); 17 | }).to.throw('configFile option must be string'); 18 | }); 19 | }); 20 | 21 | describe('with cliArgs option', function() { 22 | it ('should throw if cliArgs is not array or object', function() { 23 | expect(function() { 24 | nightwatchPlugin({ cliArgs: 'args' }); 25 | }).to.throw('cliArgs option must be array or object'); 26 | }); 27 | }); 28 | }); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /lib/helper.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var parse = require('shell-quote').parse; 4 | var parseArgs = require('minimist'); 5 | var PluginError = require('plugin-error'); 6 | var util = require('util'); 7 | 8 | var PLUGIN_NAME = 'gulp-nightwatch'; 9 | 10 | exports.merge = function(dest, src) { 11 | var prop; 12 | for (prop in src) { 13 | dest[prop] = src[prop]; 14 | } 15 | }; 16 | 17 | exports.parseCliArgs = function(cliArgs) { 18 | if (util.isArray(cliArgs)) { 19 | cliArgs = cliArgs.map(function(arg) { 20 | return parse(arg); 21 | }); 22 | cliArgs = exports.flattenArrays(cliArgs); 23 | cliArgs = parseArgs(cliArgs); 24 | 25 | // Delete unnecessary '_' property. 26 | // See: https://github.com/substack/minimist#var-argv--parseargsargs-opts 27 | delete cliArgs['_']; 28 | } else if (typeof cliArgs === 'object') { 29 | // Nothing to do. 30 | } else { 31 | throw new PluginError(PLUGIN_NAME, 'cliArgs option must be array or object'); 32 | } 33 | 34 | return cliArgs; 35 | }; 36 | 37 | exports.flattenArrays = function(arrays) { 38 | return [].concat.apply([], arrays); 39 | }; 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-present, Tatsuya Hoshino 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /examples/nightwatch.json: -------------------------------------------------------------------------------- 1 | { 2 | "src_folders" : [ "tests" ], 3 | "output_folder" : "reports", 4 | "custom_commands_path" : "", 5 | "custom_assertions_path" : "", 6 | "globals_path" : "", 7 | 8 | "selenium" : { 9 | "start_process" : true, 10 | "server_path" : "./bin/selenium-server-standalone-3.13.0.jar", 11 | "log_path" : "", 12 | "host" : "127.0.0.1", 13 | "port" : 4444, 14 | "cli_args" : { 15 | "webdriver.chrome.driver" : "./bin/chromedriver" 16 | } 17 | }, 18 | 19 | "test_settings" : { 20 | "default" : { 21 | "launch_url" : "http://localhost", 22 | "selenium_port" : 4444, 23 | "selenium_host" : "localhost", 24 | "silent": true, 25 | "screenshots" : { 26 | "enabled" : false, 27 | "path" : "" 28 | }, 29 | "desiredCapabilities": { 30 | "browserName": "firefox", 31 | "javascriptEnabled": true, 32 | "acceptSslCerts": true 33 | } 34 | }, 35 | "chrome" : { 36 | "desiredCapabilities": { 37 | "browserName": "chrome", 38 | "javascriptEnabled": true, 39 | "acceptSslCerts": true 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /examples/gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'); 2 | var nightwatch = require('../'); 3 | 4 | gulp.task('default', function() { 5 | gulp.src('') 6 | .pipe(nightwatch({ 7 | configFile: 'nightwatch.json' 8 | })); 9 | }); 10 | 11 | gulp.task('withNoOptions', function() { 12 | gulp.src('') 13 | .pipe(nightwatch()); 14 | }); 15 | 16 | gulp.task('withCliArgs:array', function() { 17 | gulp.src('') 18 | .pipe(nightwatch({ 19 | configFile: 'nightwatch.json', 20 | cliArgs: [ '--env chrome', '--tag sandbox' ] 21 | })); 22 | }); 23 | 24 | gulp.task('withCliArgs:object', function() { 25 | gulp.src('') 26 | .pipe(nightwatch({ 27 | configFile: 'nightwatch.json', 28 | cliArgs: { 29 | env: 'chrome', 30 | tag: 'sandbox' 31 | } 32 | })); 33 | }); 34 | 35 | gulp.task('withCliArgs:testcase', function() { 36 | gulp.src('') 37 | .pipe(nightwatch({ 38 | configFile: 'nightwatch.json', 39 | cliArgs: { 40 | test: 'tests/github', 41 | testcase: "Demo: GitHub Title Test" 42 | } 43 | // or, cliArgs: [ '--test tests/github', '--testcase="Demo: GitHub Title Test"' ] 44 | })); 45 | }); 46 | 47 | gulp.task('withCliArgs:multi-env', function() { 48 | gulp.src('') 49 | .pipe(nightwatch({ 50 | configFile: 'nightwatch.json', 51 | cliArgs: [ '--env chrome,default' ] 52 | })); 53 | }); 54 | 55 | gulp.task('all', ['default', 'withNoOptions', 'withCliArgs:array', 'withCliArgs:object', 56 | 'withCliArgs:testcase', 'withCliArgs:multi-env']); 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-nightwatch [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] 2 | 3 | ## Usage 4 | 5 | First, install `gulp-nightwatch` as a development dependency: 6 | 7 | ```sh 8 | npm install --save-dev gulp-nightwatch 9 | ``` 10 | 11 | Then, write your gulpfile.js as below. 12 | 13 | ```javascript 14 | var gulp = require('gulp'), 15 | nightwatch = require('gulp-nightwatch'); 16 | 17 | gulp.task('default', function() { 18 | return gulp.src('gulpfile.js') 19 | .pipe(nightwatch({ 20 | configFile: 'test/nightwatch.json' 21 | })); 22 | }); 23 | ``` 24 | 25 | You can pass command line options to Nightwatch as an array by using the option `cliArgs`. 26 | 27 | ```javascript 28 | gulp.task('nightwatch:chrome', function(){ 29 | return gulp.src('gulpfile.js') 30 | .pipe(nightwatch({ 31 | configFile: 'test/nightwatch.json', 32 | cliArgs: [ '--env chrome', '--tag sandbox' ] 33 | })); 34 | }); 35 | ``` 36 | 37 | You may use an object instead, if you prefer. 38 | 39 | ```javascript 40 | gulp.task('nightwatch:chrome', function(){ 41 | return gulp.src('gulpfile.js') 42 | .pipe(nightwatch({ 43 | configFile: 'test/nightwatch.json', 44 | cliArgs: { 45 | env: 'chrome', 46 | tag: 'sandbox' 47 | } 48 | })); 49 | }); 50 | ``` 51 | 52 | ## API 53 | 54 | ### nightwatch(options) 55 | 56 | #### options 57 | 58 | ##### configFile 59 | 60 | Type: `String` 61 | Default: `nightwatch.json` 62 | 63 | The path to your Nightwatch config 64 | 65 | ##### cliArgs 66 | 67 | Type: `Array` or `Object` 68 | Default: null 69 | 70 | Command line options for Nightwatch 71 | 72 | 73 | [npm-image]: https://img.shields.io/npm/v/gulp-nightwatch.svg 74 | [npm-url]: https://www.npmjs.com/package/gulp-nightwatch 75 | [travis-image]: https://img.shields.io/travis/tatsuyafw/gulp-nightwatch.svg 76 | [travis-url]: https://travis-ci.org/tatsuyafw/gulp-nightwatch 77 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var through = require('through'); 4 | var log = require('fancy-log'); 5 | var helper = require('./lib/helper'); 6 | var path = require('path'); 7 | var spawn = require('child_process').spawn; 8 | var PluginError = require('plugin-error'); 9 | 10 | var PLUGIN_NAME = 'gulp-nightwatch'; 11 | 12 | var nightwatchPlugin = function(options) { 13 | var child, 14 | stream, 15 | files = [], 16 | nightwatchOptions = { config: 'nightwatch.json', env: 'default' }; 17 | 18 | options = options || {}; 19 | 20 | if (options.configFile) { 21 | if (typeof options.configFile === 'string') { 22 | nightwatchOptions.config = path.resolve(options.configFile); 23 | } else { 24 | throw new PluginError(PLUGIN_NAME, 'configFile option must be string'); 25 | } 26 | } 27 | 28 | if (options.cliArgs) { 29 | helper.merge(nightwatchOptions, helper.parseCliArgs(options.cliArgs)); 30 | } 31 | 32 | function done(code) { 33 | if (child) { 34 | child.kill(); 35 | } 36 | 37 | if (stream) { 38 | if (code) { 39 | stream.emit('error', new PluginError(PLUGIN_NAME, 'nightwatch exited with code ' + code)); 40 | } else { 41 | stream.emit('end'); 42 | } 43 | } 44 | } 45 | 46 | function startNightwatch() { 47 | log('Starting nightwatch...'); 48 | 49 | child = spawn( 50 | 'node', 51 | [ 52 | path.join(__dirname, 'lib', 'background.js'), 53 | JSON.stringify(nightwatchOptions) // Nightwatch args 54 | ], 55 | { 56 | stdio: 'inherit' 57 | } 58 | ); 59 | 60 | child.on('exit', function(code) { 61 | done(code); 62 | }); 63 | 64 | } 65 | 66 | function queueFile(file) { 67 | if (file) { 68 | files.push(file.path); 69 | } 70 | } 71 | 72 | function endStream() { 73 | if (files.length) { 74 | options.files = files; 75 | } 76 | 77 | startNightwatch(); 78 | } 79 | 80 | stream = through(queueFile, endStream); 81 | return stream; 82 | 83 | }; 84 | 85 | module.exports = nightwatchPlugin; 86 | --------------------------------------------------------------------------------