Home:ALL Converter>Code coverage for Protractor tests in AngularJS

Code coverage for Protractor tests in AngularJS

Ask Time:2014-03-12T19:52:47         Author:user1206050

Json Formatter

I am running some e2e tests in my angularJS app with protractor (as recommended in the angularJS documentation). I've googled around and cannot find any information on how to measure coverage for my protractor tests.

I think I'm missing something here... is there any way to get a code coverage report for protractor e2e tests? Or is it simply a feature for unit tests?

Author:user1206050,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/22350680/code-coverage-for-protractor-tests-in-angularjs
Dag Høidahl :

This is achievable using Istanbul. Here is the process, with some example configurations that I've extracted from our project (not tested):\n\n\nInstrument your code using the command istanbul instrument. Make sure that istanbul's coverage variable is __coverage__.\n\n// gulpfile.js\n\ngulp.task('concat', function () {\n gulp.src(PATH.src)\n // Instrument for protractor-istanbul-plugin:\n .pipe(istanbul({coverageVariable: '__coverage__'}))\n .pipe(concat('scripts.js'))\n .pipe(gulp.dest(PATH.dest))\n});\n\nConfigure Protractor with the plugin protractor-istanbul-plugin.\n\n// spec-e2e.conf.js\nvar istanbulPlugin = require('protractor-istanbul-plugin');\n\nexports.config = {\n // [...]\n plugins: [{ inline: istanbulPlugin }]\n};\n\nRun your tests.\nExtract the reports using istanbul report.\n\n\nThis approach has worked for me and is easy to combine with coverage reports from unit tests as well. To automate, I've put step 1 into my gulpfile.js and step 3 and 4 in the test and posttest scripts in package.json, more or less like this:\n\n// In package.json:\n\"scripts\": {\n \"test\": \"gulp concat && protractor tests/spec-e2e.conf.js\",\n \"posttest\": \"istanbul report --include coverage/**/.json --dir reports/coverage cobertura\"\n},\n",
2015-10-07T21:37:29
Faisal Feroz :

if you are using grunt - you can use grunt-protractor-coverage plugin, it will do the job for you. You will have to instrument the code first and then use the mentioned plugin to create coverage reports for you.",
2014-07-22T17:07:19
SavoryBytes :

To add to ryanb's answer, I haven't tried this but you should be able to use something like gulp-istanbul to instrument the code and override the default coverage variable, then define an onComplete function on the jasmineNodeOpts object in your Protractor config file. It gets called once right before everything is closed down.\n\nexports.config = {\n\n // ...\n\n jasmineNodeOpts: {\n onComplete: function(){\n browser.driver.executeScript(\"return __coverage__;\").then(function(val) {\n fs.writeFileSync(\"/path/to/coverage.json\", JSON.stringify(val));\n });\n }\n }\n};\n",
2014-06-14T22:52:42
jbarrus :

I initially tried the onComplete method suggested by daniellmb, but getting the coverage results only at the end will not include all the results if there were multiple page loads during the tests. Here's a gist that sums up how I got things working, but basically I had to create a reporter that added coverage results to the instanbul collector every time a spec finished, and then wrote the reports in the onComplete method. I also had to use a \"waitPlugin\" as suggested by sjelin to prevent protractor from exiting before the results were written.\n\nhttps://gist.github.com/jbarrus/286cee4294a6537e8217",
2015-07-24T03:58:36
yy