lfc1988
lfc1988

Reputation: 67

How to avoid overriding of an generated XML report while using Protractor Parallel Tests

I am trying to generate xml reports using jasmine-reporters(JunitXmlReporter) but the result gets overridden when running multiple spec files parallelly as I am running protractor test with 4 instances on Selenium Grid. The Xml report always shows the last spec files result which ran.

Here is my onPrepare() funtion where I am using jasmine-reporter. Can some-one help me with what changes I have to make. so that I can get a consolidated report of all the spec files ran

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

Also FYI I am using shardTestFiles:true and maxInstance:4 in config.js file for parallel tests. [email protected] [email protected]

Upvotes: 2

Views: 1283

Answers (1)

Yash Jagdale
Yash Jagdale

Reputation: 1508

        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.

Upvotes: 1

Related Questions