[英]Decorate step implementation in Cucumber-js
Introduction介绍
Im using https://github.com/cucumber/cucumber-js in typescript.我在打字稿中使用https://github.com/cucumber/cucumber-js 。 Sample of code implementation looks like average cucumber implementation:
代码实现示例看起来像普通的黄瓜实现:
import {
Given,
Then,
When
} from 'cucumber'
Given(`Page is up and running`, function(this: World) {
someFunction()
})
Goal目标
What I would like to achieve is to be able to somehow decorate Given
, so that:我想要实现的是能够以某种方式装饰
Given
,以便:
Given
.Given
中的代码之前调用某些操作。 Lets say, for simple example, I would like to print first Given
argument (so Page is up and running
).Given
参数(所以Page is up and running
)。 Each step got two alias function definitions in index.d.ts
, eg:每个步骤在
index.d.ts
中都有两个别名函数定义,例如:
export function Given(pattern: RegExp | string, code: StepDefinitionCode): void;
export function Given(pattern: RegExp | string, options: StepDefinitionOptions, code: StepDefinitionCode): void;
In the original version of CucumberJS, there were more hooks available including beforeStep
.在 CucumberJS 的原始版本中,有更多可用的钩子,包括
beforeStep
。 However, there is a way to achieve this functionality in versions of the framework such as 6.0.5 using setDefinitionFunctionWrapper
.但是,有一种方法可以使用
setDefinitionFunctionWrapper
在框架版本(例如 6.0.5)中实现此功能。 Example:例子:
setDefinitionFunctionWrapper(fn => async function step(...args) {
//
// do something before the step definition runs.
//
const result = await fn.apply(this, args);
//
// do something after the step definition runs.
//
return result;
});
Online Example: https://testjam.io/?p=O2wodDVOveAMdA9HKMKy在线示例: https ://testjam.io/?p=O2wodDVOveAMdA9HKMKy
Known limitations and caveats :已知的限制和注意事项:
Update: The beforeStep
and afterStep
hooks are available in CucumberJS 7.x and later更新:
beforeStep
和afterStep
钩子在CucumberJS 7.x 及更高版本中可用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.