我正在使用量角器-黄瓜框架作为我的测试自动化。我有多个特征文件。每个特性文件都有多个场景。我正在使用“黄瓜- HTML -记者”来获得测试执行的HTML报告。此HTML报告提供了有关执行的特性总数和方案总数的详细信息。因此,只有在测试执行之后,我才知道我执行的“特性文件总数”和“方案总数”。
是否有任何命令或插件可用于
在我的JavaScript测试自动化中?
发布于 2017-05-18 21:44:03
一个通用的JS脚本,它涉及到使用Gherkin解析特性文件到AST并计数特性、场景、标记等。从这一结构中:
例:
const glob = require('glob')
const Gherkin = require('gherkin')
const parser = new Gherkin.Parser()
const AST = glob
.sync('./specifications/**/*.feature')
.map(path => parser.parse(fs.readFileSync(path).toString()))在那里,您可以遍历AST对象并提取特性/场景计数和所有其他所需信息。
发布于 2017-04-18 09:28:29
这是相当简单的实现没有插件。
为什么不创建一个以特征名为键的对象,场景作为值计算,或者将其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();
});
});CucumberJS 1.x语法
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-额外的库。代替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只需确保您正在从正确的目录运行。
特征总数
如果您也想要功能的总数:
代替console.log()
console.log(JSON.stringify(counter) + "\nFeature Count: " + Object.keys(counter).length)代替writeFile:
fs.writeFileSync("path/to/file.js","let suite = " + JSON.stringify(counter) + ", featureCount = " + Object.keys(counter).length);由于我们已经按照每个功能名称排序了场景计数,说明我们创建的对象中的键数量将给出功能数量的计数。
https://stackoverflow.com/questions/43459664
复制相似问题