gpt4 book ai didi

javascript - 如何使用 Protractor promise 调用执行函数调用

转载 作者:行者123 更新时间:2023-11-30 06:17:50 24 4
gpt4 key购买 nike

我是 Protractor 的新手,因为我对 Javascript 了解不多,因此很难预测脚本执行流程。我对 Protractor promise 的行为和 js 文件之间的数据流有非常基本的问题。

我有一个 commonTC.js,它有 describe(),it() block 。在这个“它” block 中,我在运行时调用函数。意思是,我在另一个 js 文件中编写了诸如 open()、click()、enter() 之类的函数,该文件在运行时根据从 excel 读取的函数调用数据动态调用。

在 Protractor 中,大多数浏览器交互返回 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;
});

在ActivityAction.js()中,函数定义如下

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 中知道浏览器 promise 结果?2.同样通过这种架构,所有的函数调用都是同步的。因为所有函数都作为 promise 返回,所以这些 promise 会串行执行吗?我的意思是在第一个 promise 解决之后,是否保证第二个 promise 将被执行/或者它也可能选择第五个?3. 如果它随机选择 promise 执行那么我怎样才能实现 promise 的串行执行?4. 如果需要移除 commonTC.js 中的 promise 架构,那么如何确保每个函数 open()/enter() 返回失败/成功?

最佳答案

Protractor 有一个名为 Control Flow 的内置 promise 管理器,它将 promise 保存在一个列表中,并保证列表中的 promise 按照它们在脚本中出现的顺序连续执行。

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.

关于javascript - 如何使用 Protractor promise 调用执行函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55063459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com