无法阅读财产' JsonFormatter'未定义的

时间:2017-06-13 16:00:34

标签: protractor cucumberjs

随着即将推出的Cucumberjs2.3.0,我不得不对hooks.js文件进行一些更改,以便让cucumber-html-reporter再次工作。

hooks.js使用执行报告显式创建一个json文件,稍后我将用它来生成HTML报告(因为我需要JSON和小黄瓜输出)。

这是hooks.js,其中包含Cucumberjs2.3.0的一些更新:

var JsonFormatter = require('cucumber').JsonFormatter;
var fs = require('fs');
var reporter = require('cucumber-html-reporter');
var CucumberHtmlReport = require('cucumber-html-report');
var seleniumWebdriver = require('selenium-webdriver');
var {defineSupportCode} = require('cucumber');

'use strict';

defineSupportCode(function({After,registerHandler,registerListener}) {

    var outputDir = './'

    After(function (scenario,callback) {
        if (scenario.isFailed()) {
            browser.takeScreenshot().then(function (base64png) {
                 scenario.attach(new Buffer(base64png, 'base64'), 'image/png');
                 callback();
            }, function (err) {
                return callback(err);
            });
        } else{
            callback();
        }
    });

    var createHtmlReport = function (sourceJson) {
        var report = new CucumberHtmlReport({
            source: sourceJson
            , dest: outputDir
        });
        report.createReport();
    };

    //I don't know what method to call since now JsonFormatter is a class!
    var JsonFormatter = Cucumber.Listener.JsonFormatter();
    JsonFormatter.log = function (string) {
        if (!fs.existsSync(outputDir)) {
            fs.mkdirSync(outputDir);
        }

        var targetJson = outputDir + 'cucumber.json';
        fs.writeFile(targetJson, string, function (err) {
            if (err) {
                console.log('Failed to save cucumber test results to json file.');
                console.log(err);
            } else {
                createHtmlReport(targetJson);
            }
        });
    };

   registerListener(JsonFormatter);
});

我注意到Cucumberjs2.3.0,不再有一个包含JsonFormatter的Listener类。所以这让我想到了我的问题,我不知道如何调用该方法来创建JSON文件,该文件位于hooks.js的这一行中:

var JsonFormatter = Cucumber.Listener.JsonFormatter(); ///?????

当我运行测试时,我收到此错误:

Unhandled rejection TypeError: Cannot read property 'JsonFormatter' of undefined
    at C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\support\hooks.js:36:39
    at C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\cucumber\lib\support_code_library\builder.js:77:12
    at Array.forEach (native)
    at Object.build (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\cucumber\lib\support_code_library\builder.js:76:7)
    at Cli.getSupportCodeLibrary (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\cucumber\lib\cli\index.js:137:32)
    at Cli.<anonymous> (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\cucumber\lib\cli\index.js:144:39)
    at Generator.next (<anonymous>)
    at Generator.tryCatcher (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\util.js:16:23)
    at PromiseSpawn._promiseFulfilled (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\generators.js:97:49)
    at Promise._settlePromise (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\promise.js:574:26)
    at Promise._settlePromise0 (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\promise.js:693:18)
    at Async._drainQueue (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\async.js:133:16)
    at Async._drainQueues (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:649:20)

[11:06:33] E/launcher - BUG: launcher exited with 1 tasks remaining
npm ERR! Test failed.  See above for more details.

我需要在hooks.js中进行哪些更改,以便在5月12日Cucumberjs2.3.0出现之前使其正常工作?

这是我的量角器配置文件,以防万一:

var featsLocation = 'features/';
var stepsLocation = 'steps/';

exports.config = {
    directConnect: true,
    chromeDriver: '/srv/build/applications/chromedriver/chromedriver.exe',
    seleniumServerJar: '/srv/build/applications/selenium/selenium-server-standalone-3.4.0.jar',
    rootElement: 'html',
    params:{
        authURL: '',    
        login:{
            email:'',
            passw:''
        }
    },
    resultJsonOutputFile:'',
    getPageTimeout: 30000,
    allScriptsTimeout: 30000,
    framework: 'custom',
    frameworkPath: require.resolve('protractor-cucumber-framework'),
    capabilities: {
        'browserName': 'chrome',
            chromeOptions:{
                args:[]//"--headless"]
            }
        //'browserName': 'phantomjs',
        //'phantomjs.binary.path': '/srv/build/applications/phantomjs/bin/phantomjs'   //<--Comment to run from local
    },

    onPrepare: function(){
        global.EC = protractor.ExpectedConditions;
    },

    specs: [ /**/
              featsLocation+'authenticateCSM.feature'
            , featsLocation+'rejects.feature'
            , featsLocation+'rejects_sprint.feature'
     ],

     baseUrl: '',

     cucumberOpts: {
         tags: '',
         require: [
                   './support/*.js'
                    , stepsLocation+'*.spec.js'

        ],
        monochrome: true,
        strict: true,
        plugin: "json"
     },
};

这也是我的package.json:

{
  "name": "CucumberjsAT",
  "version": "1.0.0",
  "description": "Executes automated test, using Cucumberjs",
  "scripts": {
    "test": "protractor ./protractorConf.js --params.authURL=\"http://myweb.com\" --params.login.email=user@mail.com --params.login.passw=password --cucumberOpts.tags \"not @ignore and (@smoke or @me)\""
  },
  "author": "Kyon"
}

这是我的框架信息,以防它有用......

  • C:\ workspace&gt; npm view protractor-cucumber-framework version 3.1.1
  • C:\ workspace&gt; npm view cucumber version 2.3.0
  • C:\ workspace&gt;量角器--version版本5.1.1
  • C:\ workspace&gt; npm查看cucumber-html-reporter latest 2.0.0
  • C:\ workspace&gt; npm view cucumber-html-report latest 0.6.0

1 个答案:

答案 0 :(得分:0)

我发现自己是解决问题的方法。

通过在包上传递参数为json文件提供名称。然后,以下内容更改了hooks.js中的After场景。

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