2017-10-11 83 views
1

我想創建黃瓜HTML報告,我是新來的節點JS,我試圖尋找它,我用下面的如何創建硒黃瓜HTML報告與節點JS

this.registerHandler('AfterFeatures', function(callback) { 
     try { 
      var options = { 
       theme: "bootstrap", 
       jsonFile: "/report/cucumber.json", 
       output: "/report/cucumber_report.html", 
       reportSuiteAsScenarios: true, 
       launchReport: true, 
       metadata: { 
        "App Version": "0.0.1" 
       } 
      }; 
      reporter.generate(options); 
     } catch (e) { 
      console.log(e); 
     } 
     callback(); 
    }); 

但是,當我運行碼,黃瓜功能的場景會被執行並且它最終給了我一個錯誤,說明,

Unable to parse cucumberjs output into json: '/report/cucumber.json' { Error: /report/cucumber.json: ENOENT: no such file or directory, open '/report/cucumber.json' 
    at Object.fs.openSync (fs.js:652:18) 
    at Object.fs.readFileSync (fs.js:553:33) 
    at Object.readFileSync (/Users/sarav/Documents/GitHub/automationtests/node_modules/jsonfile/index.js:67:22) 
    at isValidJsonFile (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/lib/reporter.js:404:48) 
    at Object.generate (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/lib/reporter.js:426:9) 
    at Object.generateReport [as generate] (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/index.js:30:21) 
    at /Users/sarav/Documents/GitHub/automationtests/features/support/hooks.js:49:22 
    at _combinedTickCallback (internal/process/next_tick.js:131:7) 
    at process._tickCallback (internal/process/next_tick.js:180:9) 
    errno: -2, 
    code: 'ENOENT', 
    syscall: 'open', 
    path: '/report/cucumber.json' } 

執行上面的代碼自動生成以.json和.html文件或需要我們手動創建一個以.json文件,並轉換到這一個html報告。

我一直在使用Java,它會在執行結束時自動創建json和html報告。

由於這是很新的,我無法弄清楚什麼缺少的一部分

感謝

回答

1

你的代碼來生成HTML報告會期望JSON文件:/report/cucumber.json一直存在。

因此,您需要其他代碼來幫助在測試運行期間生成json文件,我將在我的項目中使用代碼以供您參考。

注意:下面的代碼只能對黃瓜1工作,不能在Cucumver 2工作,下面是我所使用的版本:

"dependencies": { 
    "cucumber": "1.2.1", 
    "cucumber-html-reporter": "0.2.6", 

1)黃瓜JSON-report.js以產生運行期間的黃瓜JSON報告。

var fs = require('fs-extra'); 
var path = require('path'); 
var moment = require('moment'); 
var Cucumber = require('cucumber'); 

module.exports = function() { 

    var JsonFormatter = Cucumber.Listener.JsonFormatter(); 

    JsonFormatter.log = function(string) { 

    var outputDir = './reports'; 
    var targetJson = outputDir + '/cucumber_report.json'; 

    if (fs.existsSync(outputDir)) { 
     fs.moveSync(outputDir, outputDir + '_' + moment().format('YYYYMMDD_HHmmss') + "_" + Math.floor(Math.random() * 10000), { 
     overwrite: true 
     }); 
    } 
    fs.mkdirSync(outputDir); 
    fs.writeFileSync(targetJson, string); 
    }; 

    this.registerListener(JsonFormatter); 
}; 

2)screenshot.js採取截圖的時候失敗

module.exports = function() { 

    this.After(function(scenario, callback) { 
    if (scenario.isFailed()) { 
     browser.takeScreenshot().then(function(buffer) { 
     var decodedImage = new Buffer(buffer, 'base64'); 
     scenario.attach(decodedImage, 'image/png'); 
     callback(); 
     }, function(err) { 
     callback(err); 
     }); 
    } else { 
     callback(); 
    } 
    }); 
}; 

3)黃瓜HTML-report.js生成運行結束時,所有的功能後黃瓜HTML報告。

var reporter = require('cucumber-html-reporter'); 

module.exports = function() { 

    this.AfterFeatures(function(features, callback) { 
    var options = { 
     theme: 'bootstrap', 
     jsonFile: 'reports/cucumber_report.json', 
     output: 'reports/cucumber_report.html', 
     reportSuiteAsScenarios: true 
    }; 

    reporter.generate(options); 
    callback(); 
    }); 

};

4)量角器conf.js爲包括上述三個文件中cucumberOpts.require

cucumberOpts: { 
    monochrome: true, 
    strict: true, 
    plugin: ["pretty"], 
    require:[ 
     './step_definitions/*step.js', 
     './support/screenshot.js', 
     './support/cucumber-json-reporter.js', 
     './support/cucumber-html-reporter.js' 
    ] 
    tags: '' 
},