【问题标题】:how does the function calls be executed with protractor promise calls量角器承诺调用如何执行函数调用
【发布时间】:2019-03-08 12:41:03
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript protractor


    【解决方案1】:

    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.
    

    【讨论】:

    • 非常感谢永。这从巨大的压力中解脱出来。万分感谢!!量角器团队对此是否有任何支持文件,道歉,如果事情回到我身边,想要保留一些记录。
    猜你喜欢
    • 2017-08-12
    • 2022-01-08
    • 2018-07-06
    • 2016-12-19
    • 1970-01-01
    • 2019-01-21
    • 2023-03-28
    • 1970-01-01
    • 2020-01-22
    相关资源
    最近更新 更多