我有以下黄瓜测试:
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();
这是带有一些基本测试代码的GIVEN装饰器:
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;
}
}
我能够将@Given和@Then参数的值读入装饰器函数。我也能够调整特定的方法。我遇到的问题是如何使用类装饰器抽象当前构造函数中的defineSupportCode函数。
任何方向都会受到赞赏...... 谢谢!
答案 0 :(得分:0)
这是一个有效的实施方案。
功能文件:
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;
}
}
随意指出任何可能的改进。