[英]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
的每个元素时遇到了两种结果之一:
undefined
orundefined
或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.