Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从量角器中的映射创建数组_Javascript_Angularjs_Selenium_Jasmine_Protractor - Fatal编程技术网

Javascript 从量角器中的映射创建数组

Javascript 从量角器中的映射创建数组,javascript,angularjs,selenium,jasmine,protractor,Javascript,Angularjs,Selenium,Jasmine,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> 我得到的结果是这样的 [ { name

我有这个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>
我得到的结果是这样的

[ 
   { name : 'Name1', surname : 'Surname1', initials : 'Initials1', rolDivision : 'Rol1' }, 
   { name : 'Name2', surname : 'Surname2', initials : 'Initials2', rolDivision : 'Rol2' } 
]
但是我无法访问它的值,我尝试了
array[0]
array[0]['name']]
array[0]。name
array.get(0)
。。。什么都不管用。我还试图用
[]
返回
包装在
映射
中,但也没有成功

事实上,我只想要第一个用户,但我无法实现它,必须返回所有用户,因此如果您知道如何仅返回第一个用户到测试,那也太好了

这是测试代码(只是
):

新实验 我用前面的代码做了一些实验,试图了解量角器是如何工作的,这就是我得到的

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

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()
    };
  });
};
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(“”)停止带有错误的量角器,此
预期(userListPage.getFirstUser().name).toEqual(“”)返回正确的值

有人能解释一下为什么会发生这种事情吗


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

你应该可以用
map
来做这件事,但是因为你只对第一项感兴趣,你可以将
all
first
get
结合使用:

  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');
或者,您可以
然后
关闭
映射
,以解析为第一项的名称:

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绑定
。这是怎么做的-

element(by.binding('row.Name'));
但是,如果有更多具有相同ng绑定属性的html元素,则使用ElementArrayFinder获取该元素-

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(“”)
返回错误,因为函数
getFirstUser()
正在返回一个对象,您必须获取该对象的键才能访问该键的值,在您的情况下,您应该使用
.name
获取用户的名称。所以
expect(userListPage.getFirstUser().name).toEqual(“”)返回正确答案


希望有帮助。

您是否尝试使用
element.all(by.binding('row.Name')).first()获取它
?@GirishSortur也可以,使用
元素(by.binding('row.Name'))更好。
。谢谢。当我只调用名称时,第一个选项起作用,但是如果我尝试此
expect(item.name+“”+item.姓氏)。toEqual('Name1-names1')
它将
Expected'[object object][object object]'返回为'Name1-names1'。
我尝试添加
toString()
但它不起作用。这是因为这两个属性实际上都是承诺,所以您尝试将两个承诺对象添加到一起。如果您需要获得这两个结果并将它们相加,则必须解决它们。感谢您的回答(Nick;)但是为什么脚本中的第一个
expect
将对象显示为数组,而其他的则不能?您指的是哪一个
expect
?你能解释一下错误在哪里吗?这是
expect(数组[0])。toEqual(“”)
userListPage.getAllUsers()内。然后在
it
的第一行中。这也是一个对象,可以进行比较,当结果显示在控制台上时,它会显示为字符串,因为
map()
函数返回一个数组。但是您的其余函数没有实现
map()
方法,因此您不会为它们返回数组,而是手动返回的对象。
element.all(by.repeater('row in entityL')).element(by.binding('row.Name')); //Get only the first element with that binding