在实习生中进行功能测试时,使用辅助模块执行重复的有序任务 [英] Use helper modules for repeated, ordered tasks when functional testing in intern

查看:105
本文介绍了在实习生中进行功能测试时,使用辅助模块执行重复的有序任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个模块,在功能测试时填写表单输入,我希望能够从多个测试套件中调用它。



辅助文件的伪代码(helper.js)

  module.exports = {
fillForm:function( ){
this.findByCssSelector('#firstname')
.click()
.pressKeys('John')
.end()
},
anotherFunction:function(){
//更多代码
}
}

在功能测试的规范中,我将该模块加载为 helper ,我可以看到它执行。但是,我似乎无法使用此语法并保证链接步骤按照定义的顺序执行:

 '测试填充表单数据':function(){
返回this.remote
.get(require(toUrl(url))
//应首先发生
.then(helper.fillForm)
//应该发生第二个
.then(helper.anotherFunction)
//只有在上面的点击发生之后才会发生
.findByCsSelector('#submit')
// click evt应显示#someElement元素
.click()
.findByCssSelector('#someElement')
.getComputedStyle('display')
.then(style) {
//断言这里
}

似乎承诺链接允许点击事件到h在然后回调已执行之前appen。实习生可以实现这种流程吗?



更新:



暂时解决这个问题吧代码类型:

  var remote = initTest.call(this,url); 
return helpers.fillForm1Data.call(remote)
.otherChainedMethodsHere()
.moreChainedMethods()
.then(){
//断言代码

其中initTest方法执行url抓取,窗口大小调整,清除数据,fillForm1Data按预期执行。但是语法非常丑陋。

解决方案

你的助手不是返回任何值,以便将其视为同步回调,并立即执行链中的下一个操作。您也不能从promise帮助器返回此,否则会导致死锁(因为Command promise将等待自己解决 - 如果您尝试,Intern会抛出错误要做到这一点),所以你需要创建一个新命令并返回,如果你想在你的帮助器中使用链式命令接口:





< pre class =lang-js prettyprint-override> module.exports = {
fillForm:function(){
return this this.constructor(this.session)
.findByCssSelector('#firstname')
.click()
.pressKeys('John');
},
anotherFunction:function(){
// more code
}
};

你也可以从 this.session 相反,如果您不关心Command API的便利性并且可以处理正常的承诺回调链:

  module.exports = {
fillForm:function(){
var session = this.session;
返回session.findByCssSelector('#firstname')
.then(function(element){
return element.click();
})
.then( function(){
return session.pressKeys('John');
});
},
anotherFunction:function(){
// more code
}
};


I'm trying to create a module that will fill in form inputs when functional testing, and I'd like to be able to call it from multiple test suites.

Pseudo code for the helper file (helper.js)

module.exports = {
    fillForm: function() {
        this.findByCssSelector('#firstname')
            .click()
            .pressKeys('John')
            .end()
    },
    anotherFunction: function() {
        // more code
    }
}

In the spec for the functional test, I load that module as helper and I can see it execute. However, it seems I can't use this syntax and guarantee that the chained steps execute in the defined order:

'Test filling form data': function() {
    return this.remote
                .get(require(toUrl(url))
                // should happen first
                .then(helper.fillForm)
                // should happen second
                .then(helper.anotherFunction)
                // only after the above should the click happen
                .findByCsSelector('#submit')
                 // click evt should show the #someElement element
                .click()                     
                .findByCssSelector('#someElement')
                .getComputedStyle('display')
                .then(style) {
                     // assertions here
    }

It seems that the promise chaining allows the click event to happen before the then callbacks have executed. Is this sort of flow possible with intern?

UPDATE:

For the moment, working around this with this sort of code:

var remote = initTest.call(this, url);
return helpers.fillForm1Data.call(remote)
    .otherChainedMethodsHere()
    .moreChainedMethods()
.then() {
    // assertion code here

where the initTest method does url fetching, window sizing, clearing data, and the fillForm1Data does as you'd expect. But the syntax is pretty ugly this way.

解决方案

Your helper is not returning any value so it is treated as a synchronous callback and the next thing in the chain is executed immediately. You also cannot return this from a promise helper or it will cause a deadlock (because the Command promise will be waiting for itself to resolve—Intern will throw an error instead if you try to do this), so you need to create a new Command and return that if you want to use the chained Command interface within your helper:

module.exports = {
    fillForm: function() {
        return new this.constructor(this.session)
            .findByCssSelector('#firstname')
            .click()
            .pressKeys('John');
    },
    anotherFunction: function() {
        // more code
    }
};

You can also just return from this.session instead if you don’t care about the convenience of the Command API and can deal with normal promise callback chains:

module.exports = {
    fillForm: function() {
        var session = this.session;
        return session.findByCssSelector('#firstname')
          .then(function (element) {
            return element.click();
          })
          .then(function () {
            return session.pressKeys('John');
          });
    },
    anotherFunction: function() {
        // more code
    }
};

这篇关于在实习生中进行功能测试时,使用辅助模块执行重复的有序任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆