Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Protractor with a single Gulp task

Is there a way to run e2e tests using Protractor and Gulp in a single task?

Right now, in order to run e2e tests, I have to open 3 separate shells and run the following:

webdriver-manager update

webdriver-manager start

npm start (which runs the app server)

protractor protractor.conf.js (in a separate window)

There must be a simpler way of running these tests. Any thoughts?

like image 320
cusejuice Avatar asked Aug 02 '15 19:08

cusejuice


2 Answers

Here is a solution that can work

var protractor = require("gulp-protractor").protractor;
var spawn = require('child_process').spawn; 

//run webdriver method
function runWebdriver(callback) {
    spawn('webdriver-manager', ['start'], {
        stdio: 'inherit'
    }).once('close', callback);
}

//run protractor.config method
function runProtractorConfig() {
    gulp.src('./**/*-page.spec.js')
        .pipe(protractor({
            configFile: 'protractor.config.js'
        }))
        .on('error', function (e) {
            throw e;
        });
}
//execute protractor.config after webdriver is executed
function runWebdriverProtractor(){
    runWebdriver(runProtractorConfig);
}
//put them into gulp task
gulp.task('run-protractor', runWebdriverProtractor);
like image 51
Jorawar Singh Avatar answered Sep 23 '22 13:09

Jorawar Singh


Yes it's possible.

  1. First way: (only for running it from console with simple command " protractor [config file] "

Just run once command with NPM:

npm install [email protected]
npm install [email protected]

This will install required packages.

Next modify the protractor.conf.js with selenium standalone instead of selenium address:

//seleniumAddress: 'http://localhost:4444/wd/hub',
seleniumServerJar: './node_modules/selenium-standalone-jar/bin/selenium-server-standalone-2.45.0.jar',

Now when you run protractor [ config file] like: protractor protractor.conf.js he will start selenium for himself and make tests.

  1. Second way:

You can implement that into build process, during build with GULP he will fire for you protractor and protractor will fire up selenium-driver himself.

I see that its really tiring to you to run those command, why would you do that even, just use Gulp to make it for you.

I will explain you this on working example.

All you need is:

Add to your (NPM) package.json file: (pick the version that suits you, this works for me.

"gulp-protractor": "^2.1.0",
"protractor": "2.5.1",
"selenium-standalone-jar": "2.45.0",

Then you need the proper config in protractor.conf.js: You need to replace the "seleniumAddress" with direct JAR file so the protractor will fire it for you and attach to it automaticly! piece of file is:

    framework: 'jasmine',
    //seleniumAddress: 'http://localhost:4444/wd/hub',
    seleniumServerJar: './node_modules/selenium-standalone-jar/bin/selenium-server-standalone-2.45.0.jar',

The seleniumServerJar is a reference path to the file which will be installed with "selenium-standalone-jar": "2.45.0", from your package.json file.

And last step is to properly make a gulp task, here's mine:

var angularProtractor = require('gulp-angular-protractor');
    gulp.task('protractor-test', function(callback) {
        gulp
            .src(['./**/*.js'])  -< 
            .pipe(angularProtractor({
                'configFile': 'protractor.conf.js',
                'debug': true,
                'autoStartStopServer': true
            }))
            .on('error', function(e) {
                console.log(e);
            })
            .on('end', callback);

});

Enjoy!

like image 23
Fransis Q Avatar answered Sep 21 '22 13:09

Fransis Q