Configure Grunt-protractor-runner to run with a global install of protractor

Purpose

The reason to install protractor globally is that if you use the inline protractor provided with grunt-protractor-runner you have a 10 second penalty of waiting for the server to spawn a java process and then attach itself to it.

With the server already started and waiting for calls it is a lot faster turnaround for your tests to run.

Install protractor

Install protractor globally

If you have installed protractor globally to do your end to end testing via:

You may have to do this as sudo if you

npm -g install protractor

Download the chromedriver by updating webdriver-manager

webdriver-manager update

Changes to your project under test.

Install the grunt-protractor-runner for your grunt project

npm install grunt-protractor-runner --save-dev

Add the protractor block your grunt script.

Add the protractor block under grunt.initConfig({}); and load the npm task outside that block to get protractor npm tasks to be wired in your grunt script.

grunt.initConfig({
//........
  protractor: {
            e2e: {
                options : {
                    configFile: 'test/config/protractor.conf.js',
                    keepAlive: true
                }
            },
            debug: {
                options : {
                    configFile: 'test/config/protractor.conf.js',
                    keepAlive: true,
                    debug: true
                }
            }
        },
});
//add grunt protractor npm tasks into grunt.
grunt.loadNpmTasks('grunt-protractor-runner');

//......

 

Setup your protractor

Create your protractor.conf.js in your test/config/ folder if you are following the example above. In the below example I am using the firefox browser that comes with selenium core and testing with Jasmine. My original grunt script was created by yeoman so the default port is 9000


exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub', //this tells it to use your local selenium server you started with the webdriver-manager start.  You may want to use your ip address
  capabilities: { 'firefox' }, // 'chrome' is also work
  specs: ['test/e2e/protractor/**/*.js'],//where you put your protractor scripts
  rootElement: 'body',
  baseUrl: 'http://localhost:9000',//base url 

  jasmineNodeOpts: {
    // onComplete will be called just before the driver quits.
    onComplete: null,
    // If true, display spec names.
    isVerbose: true,
    // If true, print colors to the terminal.
    showColors: true,
    // If true, include stack traces in failures.
    includeStackTrace: true,
    // Default time to wait in ms before a test fails.
    defaultTimeoutInterval: 30000

  }
}

 

Run the Protractor tests

Startup selenium service locally

In one terminal window run the following:

webdriver-manager start

Start up your application

In a second terminal window run the following:


grunt serve

Run your protractor tests

In a third terminal window run the following:

grunt protractor:e2e

Finished

You will now see the browser startup and run your test and finish.  All output should go to the third terminal console.

 

Note:

on OS X if you use chrome browser it will start up the browser behind your terminal.  If your test is very fast you may not see the browser pop up at all until the test is finished.

Further reading

For more info about protractor and end to end testing 

Protractor project documentation

Advertisements

One thought on “Configure Grunt-protractor-runner to run with a global install of protractor

  1. Pingback: Javascript page object pattern based functional tests via the browser with Protractor and Astrolabe | Thoughts on code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s