我是量角器的新手,因为我对Javascript知之甚少,因此难以预测脚本执行流程。关于量角器承诺的行为和js文件之间的数据流,我有一个非常基本的问题。
我有一个commonTC.js,它有describe(),it()块。在这个'it'块中,我在运行时调用函数。意思是,我有一些函数,比如open(),click(),enter()写在另一个js文件中,在运行时根据从excel读取的函数调用数据动态调用。
在量角器中,大多数浏览器交互都会返回promise,因此当我在click()/ enter()中执行浏览器调用逻辑这些返回promise时,我想在commonTC.js中获取浏览器调用的详细信息,这是调用文件。
所以我在commonTC.js中将函数调用作为promise()
dynamicActions[enter()].then(()=>{
console.log("Success Activity:")
}).catch((err)=>{
console.log("Error on Activity():")
throw err;
});
在Activity Action.is()中,函数定义如下
this.dynamicActions.enter = function () {
var deferred = protractor.promise.defer();
element(by.id("username")).sendKeys("abc").then(
function success() {
deferred.fulfill();
},
function error(reason) {
deferred.reject(reason);
});
return deferred.promise;
}
我的问题是 - 1.由于这种架构,所有函数调用都需要作为promise返回。如果我不返回promise,浏览器承诺结果我是否会在commonTC.js中知道? 2.同样通过这种架构,所有函数调用都是同步的。因为所有函数都是作为promise返回的,所以这些promise会串行执行吗?我的意思是在第一个承诺解决之后,是否保证第二个承诺将被执行/或者它也可能选择第五个? 3.如果它随机选择承诺执行,那么我如何实现承诺的串行执行? 4.如果需要删除commonTC.js中的promise体系结构,那么如何确保每个函数open()/ enter()返回失败/成功?
Protractor有一个名为Control Flow的内置承诺管理器,它将承诺保存在列表中,并保证列表中的承诺按顺序执行,就像它们出现在脚本中一样。
it('promise execute order', ()=>{
browser.get('...') // promise 1
element(by.xxx(...)).sendKeys(); // promise 2
element(by.xxx(...)).sendKeys()
.then(()=>{ yyyy.then()....}) // nested `then()`
.then()
.catch() // promise 3, no matter how many `then()` followed
// and nested. They are treated as single promise
element(by.xxx(...)).click(); // promise 4
})
// There are 4 lines in above `it`, every protractor api return a promise
// thus there will be a list includes 4 promises:
// [promise 1, promise 2, promise 3, promise 4]
// Control Flow makes sure promise in list executed one by one.