量角器 - 如何获得承诺数组的结果到另一个数组 [英] protractor - how to get the result of an array of promises into another array

查看:87
本文介绍了量角器 - 如何获得承诺数组的结果到另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个code element.all承诺数组(by.repeater('在单位单元')),我发现真的很难数据放在另一个数组

  element.all(by.repeater('单位单位'))。然后(功能(ARR){
    变种项= [];    对于(VAR I = 0; I< arr.length;我++){
      改编[I] .getText()。然后(函数(文本){
        items.push(文本);
      });
    }   //问题ITEMS是空的
   的console.log(项目);
});


解决方案

管理工作再上一个简单的方法使用Q和中继器避免了相同的结果。使用内置的地图是卓有成效的。

  VAR选项卡= element.all(by.css('。unitTabs李一'))。图(功能(榆树){
    返回elm.getText();
});tabs.then(功能(结果){
    VAR分类= _.sortBy(因此,函数(名称){返回名称;});
    对于(VAR I = 0; I< result.length;我++){
        预期(结果[I])TOBE(排列[I])。
    }
});

I got an array of promises from this code element.all(by.repeater('unit in units')) that I am finding really difficult to get the data into another array

element.all(by.repeater('unit in units')).then(function (arr) {
    var items = [];

    for (var i = 0; i < arr.length; i++) {
      arr[i].getText().then(function(text) {
        items.push(text);
      });
    }

   //PROBLEM ITEMS is Empty
   console.log(items);
});

解决方案

Managed to get the same result on a simpler way avoiding using Q and the repeater. Using the inbuilt map does the trick.

var tabs = element.all(by.css('.unitTabs li a')).map(function (elm) {
    return elm.getText();
});

tabs.then(function (result) {
    var sorted = _.sortBy(result, function (name) { return name; });
    for (var i = 0; i < result.length; i++) {
        expect(result[i]).toBe(sorted[i]);
    }
});

这篇关于量角器 - 如何获得承诺数组的结果到另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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