2016-04-28 50 views
1

在我的Angular應用程序中,我有一個表格(ng-grid是精確的),並且我有一個向它添加新元素的過程。我省去了細節,但是這個過程是多步驟的,涉及與服務器交談並在ng-grid表中生成一個新項目。我知道新項目的名稱,我想檢查量角器測試是否確實添加了這個項目。這是我試圖做到這一點:檢查新元素是否已添加到量角器中的表中

var nameTestItem="mySuperItem"; 
//... some actions to add the element, and going back to the page with the table 
browser.get('#/resTable'); 

//checking if there is new item with this title 
var element_added=false; 
//picking the cell of the table with css selector and searching for the text 
element.all(by.css('div.ui-grid-cell-contents')).then(function(elements) { 
    _.forEach(elements, function(el) { 
     el.getText().then(function(text){ 
      console.log(text); 
      element_added= element_added || (nameTestItem==text); 
     }); 
    }); 
}); 
browser.sleep(1000); 

browser.pause(); 
expect(element_added).toBeTruthy(); 

顯然我的問題是,我正在處理很多承諾。你將如何解決這個問題?我真的不想依靠count(),因爲我不想在幾個人執行測試時發生碰撞。

回答

3

我會使用filter()代替:

var results = element.all(by.css('div.ui-grid-cell-contents')).filter(function(elm) { 
    return elm.getText().then(function(text) { 
     return nameTestItem === text; 
    }); 
}); 
expect(results.count()).toEqual(1); 

而且,如果你使用的是jasmine-matchers,可讀性更強一點的方法:

expect(results).toBeNonEmptyArray(); 
相關問題