【问题标题】:WebDriver chaining within a for loop only iterates the loop without executing the callback在 for 循环中链接的 WebDriver 仅迭代循环而不执行回调
【发布时间】:2018-04-29 03:05:27
【问题描述】:

我正在编写 Appium (v1.7.1) iOS 自动化测试,我在其中链接 webdriver 会话并尝试循环遍历元素以获取数据。

setFilterOptions: function (driver, loc) {
    var chain = driver;                  //I am assigning the driver to chain
    chain = chain
         .getFilterElementListCount()    //This gives me back the count for elements
         .then((count) => {
            console.log("Number of Filters - ",count);  

            for(var i=1; i<=count; i++) {
              ((i) => {
                console.log("Value of i - ", i);
                //This gives me the label for each Cell->StaticText
                chain = chain
                      .getElAttribute('label',util.format('//XCUIElementTypeCell[%d]/XCUIElementTypeStaticText[1]', i), 'xpath')                           
                      .then((filterTitle, i) => {
                            console.log("Filter Title - ", filterTitle);
                            console.log("I value - ", i);
                      });
              })(i);
            }
         });
  return chain;
},    

The Console o/p i am getting is - 
Number of Filters -  3
Value of i -  1
Value of i -  2
Value of i -  3

循环迭代但不执行 for 循环中的链。有没有办法让链在返回之前完成所有回调执行。

【问题讨论】:

    标签: javascript promise mocha.js appium magellan-nightwatch


    【解决方案1】:

    您的目标是返回一个承诺,一旦所有循环中执行的工作完成,该承诺将解决。但是,这不是你正在做的。你的问题是你有这个:

      chain = chain.//
                    // Bunch of asynchronous operations, some of which assign 
                    // to the variable `chain`.
                    //
      return chain;
    

    为了让您的代码工作,异步操作必须分配给chainreturn 语句执行之前。但这不是异步操作的工作方式。当您调用任何异步方法时,您只是调度异步操作以供将来执行。它将在未来的某个时间执行,但绝对不会立即。在您拥有的代码中,您安排了一个异步操作并立即返回chain您返回的chain 的值不能是在循环中设置的值。您的循环尚未执行。

    您应该执行以下操作。重要的是在循环中创建一个操作链,并从您传递给.then 的函数中返回该链,以便最顶层的承诺解析为您在循环中创建的链。这样一来,您从方法返回的承诺必须等待所有内部操作完成后才能被解析。

    setFilterOptions: function (driver, loc) {
        return driver
             .getFilterElementListCount()    //This gives me back the count for elements
             .then((count) => {
                var chain = Promise.resolve();
                console.log("Number of Filters - ",count);  
    
                for(var i=1; i<=count; i++) {
                  ((i) => {
                    console.log("Value of i - ", i);
                    //This gives me the label for each Cell->StaticText
                    chain = chain
                          .getElAttribute('label',util.format('//XCUIElementTypeCell[%d]/XCUIElementTypeStaticText[1]', i), 'xpath')                           
                          .then((filterTitle, i) => {
                                console.log("Filter Title - ", filterTitle);
                                console.log("I value - ", i);
                          });
                  })(i);
                }
    
                // Return your promise!                
                return chain;
             });
    },    
    

    还请注意,如果您在循环中使用 let i 而不是 var i,则可以摆脱循环中立即调用的箭头函数,这似乎只是为了确保循环获取i 的顺序值(而不是全部以i 的最后一个值执行)。

    【讨论】:

    • 非常感谢您的回复!用你的答案解决了问题。另外,在循环中使用了 let i 并清理了方法。
    猜你喜欢
    • 1970-01-01
    • 2021-04-13
    • 2018-07-07
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 2015-02-03
    相关资源
    最近更新 更多