2017-07-04 127 views
0

我使用Nightwatch-Cucumber自動化我的end2end測試,並希望創建一個黃瓜html報告後測試執行與cucumber-html-reporter,但我得到一個錯誤,而報告生成時cucumber-html-reporter使用黃瓜-html記者與Nightwatch黃瓜意外結束

Unable to parse cucumberjs output into json: 'reports/cucumber.json' SyntaxError: reports/cucumber.json: Unexpected end of JSON input 
    at JSON.parse (<anonymous>) 
    at Object.readFileSync (/Users/GRme/projects/e2e-web-tests/node_modules/jsonfile/index.js:69:17) 

而且我不知道爲什麼我產生cucumber.json是無效的。

我使用以下版本:

"cucumber-html-reporter": "^2.0.0", 
"nightwatch": "^0.9.16", 
"nightwatch-cucumber": "^7.1.10", 

這是我在nightwatch.conf.js配置:

require('nightwatch-cucumber')({ 
    cucumberArgs: [ 
     '--tags', '@run', 
     '--require', 'timeout.js', 
     '--require', 'hooks.js', 
     '--require', 'features/step_definitions', 
     '--format', 'pretty', 
     '--format', 'json:reports/cucumber.json', 
     'features'] 
}); 

這是hooks.js,其中執行我的黃瓜生成HTML報告:

const {client} = require('nightwatch-cucumber'); 
const {defineSupportCode} = require('cucumber'); 
var reporter = require('cucumber-html-reporter'); 

var options = { 
    theme: 'bootstrap', 
    jsonFile: 'reports/cucumber.json', 
    output: 'reports/cucumber_report.html', 
    reportSuiteAsScenarios: true, 
    launchReport: false, 
    //ignoreBadJsonFile: true, 
    name: 'NIKITA end2end tests', 
    brandTitle: 'NIKITA end2end tests', 
    storeScreenShots: true, 
    metadata: { 
// "App Version": "0.0.1", 
// "Test Environment": "AAT", 
// "Browser": "Chrome XXX", 
// "Platform": "Mac OS X", 
    } 
}; 

defineSupportCode(({Before, After}) => { 
    Before(function() { 
    client.maximizeWindow(); 
    }); 

    After(function() { 
    reporter.generate(options); 
    }); 
}); 

我生成並顯然無效cucumber.json看起來是這樣的:

[ 
    { 
    "keyword": "Feature", 
    "line": 1, 
    "name": "only a test feature", 
    "tags": [], 
    "uri": "/Users/GRme/projects/e2e-web-tests/features/testFeature.feature", 
    "elements": [ 
     { 
     "keyword": "Scenario", 
     "line": 4, 
     "name": "only a test Scenario", 
     "tags": [ 
      { 
      "line": 3, 
      "name": "@run" 
      } 
     ], 
     "id": "only-a-test-feature;only-a-test-scenario", 
     "steps": [ 
      { 
      "arguments": [], 
      "keyword": "Before", 
      "result": { 
       "status": "passed", 
       "duration": 1 
      }, 
      "hidden": true, 
      "match": { 
       "location": "/Users/GRme/projects/e2e-web-tests/hooks.js:24" 
      } 
      }, 
      { 
      "arguments": [], 
      "keyword": "When ", 
      "name": "\"1\" seconds waiting", 
      "result": { 
       "status": "passed", 
       "duration": 2615 
      }, 
      "line": 5, 
      "match": { 
       "location": "/Users/GRme/projects/e2e-web-tests/features/step_definitions/abstractStepDefinition.js:10" 
      } 
      }, 
      { 
      "arguments": [], 
      "keyword": "After", 
      "result": { 
       "status": "passed", 
       "duration": 4 
      }, 
      "hidden": true, 
      "match": { 
       "location": "/Users/GRme/projects/e2e-web-tests/hooks.js:28" 
      } 
      } 
     ] 
     } 
    ], 
    "id": "only-a-test-feature" 
    } 
] 

通過詹金斯產生黃瓜HTML報告與Cucumber Reports Plugin運行成功。

那麼,我該如何解決我的問題以及哪個框架(Nightwatch-Cucumbercucumber-html-reporter)纔是原因呢?什麼是我生成的cucumber.json的無效部分?

+0

您發佈的JSON是有效的。檢查文件以確保沒有額外的東西,比如BOM字符。 – Barmar

+0

這真的是問題的原因。我在一個編輯器中打開並保存了'cucumber.json',並將其保存爲一個沒有BOM的UTF-8文件,現在它運行。所以問題應該是'Nightwatch-Cucumber',它創建並寫入這個文件。我如何確保在JavaScript中刪除這些BOM?有沒有Node.js庫來做到這一點? – Martin

回答

0

我使用Event Handler解決了問題。我將黃瓜報告生成功能應用於事件AfterFeatures的Event Handler功能,它對我來說非常合適。

更多信息你可以找到here

0

嘗試在測試執行的單獨步驟中生成黃瓜html報告。例如,如果您正在使用npm腳本,請使用單獨的npm腳本來生成報告。你的方法的問題是,在Cucumber.js全局After掛鉤報告尚未創建。這就是爲什麼你會遇到這樣的錯誤。我建議使用單獨的node.js腳本來運行cucumber-html-reporter。