函数未完成时通过

Passed when the function is not finished

所以我一直在使用量角器进行 passing/failing 测试,并且我已经在脚本中创建了大量的点击次数。基本上是 运行 x 点击次数,一旦完成就应该通过。

新编辑

it('Click remove button', function (done) {

    let allProds = element.all(by.css('div.stock-controller'));

    allProds.count()
    .then(function (cnt) { // amount of products

        let allPromises = []

        for(let index=0;index<cnt;index++) {

            let section = allProds.get(index),

                // message string which include qty in stock
                stock_qty_str = section.element(by.css('div.message')).getText(),
                // user inputed qty 
                user_qty_str = section.element(by.css('div.quantity-input input'))
                                      .getAttribute('value'),
                // button Descrease
                btn_dec = section.element(by.css('button[aria-label="Decrease"]'));

            allPromises.push(Promise.all([stock_qty_str, user_qty_str])
                .then(function(data){
                    // use RegExp to extract qty in stock
                    let group = data[0].trim().match(/^Sorry.*?(\d+)/)

                    if(group) {
                        let stock_qty = group[1] * 1,
                            user_qty = data[1].trim() * 1,
                            gap = user_qty - stock_qty; // click times of Decrease button

                        for(let i=0;i<gap;i++) {
                            btn_dec.click();
                            browser.sleep(1000).then(function(){
                                console.log('Click Decrease button: ' + i + '/' + gap)
                            })
                        }
                    }
                })
            )

        }
        return Promise.all(allPromises)

    })
    .then(()=>{
        done();
    })

});

但是我的问题是它现在所做的是:

如您所见,它会计算应该点击的次数,然后将其标记为已完成,但在通过后它仍然会点击,我想说这很奇怪...

我想知道如何让它等到功能完全完成然后将其标记为passed/failed?

您需要 return 一个承诺,以便测试等待该承诺。

目前,承诺 return 立即生效,无需等待点击。

您需要从 for 和 return 中收集所有 Promise.all 作为承诺(可能再次使用 Promise.all

像这样

it('Click remove button', function (done) {

 let allProds = element.all(by.css('div.stock-controller'));

 allProds.count()
 .then(function (cnt) { // amount of products
    let allPromises = []
    for(let index=0;index<cnt;index++) {

        let section = allProds.get(index),

            // message string which include qty in stock
            stock_qty_str = section.element(by.css('div.message')).getText(),
            // user inputed qty 
            user_qty_str = section.element(by.css('div.quantity-input input'))
                                  .getAttribute('value'),
            // button Descrease
            btn_dec = section.element(by.css('button[aria-label="Decrease"]'));

            allPromises.push(Promise.all([stock_qty_str, user_qty_str])...)
     }

     return Promise.all(allPromises)
  })
  .then(()=>{
    done();
  })

});