【问题标题】:Protractor: how to iterate and compare values got using repeater from application and from scenario tables量角器:如何迭代和比较使用转发器从应用程序和场景表中获得的值
【发布时间】:2026-01-06 10:20:03
【问题描述】:

我正在使用转发器函数从应用程序中获取实际值列表,并从场景数据表中获取预期值。如何存储和比较实际值与预期值?

场景: 场景:作为用户,我应该能够登录应用程序并查看用户列表和注册日期 假设我使用用户名“abcd”和密码“passwOrd123”登录应用程序 当我打开查看会员注册 然后必须列出以下用户和注册日期: |用户名|注册日期| |user1|2010 年 5 月 30 日 | |user2|2009 年 6 月 11 日 |

下面是我为该步骤编写的实现代码:

this.Then(/^Then the following users and registration date must be listed:$/, function (table) {

        var data = table.hashes();

        element.all(by.repeater('members in member list')).then(function(member) {

            for (i=0;i<member.length;i++) {
                member[i].element(by.binding('app.memberName')).getText().then(function (actualMemberName) {
                    console.log("Member Name: " + actualMemberName);
                });

                member[i].element(by.binding("app.reg.date | jsonDate:'MM/dd/yyyy'")).getText().then(function (actualRegDate) {
                    console.log("Actual Registration Date: " + actualRegDate);
                });
            }
        });
    });

“members in member”列表返回的用户列表不会一直保持相同的顺序。

【问题讨论】:

标签: javascript angularjs selenium protractor


【解决方案1】:

您使用的是哪个框架?如果您使用 Jasmine 框架,那么您可以在 for 循环中添加 expect 语句来验证实际值和预期值,例如 sn-p -

expect(actual_value).toEqual(expected_value);

如果您没有使用任何框架,那么您可以尝试使用像 sn-p 这样的纯 javascript 验证值 -

if(actual_value === expected_value)
    //do something
else
    //print error

if(actual_value.localeCompare(expected_value) == 0)
    //do something
else
    //print error

这两个函数的工作方式相同。

要存储转发器功能的用户列表,请查看MAP function 在量角器中可用。它将 element.all() 函数中的数据串行存储在一个数组中。您可以稍后使用该数组来检查表。这是一个例子-

element.all('selector here').map(function(ele){
    return ele.getText().then(function(arrayText){
            return arrayText;
        });
    }).then(function(arrayText){
            //arrayText is an array with list of elements from repeater function
            //Use the arrayText and compare it to your table
    });

希望这会有所帮助。

【讨论】:

  • 我得到了一个使用中继器功能的用户列表。我不知道如何存储它们并与我从场景中获得的数据表进行比较。
  • 更新了上面的map函数,可以帮助你存储repeater函数的值。