量角器:如何迭代和比较从应用程序和场景表使用中继器得到的值

Protractor: how to iterate and compare values got using repeater from application and from scenario tables

本文关键字:中继器 何迭代 迭代 量角器 比较 应用程序      更新时间:2023-09-26

我从应用程序使用中继器函数和期望值从场景数据表中获得的实际值的列表。如何存储和比较实际值与期望值?

场景:场景:作为用户,我应该能够登录到应用程序并查看用户列表和注册日期假设我登录到应用程序的用户名"abcd"和密码"passwOrd123"当我打开视图成员注册时然后必须列出以下用户和注册日期:|用户名| registrationDate |user1 | | 05/30/2010 || user2 | 06/11/2009 |

下面是我为这一步写的实现代码:

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' List返回的用户列表不会始终以相同的顺序排列。

您使用的是哪个框架?如果您正在使用Jasmine框架,那么您可以在for循环的中添加expect语句,以验证实际和期望的值,如以下代码片段-

expect(actual_value).toEqual(expected_value);

如果你没有使用任何框架,那么你可以尝试使用普通javascript验证值,就像下面的代码片段-

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

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

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

要存储来自中继器函数的用户列表,请查看MAP函数可在量角器。它将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
    });