Cover image
anonymoussc
Mar 8, 2016 • 1 min read

Automating test runners using Gulp

Example how to set up and configure Gulp to run the following tasks - HTTP web server, Karma test runner, WebDriver, Protractor.

Install Gulp and Protractor globally

npm install -g gulp protractor

Create a package.json

npm init

Install dependencies

npm install connect gulp-load-plugins gulp-protractor karma karma-chrome-launcher karma-jasmine -—save-dev

Create Karma configuration file named karma.conf.js

module.exports = function () {
    
    return {
        frameworks: ['jasmine'],
        files: [
            'lib/angular/angular.js',
            'lib/angular/angular-mocks.js',
            'src/app.js',
            'test/unit/appSpec.js'
        ],
        autoWatch: true,
        browsers: ['Chrome']
    };

};

Create a Protractor configuration file named protractor.conf.js

exports.config = {
    seleniumServerJar: './node_modules/protractor/selenium/selenium-server-standalone-2.41.0.jar',
    specs            : ['test/e2e/appSpec.js'],
    jasmineNodeOpts  : {
        showColors            : true,
        defaultTimeoutInterval: 30000
    }
};

Create a file named gulpfile.js

var gulp = require('gulp');
var $    = require('gulp-load-plugins')();

gulp.task('webdriver_update', $.protractor.webdriver_update);

gulp.task('connect', function() {

    var connect = require('connect');
    var app     = connect().use(connect.static('src'));

    $.server    = require('http').createServer(app).listen(8000);

});

gulp.task('test', function(done) {

    var karma     = require('karma').server;
    var karmaConf = require('./karma.conf.js')();

    karma.start(karmaConf, done);

});

gulp.task('e2e', ['connect', 'webdriver_update'], function(done) {

    gulp.src(['test/e2e/appSpec.js'])
        .pipe($.protractor.protractor({
            configFile: './protractor.conf.js',
        }))
        .on('end', function() {

            $.server.close();
            done();

        });

});

Run unit tests

gulp test

Run the end-to-end tests

gulp e2e

Television is simply automated daydreaming. - Lee Loevinger

Post by: Anonymoussc (@anonymoussc)