从量角器中的地图创建数组

Create array from map in Protractor

我有这个HTML

<tr ng-repeat="row in entityL">
  <td>{{row.Name}}</td>
  <td>{{row.Surname}}</td>
  <td>{{row.Initials}}</td>
  <td>{{row.RolDivisionList.Name}}</td>
</tr>

我想在 Protractor 中获取包含所有这些值的数组,我必须访问第一个。

我尝试在页面对象中使用这个 Protractor 函数

this.getAllUsers = function() {
  var userList = element.all(by.repeater('row in entityL'));

  return userList.map(function(tr) {    
    return {
      name: tr.element(by.binding('row.Name')).getText(),
      surname: tr.element(by.binding('row.Surname')).getText(),
      initials: tr.element(by.binding('row.Initials')).getText(),
      rolDivision: tr.element(by.binding('row.RolDivisionList.Name')).getText()
    };
  });
};

我得到了这样的结果

[ 
   { name : 'Name1', surname : 'Surname1', initials : 'Initials1', rolDivision : 'Rol1' }, 
   { name : 'Name2', surname : 'Surname2', initials : 'Initials2', rolDivision : 'Rol2' } 
]

但我无法访问它的值,我尝试了 array[0]array[0]['name']array[0].namearray.get(0)... 但没有任何效果。我还尝试用 []return 包裹在 map 中,但也没有用。

事实是我只想要第一个用户,但我无法实现,必须return全部,所以如果你知道怎么做to return 刚好第一个来测试那也太好了。

这是测试代码(只是it):

iit('should return the first user', function() {    
  expect(userListPage.getAllUsers()[0]['name']).toBe('Name1');
});

新实验

我用以前的代码尝试了一些实验,试图了解 Protractor 的工作原理,这就是我得到的结果。

我将在页面对象中使用以前的 HTML 和新的 Protractor 函数

var UserListPage = function() {
  this.userList = element.all(by.repeater('row in entityL'));

  this.getFirstUser = function() {
    return {
      name: element(by.binding('row.Name')).getText(),
      surname: element(by.binding('row.Surname')).getText(),
      initials: element(by.binding('row.Initials')).getText(),
      rolDivision: element(by.binding('row.RolDivisionList.Name')).getText()
    };
  };

  this.getUserData = function (tr) {
    return {
      name: tr.element(by.binding('row.Name')).getText(),
      surname: tr.element(by.binding('row.Surname')).getText(),
      initials: tr.element(by.binding('row.Initials')).getText(),
      rolDivision: tr.element(by.binding('row.RolDivisionList.Name')).getText()
    };
  };

  this.getAllUsers = function() {    
    return this.userList.map(function(tr) {      
      return {
        name: tr.element(by.binding('row.Name')).getText(),
        surname: tr.element(by.binding('row.Surname')).getText(),
        initials: tr.element(by.binding('row.Initials')).getText(),
        rolDivision: tr.element(by.binding('row.RolDivisionList.Name')).getText()
      };
    });
  };
};

module.exports = UserListPage;

现在是包含所有实验的测试块

it('should test things', function() {
  userListPage.getAllUsers().then(function(array) {
    //First user array
    expect(array[0]).toEqual("");
    //Name of the first user
    expect(array[0].name).toEqual("");
  });

  //Array with all users
  expect(userListPage.getAllUsers()).toEqual("");

  //Name of the first user
  expect(userListPage.getAllUsers().name).toEqual("");

  //undefined
  expect(userListPage.getAllUsers()[0]).toEqual("");

  //Cannot read property 'name' of undefined
  expect(userListPage.getAllUsers()[0].name).toEqual("");



  //.then is not a function
  userListPage.getFirstUser().then(function(array) {});

  //ERROR Process out of memory
  expect(userListPage.getFirstUser()).toEqual("");

  //Name of the first user
  expect(userListPage.getFirstUser().name).toEqual("");

  //undefined
  expect(userListPage.getFirstUser()[0]).toEqual("");

  //Cannot read property 'name' of undefined
  expect(userListPage.getFirstUser()[0].name).toEqual("");



  //.then is not a function
  userListPage.getUserData(userListPage.userList.first()).then(function(array){});

  //ERROR Process out of memory
  expect(userListPage.getUserData(userListPage.userList.first())).toEqual("");

  //Name of the first user
  expect(userListPage.getUserData(userListPage.userList.first()).name).toEqual("");

  //undefined
  expect(userListPage.getUserData(userListPage.userList.first())[0]).toEqual("");

  //Cannot read property 'name' of undefined
  expect(userListPage.getUserData(userListPage.userList.first())[0].name).toEqual("");

});

我觉得这个 expect(userListPage.getFirstUser()).toEqual(""); 停止 Protractor 时出现错误,而这个 expect(userListPage.getFirstUser().name).toEqual(""); return 是正确的值,这对我来说似乎很奇怪。

有人能解释一下为什么会这样吗?

PD:我应该把所有这些都放在一个新问题中吗?

您应该可以使用 map 来执行此操作,但由于您只对第一项感兴趣,因此您可以将 allfirst 或 [=15= 结合使用]:

  function getUserData (tr) {
    return {
      name: tr.element(by.binding('row.Name')).getText(),
      surname: tr.element(by.binding('row.Surname')).getText(),
      initials: tr.element(by.binding('row.Initials')).getText(),
      rolDivision: tr.element(by.binding('row.RolDivisionList.Name')).getText()
    };
  }

  var userList = element.all(by.repeater('row in entityL'));
  var item = getUserData(userList.first());

  expect(item.name).toEqual('Name1');

或者,您可以 then 关闭您的 map 以解析第一项的名称:

var name = element.all(by.repeater('row in entityL'))
  .map(getUserData)
  .then(function (mappedArray) {
    return mappedArray[0].name
  });

expect(name).toEqual('Name1');

如果DOM中没有具有相同绑定的元素,则无需使用.map()函数即可获取。量角器具有 ng-binding 的内置元素定位器。方法如下 -

element(by.binding('row.Name'));

但是,如果有更多 html 个具有相同 ng-binding 属性的元素,则使用 E​​lementArrayFinder 获取元素 -

element.all(by.binding('row.Name')).first();

另一种方法是使用其父元素获取元素 -

element.all(by.repeater('row in entityL')).element(by.binding('row.Name')); //Get only the first element with that binding

关于你的研究新问题,你的 expect(userListPage.getFirstUser()).toEqual(""); returns 是一个错误,因为函数 getFirstUser() 正在返回一个对象,你必须获得对象的键才能访问键的值,在您的情况下,您应该使用 .name 来获取用户名。所以 expect(userListPage.getFirstUser().name).toEqual(""); returns 一个正确的答案。 More on objects in Javascript.

希望对您有所帮助。