Javascript 函数未完成时传递

Javascript 函数未完成时传递,javascript,protractor,Javascript,Protractor,因此,我一直在使用量角器来处理通过/失败测试的问题,并在脚本中创建了大量的点击操作。基本上,要运行x次点击,一旦完成,它应该通过 新编辑 it('Click remove button', function (done) { let allProds = element.all(by.css('div.stock-controller')); allProds.count() .then(function (cnt) { // amount of products

因此,我一直在使用量角器来处理通过/失败测试的问题,并在脚本中创建了大量的点击操作。基本上,要运行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();
    })

});
it('Click remove button',函数(done){
让allProds=element.all(by.css('div.stock-controller');
allProds.count()
.然后(函数(cnt){//产品数量
让所有的承诺=[]

对于(让index=0;index您需要返回一个承诺,以便测试等待该承诺

目前,承诺会立即返回,无需等待点击

您需要从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();
  })

});
it('Click remove button',函数(done){
让allProds=element.all(by.css('div.stock-controller');
allProds.count()
.然后(函数(cnt){//产品数量
让所有的承诺=[]
对于(设索引=0;索引{
完成();
})
});

Hmm我不确定我是如何做到这一点的?我也不确定我是否正确理解了它,是否有任何代码示例可以提供给我,让我以某种方式了解您的意思?我刚刚更新了代码,使其与您的代码一样,但问题仍然相同。即使它点击了,它仍然表示它已通过。与以前的问题完全相同。只是n注意到您有另一个不等待的承诺。browser.sleep(1000)。然后(…)。您需要收集所有这些承诺并返回它们。我使用了
。然后(异步函数(数据){
,而不是
。然后(函数(数据){
,如果我等待browser.sleep,它将等待浏览器等待