2015-07-01 72 views
1

我正在嘗試創建一個函數,它將梳理元素數組並返回符合條件的第一個實例。量角器 - 功能退出時返回待定承諾

這是我有我的測試內部的確實工作

element.all(by.css('_cssSelector_')).filter(function(elms, index) { 
     return elms.getAttribute('height').then(function(height) { 
      return parseInt(height) > 0; 
     }); 
    }).then(function(validElms) { 
     browser.actions().mouseMove(validElms[0]).perform(); 
    } 

...但如果我發揮了這一點,它不工作

getValidElm = function() { 
    var validElm = element.all(by.css('_cssSelector_')).filter(function (elms, index) { 
     return elms.getAttribute('height').then(function (height) { 
      return parseInt(height) > 0; 
     }); 
    }).then(function (validElms) { 
     return validElms[0]; 
     }); 

    return validElm; 
} 

如果然後我運行:

var validElmFromPage = getValidElm(); 
console.log(validElmFromPage); 

我得到:承諾:: 2046 {[PromiseStatus]:「待定」}

指向內部正在使用的功能外VAR之前沒有解決功能的東西的問題。在閱讀(廣泛地)通過這裏的帖子,甚至這個美妙的博客文章(http://spin.atomicobject.com/2014/12/17/asynchronous-testing-protractor-angular/)後,我仍然無法弄清楚交易是什麼。我知道這很簡單,最有可能與controlFlow相關?

感謝您的幫助。

回答

1

讓函數返回一個承諾。由於filter()返回ElementArrayFinder,你應該能夠使用first()

getValidElm = function() { 
    return element.all(by.css('_cssSelector_')).filter(function (elms, index) { 
     return elms.getAttribute('height').then(function (height) { 
      return parseInt(height) > 0; 
     }); 
    }).first(); 
} 

first()將返回ElementFinder這可以傳遞給mouseMove()

var elm = getValidElm(); 
browser.actions().mouseMove(elm).perform();