Protractor 如何比较表中的名称列表

Protractor 如何比较表中的名称列表,protractor,Protractor,我的测试场景是搜索姓氏,并期望表中的所有名称是否都等于搜索值。我有一个不同的功能来搜索姓氏 我现在想要的是获取表中的所有名称,并测试所有名称是否具有相同的值。我想在我的页面对象中使用下面的函数,并在规范中的expect中使用它。如何做到这一点 我不知道如何使用getText并将它们放入数组中并返回数组,以便在expect中使用它 this.getAllBorrowerNamesInTable = function () { element.all(by.binding('row.borr

我的测试场景是搜索姓氏,并期望表中的所有名称是否都等于搜索值。我有一个不同的功能来搜索姓氏

我现在想要的是获取表中的所有名称,并测试所有名称是否具有相同的值。我想在我的页面对象中使用下面的函数,并在规范中的expect中使用它。如何做到这一点

我不知道如何使用getText并将它们放入数组中并返回数组,以便在expect中使用它

this.getAllBorrowerNamesInTable = function () {
    element.all(by.binding('row.borrowerName')).then(function (borrowerNames){
     });        
};

您可以使用javascript的“推送”功能添加每个借款人的姓名,然后我们可以返回该数组

this.getAllBorrowerNamesInTable = function () {
var names = [];
element.all(by.binding('row.borrowerName')).then(function (borrowerNames){
    borrowerNames.each(function(borrowerName) {
        borrowerName.getText().then(function(name) {
            names.push(name);
        });
    });
 });
 return names;        
};
我正在使用来完成这项工作:

this.getAllBorrowerNamesInTable = function () {
    return element.all(by.binding('row.borrowerName')).map(function(elem) {
        return elem.getText();
    )};
}
除了使用map之外,您还可以通过在ElementArrayFinder上调用getText来实现它—element.all调用的结果:

然后,您可以断言结果等于字符串数组:

expect(page.getAllBorrowerNamesInTable()).toEqual(["Borrower 1", "Borrower 2"]);

这将返回一个空数组。因为names.push是异步进行的,所以FYIi不知道getText将在elementArrayFinder中工作……感谢您提供的信息
expect(page.getAllBorrowerNamesInTable()).toEqual(["Borrower 1", "Borrower 2"]);