首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >抽象化E2E测试生物板的打字译码器

抽象化E2E测试生物板的打字译码器
EN

Stack Overflow用户
提问于 2017-10-30 10:27:54
回答 1查看 219关注 0票数 0

我要做以下黄瓜试验:

代码语言:javascript
复制
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);
});

想要做的是创建一个类并对类和相关方法进行注释,并使用一个装饰器将黄瓜样板抽象出来。

下面是我创建的类,它仍然嵌入样板,但它可以工作:

代码语言:javascript
复制
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();

下面是我希望这门课看起来的样子:

代码语言:javascript
复制
@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();

下面是具有一些基本测试代码的给定的装饰器:

代码语言:javascript
复制
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函数。

任何方向都将不胜感激..。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-30 14:48:23

这是一个可行的实现。

特征文件:

代码语言:javascript
复制
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"

测试:

代码语言:javascript
复制
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();

装潢师:

代码语言:javascript
复制
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;
    }
}

请随时指出任何可能的改进。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47013011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档