访问父变量.then在子变量.then中的变量,嵌套的承诺,prototractor。

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

如何从子项目中访问一个变量 .then 变量被定义在一个父变量 .then

你可能已经猜到了,我对javascript是个新手。我只知道同步思维。

我现在需要一个快速的解决方案。我读过关于末日金字塔嵌套和链式承诺的文章,但无法理解它。如果你能给我一个工作代码,我将非常感激,这样我就可以获得 items[i] 子变量 .then

var options = element.all(by.xpath("//....."));

options.then(function(items){
   for(var i=0; i<items.length;i++){
      items[i].getAttribute("disabled").then(function(attr){
              if(attr){ // do something based on attr value
                  var option = items[i];    
                  // unable to access items => option is undefined
                  console.log(option); 
              }
          });
   }
});
javascript protractor angular-promise
1个回答
1
投票

要一次性获得所有元素的属性,你可以使用

var disabledArray = element.all(by.xpath("some_xpath")).getAttribute("disabled");
disabledArray.then(function(disabledValues){
  console.log(disabledValues); // prints the array of attribute values [true, true, false, ...etc]
});

要根据某些条件过滤元素列表,可以使用

var disabledElements = element.all(by.xpath("some_xpath")).filter(function(ele) {
       return ele.getAttribute("disabled").then(function(isDisabled){
           return isDisabled === true;
        });
});
© www.soinside.com 2019 - 2024. All rights reserved.