2016-11-18 151 views
3
var DrpStack = browser.findElement(by.xpath(XPath)) 
      var Elems = DrpStack.findElements(by.tagName(TagName)) 
      Elems.then(function(list){ 
       for (var i = 0; i < list.length; i++) { 
        if (list[i].getAttribute("aria-pressed")=='true') { 
         Labl1.list[i].getAttribute("onlabel"); 

        } else { 
         console.log("FAIL"); 
        } 
       } 

       Q.all(Labl1).done(function (result) { 
        // Q.All will print the results when the lookups and processing are done 
        console.log(result.length); 
        console.log(result); 
       }); 
      }); 

這裏我檢查'aria-pressed'的屬性是true還是false。如果該元素被推入數組中,那麼它應該打印爲false。 它不起作用,日誌始終打印失敗。我做錯了什麼。請幫忙。比較元素的屬性

回答

1

方法getAttribute返回一個Promise。所以你首先需要解決它來獲得價值。

在你的情況,你可以用promise.filter過濾的屬性:

var filter = protractor.promise.filter; 

filter(browser.findElements(...), (elem, i) => { 
    return elem.getAttribute("aria-pressed").then(attr => { 
     if(attr === "true") return true; 
     console.log(`failed attribute at index {i}`); 
    }); 
}).then(results => { 
    console.log("elements with attribute:", results); 
}); 
+0

感謝名單伴侶。該承諾工作。 – Ramii