检查新元素是否已添加到 Protractor 中的 table

Check if the new element has been added to the table in Protractor

在我的 Angular 应用程序中,我有一个 table(准确地说是 ng-grid),并且我有一个向其中添加新元素的过程。我不告诉你细节,但这个过程是多步骤的,涉及与服务器对话并在 ng-grid table 中产生一个新项目。我知道新项目的名称,我想在我的量角器测试中检查是否确实添加了该项目。这就是我正在尝试的方式:

 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() 因为我不想在几个人执行测试时发生冲突。

我会用 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();