从JavaScript测试自动化项目中获取功能和场景的总数

从JavaScript测试自动化项目中获取功能和场景的总数,javascript,automation,protractor,cucumber,cucumberjs,Javascript,Automation,Protractor,Cucumber,Cucumberjs,我正在使用量角器Cumber框架进行测试自动化。我有多个功能文件。每个要素文件都有多个场景。我正在使用“cucumber html reporter”获取测试执行的html报告。此HTML报告提供了有关功能总数和已执行场景总数的详细信息。所以在测试执行之后,我才知道我执行的“功能文件总数”和“场景总数” 是否有任何命令或插件可供获取 功能的总数 每个功能文件中的方案总数 在我的JavaScript测试自动化中?这在没有插件的情况下很容易实现 为什么不创建一个对象,将功能名称作为键,场景计数作

我正在使用量角器Cumber框架进行测试自动化。我有多个功能文件。每个要素文件都有多个场景。我正在使用“cucumber html reporter”获取测试执行的html报告。此HTML报告提供了有关功能总数和已执行场景总数的详细信息。所以在测试执行之后,我才知道我执行的“功能文件总数”和“场景总数”

是否有任何命令或插件可供获取

  • 功能的总数
  • 每个功能文件中的方案总数

在我的JavaScript测试自动化中?

这在没有插件的情况下很容易实现

为什么不创建一个对象,将功能名称作为键,场景计数作为值,然后
console.log()

我将展示这两种方法(2.x语法和1.x语法,我已经介绍了它们的基础)

CucumberJS 2.x语法

let {defineSupportCode} = require('cucumber'),
    counter = {};

defineSupportCode(({registerHandler, Before}) => {

    registerHandler('BeforeFeature', function (feature, callback) {
        global.featureName = function () {
            return feature.name;
        };
        callback();
    });

   Before(function (scenario, callback){
        counter[featureName()] !== undefined ? counter[featureName()] += 1 : counter[featureName()] = 1;
        callback();
   });

   registerHandler('AfterFeatures', function (feature, callback) {
        console.log(JSON.stringify(counter));
        callback();
  });
});
var counter = {};

module.exports = function () {

    this.BeforeFeature(function (feature, callback) {
        global.featureName = function () {
            return feature.name;
        };
        callback();
    });

   this.Before(function (scenario, callback){
        counter[featureName()] !== undefined ? counter[featureName()] += 1 : counter[featureName()] = 1;
        callback();
   });

   this.AfterFeatures(function (feature, callback) {
        console.log(JSON.stringify(counter));
        callback();
  });
};
CucumberJS 1.x语法

let {defineSupportCode} = require('cucumber'),
    counter = {};

defineSupportCode(({registerHandler, Before}) => {

    registerHandler('BeforeFeature', function (feature, callback) {
        global.featureName = function () {
            return feature.name;
        };
        callback();
    });

   Before(function (scenario, callback){
        counter[featureName()] !== undefined ? counter[featureName()] += 1 : counter[featureName()] = 1;
        callback();
   });

   registerHandler('AfterFeatures', function (feature, callback) {
        console.log(JSON.stringify(counter));
        callback();
  });
});
var counter = {};

module.exports = function () {

    this.BeforeFeature(function (feature, callback) {
        global.featureName = function () {
            return feature.name;
        };
        callback();
    });

   this.Before(function (scenario, callback){
        counter[featureName()] !== undefined ? counter[featureName()] += 1 : counter[featureName()] = 1;
        callback();
   });

   this.AfterFeatures(function (feature, callback) {
        console.log(JSON.stringify(counter));
        callback();
  });
};
额外的

如果您想将其保存到文件中,以便以后可以看到它,我建议使用fs extra库。使用以下命令代替
console.log()

fs = require('fs-extra');
fs.writeFileSync("path/to/file.js","let suite = " + JSON.stringify(counter));
请注意,该文件将从运行测试的位置创建

Given I am running from "frameworks/cucumberjs"
When I generate a file from "frameworks/cucumberjs/hooks/counter.js" with the fs library at "./file.js"
Then the file "frameworks/cucumberjs/file.js" should exist

Given I am running from "frameworks/cucumberjs"
When I generate a file from "frameworks/cucumberjs/features/support/hooks/counter.js" with the fs library at "./hello/file.js"
Then the file "frameworks/cucumberjs/hello/file.js" should exist
只要确保您是从正确的目录运行

功能的总数

如果还需要功能的总数,请执行以下操作:

代替
控制台.log()

并取代writeFile:

fs.writeFileSync("path/to/file.js","let suite = " + JSON.stringify(counter) + ", featureCount = " + Object.keys(counter).length);

由于我们已经按照每个功能名称对场景计数进行了排序,说明我们创建的对象中的键数量将为我们提供功能数量的计数。

一个通用的JS脚本,它涉及使用Gherkin将功能文件解析为AST,并对功能、场景、标记等进行计数。。根据该结构:

例如:


从这里,您可以遍历AST对象并提取功能/场景计数和所有其他所需信息。

请看一看——Faims——非常感谢您的详细解释