2

我正在尝试使用 jasmine-reporters(JunitXmlReporter) 生成 xml 报告,但是当我在 Selenium Grid 上使用 4 个实例运行量角器测试时,并行运行多个规范文件时结果会被覆盖。Xml 报告总是显示最后运行的规范文件结果。

这是我使用 jasmine-reporter 的 onPrepare() 函数。有人可以帮助我进行哪些更改。这样我就可以获得运行的所有规范文件的综合报告

    onPrepare: function() {
 let jasmineReporters = require('jasmine-reporters');
        jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
            consolidateAll: false,
            savePath: 'xmlCloudlets_Reports',
            filePrefix: + '-xmloutput'
        }));
}

另外仅供参考,我在 config.js 文件中使用 shardTestFiles:true 和 maxInstance:4 进行并行测试。量角器@5.2.0 jasmine-reporters@2.2.1

4

1 回答 1

1
        You can use below code while setting up the reporter. This worked for me,

        var DEFAULT_SUITE_DIR = 'target/chrome-reports';
        setupDefaultReporters: function(suiteDirectory) {
                return browser.getSession().then(function(session) {
                    var HtmlScreenshotReporter =  require('protractor-angular-screenshot-reporter');
                    var SpecReporter = require('jasmine-spec-reporter/src/jasmine-spec-reporter.js');
                    var jasmineReporters = require('jasmine-reporters');
                    var suiteDir = suiteDirectory || DEFAULT_SUITE_DIR;

                    if (!junitReporter) {
                        var junitReportFile = 'xml-results-' + session.getId()  + '-' + Date.now() + '-';
                        console.log('JUnit reporter using file: ', junitReportFile);
                        junitReporter = new jasmineReporters.JUnitXmlReporter({
                            savePath: suiteDir,
                            filePrefix: junitReportFile,
                            consolidateAll: false,
                            consolidate: true,
                            // Use space instead of dot to separate suite names
                            useDotNotation: false,
                            // Include a timestamp in suite names to make them unique in case of duplicate names
                            modifySuiteName: function(suiteName, suite) {
                                return suiteName + ' ' + Date.now();
                            }
                        });
                    }

                    if (!screenshotReporter) {
                        screenshotReporter = new HtmlScreenshotReporter({
                            baseDirectory: suiteDir + '/screenshots',
                            docName: 'chrome-summary-results.html',
                            takeScreenShotsOnlyForFailedSpecs: true,
                            docTitle: 'Protractor Tests Report - Chrome',
                            preserveDirectory: false
                        }).getJasmine2Reporter();
                    }

                    if (!specReporter) {
                        specReporter = new SpecReporter({displayStacktrace: 'all'});
                    }

                    jasmine.getEnv().addReporter(junitReporter);
                    jasmine.getEnv().addReporter(screenshotReporter);
                    jasmine.getEnv().addReporter(specReporter);
                });
            },


    After completing execution you need to merge the reports by using below function

    mergeJUnitReports: function(suiteDirectory, exitCode) {
            console.log('Merging JUnit reports...');
            var deferred = Promise.defer();
            var suiteDir = suiteDirectory || DEFAULT_SUITE_DIR;
            var destinationFile = suiteDir + '/xml-results.xml';

            var fs = require('fs');
            var sourceFiles = fs.readdirSync(suiteDir)
                .filter(function(filename) {
                    return filename.match(/^xml-results-.*.xml$/);
                })
                .map(function(filename) {
                    return suiteDir + '/' + filename;
                });

            console.log('Source JUnit report files: ', sourceFiles);
            console.log('Destination JUnit report file: ', destinationFile);

            var fs = require('fs');
            var startTag = '<testsuites>';
            var endTag = '</testsuites>';
            var result = '<?xml version="1.0" encoding="UTF-8" ?>' + startTag;

            sourceFiles.forEach(function(sourcePath) {
                var contents = fs.readFileSync(sourcePath, 'utf8');
                var startIndex = contents.indexOf(startTag) + startTag.length;
                var endIndex = contents.indexOf(endTag);
                var suites = contents.substring(startIndex, endIndex);
                result += suites;
            });

            result += endTag;

            fs.writeFileSync(destinationFile, result, 'utf8');
            console.log('JUnit reports merged into file: ', destinationFile);
            return exitCode;
        },


Your conf will contains the calls for merge report like below
afterLaunch: function(exitCode) {
        return helpers.mergeJUnitReports(suiteDir, exitCode);
    },

and Call for setting reporter will go to conf - onPrepare function.
于 2017-11-08T11:32:44.317 回答