简体   繁体   English

如何使用 protractor 页面 object model 设计和承诺创建和返回对象数组

[英]How to create and return an array of objects using protractor page object model design and promises

I'm creating a page object model and one of the properties is all the users from a table.我正在创建一个页面 object model ,其中一个属性是表中的所有用户。 The table has a few columns so I'd like to parse that table and create a user object for each row, then return that set to then be used in tests.该表有几列,所以我想解析该表并为每一行创建一个用户 object,然后返回该集以用于测试。 So far, this is what that property of my page object looks like:到目前为止,我的页面 object 的属性如下所示:

users: {
    get: function() {
        let userRecords = [];
        var users = element.all(by.repeater('user in users')).each(function(tr, index) {
            let user = {
                name:   tr.element(by.css('td:nth-child(2)')).getText().then(text => {return text;}),
                role:   tr.element(by.css('td:nth-child(3)')).getText().then(text => {expect(text).toBeTruthy();}),
                status: tr.element(by.css('td:nth-child(4)')).getText().then(text => {expect(text).toBeTruthy();}),
                //actionsButton: tr.element(by.css('btn btn-default'))
            };
            userRecords += user;
        }).then(userRecords => {return userRecords});
        
        return userRecords;
    }
},

Through trial and error I encounter one of two outcomes when just trying to print to screen each element of userRecords :通过反复试验,我在尝试打印以筛选userRecords的每个元素时遇到了两种结果之一:

  1. each element prints as undefined or每个元素打印为undefined
  2. userRecords is not defined . userRecords is not defined

Just to reiterate, I'm simply trying to build an array that holds each user as an object to then be able to iterate / filter on that set in my tests.重申一下,我只是想构建一个数组,将每个用户保存为 object,然后能够在我的测试中对该集合进行迭代/过滤。

Given the approach I'm taking, what's the ideal way to construct this user array and resolve the promises?鉴于我正在采用的方法,构建此用户数组并解决承诺的理想方法是什么?

Edit: it's worth noting that if I do a console.log() within each of the getText().then() statements, it does print the correct data from the table.编辑:值得注意的是,如果我在每个getText().then()语句中执行console.log() ,它确实会从表中打印正确的数据。 So, I do know that it's reading the table correctly.所以,我知道它正在正确读取表格。

I'd go with a method that returns json, and would make it async我会 go 使用返回 json 的方法,并使其异步

users: async function() {
  let userRecords = [];
  var users = element.all(by.repeater('user in users'));
  for (let i = 0; i < await users.count(); i++) {
    let tr = users.get(i);
    let user = {
      name: await tr.element(by.css('td:nth-child(2)')).getText(),
      role: await tr.element(by.css('td:nth-child(3)')).getText(),
      status: await tr.element(by.css('td:nth-child(4)')).getText()
    };

    userRecords.push()
  }
        
  return userRecords;
},

and then use:然后使用:

console.log(
  JSON.stringify(
    await constructorName.users()
  )
)

should be as simple as that.应该就这么简单。 Note, I didn't test the code, but I did use the approach in my experience.请注意,我没有测试代码,但根据我的经验,我确实使用了这种方法。 So it may require some minor modifications所以它可能需要一些小的修改

In general, try to avoid .then - async/await is easier to use, .each - go with for loop instead.一般来说,尽量避免使用.then - async/await更容易使用, .each - go 用for循环代替。 Also userRecords += user;还有userRecords += user; doesn't do what you think it does (though I may be wrong here)没有做你认为的事情(尽管我在这里可能错了)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 量角器:跟进另一个有关如何处理在页面对象模型中返回数组的定位符的线程 - protractor: follow up to another thread regarding how to handle locators that return array in page object model 页面对象设计模式量角器 - page object design pattern protractor 如何使用量角器返回数组中的getText()值 - How to return the getText() values in array using protractor 如何在对象数组上异步查询 API,然后正确改变每个对象? (正确使用承诺) - How do I asynchronously query an API on an array of objects and then mutate each object correctly? (Using promises correctly) 在使用页面对象模型的量角器测试中需要断言表单字段 - Asserting form fields are required in Protractor test using page object model 将页面对象模型与量角器配合使用时,Promise不会得到解决 - Promise not getting resolved when using Page Object Model with Protractor 如何在使用element.all的量角器端对端测试中从javascript的承诺中返回值 - How to return values from promises of javascript in protractor end to end testing on using element.all 如何在Javascript中使用Protractor和Page Object Model正确使用getText() - How to accurately use getText() with Protractor and Page Object Model in Javascript 量角器:使用承诺 - Protractor: using promises 从量角器页面对象返回一个对象 - Return an object from Protractor page object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM