获得一系列承诺(量角器)的大小 [英] getting the size of an array of promises(protractor)

查看:82
本文介绍了获得一系列承诺(量角器)的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框列表,我想检查它们是否被选中。我想循环遍历对象数组并逐个检查,但我无法获得数组的大小。我尝试了以下两种方法,但我得到了不同的错误。

I have a list of checkboxes that I want to check if they are checked. I want to loop through the array of objects and check one by one but I cannot get the size of the array. I have tried the following 2 ways but I get different errors.

var i, n;
    networkStatusDetail.detailsList.all(by.tagName('div')).then(function(arr){
        console.log(arr.length);
        for(i=0;i<; i++){
            arr.get(i).element(by.model('displayedsites[site.siteid]')).isSelected().then(function(checked){
                expect(checked).toBe(true);
            });
        }
    });

使用上面的代码我得到一个错误,说arr.get()不是一个有效的函数。

With this code above I get an error saying that arr.get() is not a valid function.

以下代码不起作用,因为我无法获得数组的大小。将数组的大小分配给另一个变量只能在promise函数中使用,但是一旦在外部使用该变量,该值就会消失。

The following code does not work because I am unable to get the size of the array. Assigning the size of the array to another variable only works within a promise function, but once the variable is used outside the value is gone.

networkStatusDetail.detailsList.all(by.tagName('div')).then(function(size){
        n = size;
        console.log('n = '+n);
    });

    console.log('n outside '+ n);
for(i=0;i<n; i++){
        check.get(i).element(by.model('displayedsites[site.siteid]')).isSelected().then(function(checked){
    expect(checked).toBe(true);
   //   console.log(i);
        });
   }

2个控制台将分别打印512和undefined,显示n的值在promise函数之外不起作用。

the 2 consoles will print 512 and undefined respectively, showing that the value of n outside of the promise function does not work.

如果我手动设置for循环的最大值,我只能使它工作,但是这个值可以随时间变化这是不正确的。

I can only make it work if I manually set the max value of the for loop, but this value can change over time so It's not correct.

如果有人能帮我说明如何做到这一点,我将不胜感激。

If anybody could give me a hand on how to do this it would be greatly appreciated.

推荐答案

您不需要知道有多少个复选框可以循环使用它们 - 使用 map() each() 相反:

You don't need to know how many checkboxes are there to loop over them - use map() or each() instead:

element.all(by.model('displayedsites[site.siteid]')).each(function (checkbox) { 
    return checkbox.isSelected().then(function (isSelected) {
        console.log(isSelected);
    });
});

这篇关于获得一系列承诺(量角器)的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆