量角器测试成功运行,但Protractor-coverage报告完全为空

时间:2014-11-28 11:46:31

标签: angularjs gruntjs code-coverage protractor angularjs-e2e

我试图将我当前的AngularJS项目与Protractor Coverage集成。请在下面找到package.json和我的protractor-config。

的package.json

"devDependencies": {
    "chromedriver": "~2.8.1",
    "grunt": "~0.4.0",
    "grunt-contrib-clean": "~0.4.0",
    "grunt-contrib-copy": "~0.5.0",
    "grunt-contrib-jshint": "~0.2.0",
    "grunt-contrib-concat": "~0.1.3",
    "grunt-contrib-uglify": "~0.1.1",
    "grunt-contrib-watch": "~0.3.1",
    "grunt-html2js": "~0.1.0",
    "grunt-karma": "~0.4.4",
    "grunt-protractor-runner": "~1.1.4",
    "grunt-contrib-less": "~0.11.3",
    "grunt-shell": "~0.6.0",
    "selenium": "~2.20.0",
    "grunt-protractor-coverage": "^0.2.1",
    "grunt-istanbul": "^0.2.5",
    "grunt-express-server": "~0.4.5",
    "protractor": "~1.4.0",
    "grunt-contrib-connect": "~0.7.1"
  }

量角器配置

exports.config = {

    seleniumServerJar: '../../node_modules/selenium/lib/runner/selenium-server-standalone-2.20.0.jar',
    chromeDriver: '../../node_modules/chromedriver/lib/chromedriver/chromedriver',
    baseUrl: 'http://localhost:3000/',

    capabilities: {
        'browserName': 'chrome',
        'chromeOptions': {
            'args': ['incognito', 'disable-extensions', 'start-maximized']
        }
    },

    onPrepare: function() {
        exports.server = require('../../server.js');
    },

    specs: ['../e2e/**/*.spec.js'],  

    jasmineNodeOpts: {
        onComplete: function () {},
        isVerbose: true,
        showColors: true,
        includeStackTrace: true,
        defaultTimeoutInterval: 90000
    }
};

我的咕噜声任务

grunt.registerTask('test', ['clean:coverage', 'copy:instrument', 'instrument',  'protractor_coverage:chrome', 'makeReport']);

    copy: {
        instrument: {
            files: [{
                src: ['src/app/**/*', '!src/app/**/*.js'],
                dest: 'coverage/e2e/instrumented/'
            }]
        },
    },

    clean: {
        coverage: ['coverage', 'instrumented', 'reports']
    }


    instrument: {
        files: 'src/app/**/*.js',
        options: {
            lazy: true,
            basePath: 'coverage/e2e/instrumented'
        }
    },

    makeReport: {
        src: 'coverage/e2e/instrumented/*.json',
        options: {
            type: 'lcov',
            dir: 'coverage/e2e/reports',
            print: 'detail'             
        }
    },


    protractor_coverage: {
        options: {
            configFile: 'test/config/protractor-config.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: 'coverage/e2e/instrumented',
            args: {}
        },
        chrome: {
            options: {
                args: {
                    baseUrl: 'http://localhost:3000/',
                    // Arguments passed to the command
                    'browser': 'chrome'
                }
            }
        },
    },

当我尝试运行grunt任务时,所有测试都成功运行,但量角器覆盖率报告完全为空。我尝试了几种选择,但我无法让报告显示出来。

enter image description here

问题:我在这里做错了吗?如何使量角器覆盖加载我的src js文件?

3 个答案:

答案 0 :(得分:4)

这里有一个很棒的人有一个适合我的黑客(http://javahow.net/questions/22350680/code-coverage-for-protractor-tests-in-angularjs

基本上,您添加了一个e2e测试,用于从浏览器中写出收集的覆盖范围,如下所示:

'use strict';

var fs = require('fs');

describe('Output the code coverage objects', function() {
  it('should output the coverage object.', function() {
    browser.driver.executeScript("return __coverage__;").then(function(val) {
      fs.writeFileSync("coverage/e2e/coverageE2E.json", JSON.stringify(val));
    });
  });
});

我已经针对这个问题发布了这个解决方案,以防它们也帮助他们:Protractor coverage not generating report

答案 1 :(得分:1)

您可以尝试添加运行{},如下所示:

protractor_coverage: {
    options: {
        ......
    },
    chrome: {
       ......
    },
    run: {}
},

答案 2 :(得分:1)

只想添加到simonvandyk的答案。 coverage变量不是 coverage 。相反,它似乎默认附加日期/时间。你要做的是设置覆盖变量 在你的gruntfile中,在instrument任务下,添加coverageVariable,如下所示。



instrument: {
        files: ['js/*.js'],
        options: {
            lazy: true,
            basePath: "./instrumented",
            coverageVariable: '__coverage__' //<<--- sets it to specific value
        }
    },
&#13;
&#13;
&#13;