Search code examples
jasmineprotractor

How to get spec name to be used as output file name in protractor-html-reporter-2?


I'm using protractor html reporter 2 for my reporting. I need to get spec name as html report output file name.

i'm using the below code in my config file.

var today = new Date(),
timeStamp = today.getMonth() + 1 + '-' + today.getDate() + '-' + today.getFullYear();
onPrepare:
 function () 
{
    browser.driver.manage().window().maximize();
    var fs = require('fs-extra');        

    scriptName=function(){
        return browser.getProcessedConfig().then(function(config){
        return config.specs;
        });
    };

    fs.emptyDir('./target/htmlReports/'+scriptName+'-'+timeStamp+'/screenShots/', function (err) {
        console.log(err);
    });
    jasmine.getEnv().addReporter({
        specDone: function(result) {
            //if (result.status == 'failed' || result.status == 'passed') {
            if (1==1) {
                browser.getCapabilities().then(function (caps) {
                    var browserName = userData.testUser.browser.toUpperCase();

                    browser.takeScreenshot().then(function (png) {
                        var stream = fs.createWriteStream('./target/htmlReports/'+scriptName+'-'+timeStamp+'/screenShots/'+ result.fullName+'.png');
                        stream.write(new Buffer(png, 'base64'));
                        stream.end();
                    });
                });
            }
        }
    });



    jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
        consolidateAll: true,
        savePath: 'target/XMLReports',
        filePrefix: 'xmlresults'
    }));

},

onComplete: function() {
    var browserName, browserVersion;
    var capsPromise = browser.getCapabilities();

    capsPromise.then(function (caps) {
        browserName = userData.testUser.browser.toUpperCase();
        platform=caps.get('platform');
        browserVersion = caps.get('version');


        testConfig = {
            reportTitle: 'Test Execution Report',
            outputPath: './target/htmlReports/'+scriptName+'-'+timeStamp,
            screenshotPath: './target/htmlReports/'+scriptName+'-'+timeStamp+'/screenShots',
            testBrowser: browserName,
            browserVersion: browserVersion,
            outputFilename:'ProtractorTestReport',
            testPlatform: platform,
            //browserVersion: browserVersion,
            modifiedSuiteName: true,
            screenshotsOnlyOnFailure: false
        };
        new HTMLReport().from('./target/XMLReports/xmlresults.xml', testConfig);
    });
},

plugins: [{
    package: 'jasmine2-protractor-utils',
    disableHTMLReport: true,
    disableScreenshot: false,
    screenshotPath:'./target/htmlReports/'+scriptName+'-'+timeStamp+'/screenShots',
    screenshotOnExpectFailure:true,
    screenshotOnSpecFailure:true,
    clearFoldersBeforeTest: true,
    htmlReportDir: './target/htmlReports'

}],

i tried with browser.getProcessedConfig().then(function(config){ console.log(config.specs); }); ,it returns

[ 'D:\projects\HeartlandSSP\Automation\TenantManagement\Ssp.TenantManagement.Protractor_Test\specs\createTenantSpec.js', 'C:\Users\renusri.rajalingam\AppData\Roaming\npm\node_modules\protractor\built\frameworks\__protractor_internal_afterEach_setup_spec.js' ]

but the actual spec name createTenantSpec.js is not returning. I need only the filename of the spec and not the name of the describe or it functions. Since I have 5 specs, i need to generate separate report with its spec name. Can anyone please help me on this?


Solution

  • The value of config.specs is an array and according to output we have the file's absolute path is at index 0. So the file name can be extracted as follows.

    browser.getProcessedConfig().then(function (config) {
      var fullName = config.specs[0];
      var fileName = fullName.substring(fullName.lastIndexOf('/')+1);
      console.log('fileName:', fileName);
    });
    
    // output: 
    // fileName: createTenantSpec.js
    

    Or if you would like to have all file names in this array you could use this:

    browser.getProcessedConfig().then(function (config) {
      var fileNames = config.specs.map(function(path) { 
        return path.substring(path.lastIndexOf('/')+1); 
      });
      fileNames.forEach(function(fileName) {
        console.log('fileName:', fileName);
      });
    });
    
    // output:
    // fileName: createTenantSpec.js
    // __protractor_internal_afterEach_setup_spec.js
    

    References:

    array.prototype.map() => click me

    array.prototype.forEach() => click me