Next step was integrating Require.JS and configuring measurement of code coverage on client and server by Instanbul. When was this all done I was wondering if it is possible to measure code coverage by Protractor end-to-end testing.
After quick search I found that Ryan Bridges recently released grunt-protractor-coverage plugin. Interesting coincidence. So I decided to try it and can confirm that it’s working fine with mentioned stack. Configuration was smooth and Ryan fixed small issue very promptly. It’s based on grunt-protractor-runner plugin.
I created separate Grunt configuration file just for this purpose not to mess around with normal build. I had also problems to run ‘makeReport’ task of grunt-istanbul plugin for two different directories (Mocha server side code coverage measurement is using same task).
cwd: '<%= dirs.app %>',
dest: '<%= dirs.instrumentedE2E %>/app',
Next step is instrumentation of the code. It is needed for gathering coverage stats. Each line is decorated by special instructions that helps during measurement. Pay attention to fact that we are instrumenting server and client side code. Instrumented code is placed into target directory represented by placeholder
<%= dirs.instrumentedE2E %>.
files: ['server/**/*.js', 'app/scripts/**/*.js'],
basePath: '<%= dirs.instrumentedE2E %>/'
Next we start Express from target directory.
port: process.env.PORT || 9000
script: '<%= dirs.instrumentedE2E %>/lib/server.js',
And the protractor_coverage task of grunt-protractor-coverage plugin. Configuration should be the same as for grunt-protractor-runner plugin.
configFile: 'test/protractor/protractorConf.js', // Default config file
keepAlive: true, // If false, the grunt process stops when the test fails.
noColor: false, // If true, protractor will not use colors in its output.
coverageDir: '<%= dirs.instrumentedE2E %>',
// Arguments passed to the command
Last step is generation of coverage report.
src: '<%= dirs.instrumentedE2E %>/*.json',
dir: '<%= dirs.coverageE2E %>/reports',
Finally, this is grunt task gathering all steps.
EDIT: Notice that following Github link was changed to branch, because project structure was significantly changed:
Source code for this project can be found on GitHub.
For running end to end Protractor test you have to have webdriver-manager running. See Protractor documentation.
To install and start Selenium Webdriver:
npm install -g protractor
To install project dependencies:
To run this end-to-end testing with coverage measurement (webdriver-manager has to be running):
grunt --gruntfile Gruntfile-e2e.js