首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法用Nightwatchjs获得html报告

无法用Nightwatchjs获得html报告
EN

Stack Overflow用户
提问于 2020-06-15 14:03:03
回答 1查看 525关注 0票数 0

我想要有关测试结果的html报告,所以就去尝试一下。但我有一个问题,这是我已经做了我的设置,但无法运行它。

在安装之前,我已经完成了fspath的安装。

nightwatch.json文件下面。

代码语言:javascript
复制
{
  "src_folders" : ["tests"],
  "page_objects_path": "page-objects",
  "globals_path": "./globals.js",
  "output_folder": "reports",

  "webdriver" : {
    "start_process": true,
    "server_path": "node_modules/chromedriver/lib/chromedriver/chromedriver.exe",
    "port": 9515
  },

  "test_settings" : {
    "default" : {
      "desiredCapabilities": {
        "browserName": "chrome",
        "chromeOptions": {
          "args": ["incognito", "start-maximized", "disable-infobars"]
      }
      }
    }
  }
}

我已经完成了链接中提到的所有设置。但是得到一个错误,如:由npm test --reporter html-reporter.js运行它

失败:1错误(1ms) TypeError:无法读取未定义的属性'filename_prefix‘at Object.write Object.write at processTicksAndRejections (内部/进程/任务队列:93:93:5)

在下面的代码中,由于options param导致的错误为null。

代码语言:javascript
复制
module.exports = {
  write : function(results, options, done) {

    var reportFilename = options.filename_prefix + (Math.floor(Date.now() / 1000)) + '.html';
    var reportFilePath = path.join(__dirname, options.output_folder, reportFilename);

但是我看到所有的设置都是链接中提到的

EN

回答 1

Stack Overflow用户

发布于 2020-06-28 06:47:00

试试这个..。在相同的目录中创建html-cripter.hbs和html-cripter.js文件。

将下面的代码复制到html中,从下面复制. in。也要注意使用的路径。看起来不错。html的代码可以从https://gist.github.com/denji/204690bf21ef65ac7778中使用。

代码语言:javascript
复制
var fs = require('fs');
var path = require('path');
var handlebars = require('../../node_modules/handlebars');
var json2csv = require('../../node_modules/json2csv');
var _ = require('../../node_modules/lodash');
module.exports = {
  write : function(results, options, done) {
    var date = new Date();
    var dateString = date.getDate()  + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
    var ampm = ((date.getHours()) >= 12) ? "PM" : "AM";
    var timeStamp = dateString + " " + date.getHours() + ":" + date.getMinutes() + " " +ampm;

    var serverUrl = options.globals.server;
    var browser = options.filename_prefix.split('_').join(' ');

    var skippedCount = 0, totalTests = 0, testFailures = 0, totalTime = 0.0;
    var suiteNames = [];
    for (var workflow in results.modules) {
      if ((results.modules).hasOwnProperty(workflow)) {
        totalTests = totalTests + results.modules[workflow].tests;
        testFailures = testFailures + results.modules[workflow].failures;
        totalTime = parseFloat(totalTime)+ parseFloat(results.modules[workflow].time);
        suiteNames.push(results.modules[workflow].group);
        for (var skippedTests in results.modules[workflow].skipped) {
          if ((results.modules[workflow].skipped).hasOwnProperty(skippedTests)) {
            skippedCount = skippedCount + parseInt(skippedTests.length);
          }
        }
      }
    }
    var testsPassed = totalTests - testFailures - skippedCount;
    this.writeToHTML(results, options, timeStamp,browser, serverUrl,totalTests, testsPassed,testFailures,skippedCount,done);

  },

  writeToHTML : function(results, options, timeStamp,browser, serverUrl,totalTests, testsPassed,testFailures,skippedCount,done) {
    
    let datestm = new Date();
    let month = parseInt(datestm.getMonth()) + 1;
    let executionTime = datestm.getDate() + '-' + month + '-' +datestm.getFullYear() +'_'+datestm.getHours()+'-'+datestm.getMinutes()+'-'+datestm.getSeconds();


    var htmlFilename = options.globals.reportFileName +'_'+executionTime+'.html';
    var htmlFilePath = __dirname + '/../../testReports/' + htmlFilename;

    // var htmlFilename = options.globals.reportFileName + '.html';
    // var htmlFilePath = __dirname + '/../../testReports/' + htmlFilename;

    // read the html template
    fs.readFile('./testReports/custom-report/html-reporter.hbs', function (err, data) {
      if (err) throw err;
      var template = data.toString();

      // merge the template with the test results data
      var html = handlebars.compile(template)({
        results: results,
        options: options,
        timestamp: timeStamp,
        browser: browser,
        server: serverUrl,
        totalTests: totalTests,
        testsPassed: testsPassed,
        testsFailed: testFailures,
        testsSkipped: skippedCount,
        totalAssertions: results.tests,
        assertionsPassed: results.passed,
        assertionsFailed: results.failed
      });

      // write the html to a file
      fs.writeFile(htmlFilePath, html, function (err) {
        if (err) throw err;
        console.log('Report generated: ' + htmlFilePath);
        done();
      });
    });
  }
};

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62389853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档