如何在使用Protractor Parallel Tests时避免覆盖生成的XML报告

时间:2017-11-07 17:27:11

标签: selenium junit jasmine protractor jasmine-reporters

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

这是我的onPrepare()函数,我使用的是jasmine-reporter。一些人可以帮助我做出必须做出的改变。这样我就可以获得所有运行的spec文件的综合报告

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

另外FYI我在config.js文件中使用shardTestFiles:true和maxInstance:4进行并行测试。 Protractor@5.2.0 jasmine-reporters@2.2.1

1 个答案:

答案 0 :(得分: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.