如何在protractor-html-reporter-2中获取规格名称作为输出文件名称?

时间:2018-07-27 06:24:22

标签: jasmine protractor

我正在使用量角器html记者2进行报告。我需要获取规范名称作为html报告输出文件名。

我在配置文件中使用以下代码。

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'

}],

我尝试过 browser.getProcessedConfig().then(function(config){ console.log(config.specs); }); ,它返回

  

['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']

,但未返回实际的规范名称 createTenantSpec.js 。我只需要规范的文件名,而不需要describe或函数的名称。由于我有5个规格,因此我需要使用其规格名称生成单独的报告。有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

fun start() { tick().andThen(tick()) } fun tick(): Completable { return Completable .timer(2, TimeUnit.SECONDS) .subscribe { val time = Instant.ofEpochMilli(System.currentTimeMillis()).atZone( ZoneId.systemDefault()).toLocalTime() println(time) } } 的值是一个数组,根据输出,我们可以确定文件的绝对路径位于索引config.specs处。因此,可以按以下方式提取文件名。

0

或者,如果您希望在此数组中拥有所有文件名,则可以使用以下方法:

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

参考文献:

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 => click me

array.prototype.map() => click me

答案 1 :(得分:0)

在完成时使用它:

browser.getProcessedConfig().then(function (config) 
{
            var fullName = config.specs[0];
            console.log('fullName' ,fullName);
            var start= fullName.lastIndexOf('\\');
            var stop=fullName.length;
            console.log('start:', start);
            console.log('stop:', stop);
            var fileName = fullName.substring(start+1,stop);
            console.log('fileName:', fileName);
           
});