功能未完成时通过

问题描述 投票:0回答:1

因此,我一直在与量角器一起进行通过/失败测试,​​并在脚本中创建了大量的单击。基本上来说,要运行x次点击并完成一次,它就应该通过。

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

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

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

        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"]'));

            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)
                            })
                        }
                    }
                })

        }

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

});

但是我的问题是,现在要做的是:

IMAGE

正如您所看到的,发生的事情是它计算了应该单击的次数,然后将其标记为完成,但是在通过之后仍然单击,这很奇怪,我会说...

我想知道如何让它等待功能完全完成,然后将其标记为通过/失败?

javascript protractor
1个回答
0
投票

您需要返回一个诺言,以便测试等待该诺言。

此刻,诺言立即返回而无需等待点击。

您需要从for中收集所有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();
  })

});
© www.soinside.com 2019 - 2024. All rights reserved.