我要做以下黄瓜试验:
import {defineSupportCode} from 'cucumber';
import action from './helpers/action';
import check from './helpers/action';
defineSupportCode(({ Given, Then }) => {
Given(/^I open the (url|site) "([^"]*)?"$/,action.openWebsite);
Then(/^I expect that the title is( not)* "([^"]*)?"$/,check.title);
});想要做的是创建一个类并对类和相关方法进行注释,并使用一个装饰器将黄瓜样板抽象出来。
下面是我创建的类,它仍然嵌入样板,但它可以工作:
class checkSite {
constructor(){
defineSupportCode(({ Given, Then }) => {
Given(/^I open the (url|site) "([^"]*)?"$/,this.openWebsite);
Then(/^I expect that the title is( not)* "([^"]*)?"$/,this.title);
});
}
public openWebsite(type: any, page: any) {
return action.openWebsite(type, page);
}
public title(Case: any, Title: any){
check.Title(Case, Title);
}
}
let CheckSite = new checkSite();下面是我希望这门课看起来的样子:
@binding
class checkSite {
@Given(/^I open the (url|site) "([^"]*)?"$/)
public openWebsite(type: any, page: any) {
return action.openWebsite(type, page);
}
@Then(/^I expect that the title is( not)* "([^"]*)?"$/)
public title(Case: any, Title: any) {
return check.Title(Case, Title);
}
}
let CheckSite = new checkSite();下面是具有一些基本测试代码的给定的装饰器:
export function given(value: any) {
return function(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>){
// save a reference to the original method
let originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
// pre
console.log('The method args are: ' + JSON.stringify(args));
// run and store the result
let result = originalMethod.apply(this, args);
// post
console.log('The return value is: ' + result);
// return the result of the original method
return result;
};
return descriptor;
}
}我能够将“给定”和“然后”参数的值读取到装饰函数中。我也能调整特定的方法。我陷入困境的地方是如何使用类装饰符来抽象当前构造函数中的defineSupportCode函数。
任何方向都将不胜感激..。谢谢!
发布于 2017-10-30 14:48:23
这是一个可行的实现。
特征文件:
Feature: Site test
As a developer
I want google site to have the correct title
Background:
Given I open the url "http://www.google.com"
Scenario: Google
Then I expect that the title is "Google"测试:
import { Cucumber, Given, When } from '../decorator/decorator'
import action from '../helpers/action';
import check from '../helpers/check';
@Cucumber
class checkSite {
@Given(/^I open the (url|site) "([^"]*)?"$/)
public openWebsite() {
return action.openWebsite;
};
@When(/^I expect that the title is( not)* "([^"]*)?"$/)
public title() {
return check.Title
};
};
let CheckSite = new checkSite();装潢师:
import {defineSupportCode} from 'cucumber';
export function Cucumber(target: Function){
for (var member in target.prototype) {
target.prototype[member]();
}
}
export function Given(expression: any) {
return function(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>){
let assertion = descriptor.value();
descriptor.value = () =>
defineSupportCode(({ Given }) => {
Given(expression,assertion);
});
return descriptor;
}
}
export function When(value: any) {
return function(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>){
let assertion = descriptor.value();
descriptor.value = () =>
defineSupportCode(({ When }) => {
When(value, assertion);
});
return descriptor;
}
}请随时指出任何可能的改进。
https://stackoverflow.com/questions/47013011
复制相似问题