angular - Angular 6量角器返回未定义的字符串,即使它返回承诺
问题描述
[![我有一个表格,其中有多行和 5 列,我正在尝试查找电子邮件 ID,然后在电子邮件可用时返回状态。这是代码 文件 A
async function checkInviteUserStatus(xyz) {
const tablecontent = this.getTableContent();
const rows = tablecontent.all(by.tagName('tr'));
let ind: number;
return new Promise(_ => {
rows.each((tr, index) => {
const cols = tr.all(by.tagName('td'));
let rowindex = false;
cols.each((td, i) => {
td.getText().then(text => {
if (text === emailId) {
rowindex = true;
ind = i;
}
if (rowindex) {
if (i === ind + 1) {
console.log('Entering worked');
console.log('status entered test' + text); // prints correct value here
resolve(text);
}
}
});
});
});
});
}
await `enter code here`
myProfile.checkInviteUserStatus('test12.535116665229136@test.com').then(t => {
console.log('testing ' + t);
});
解决方案
您使用了一种复杂的方法。您可以将所有td的文本放入一个数组中,然后从数组中找到特定电子邮件的索引,如果数组中存在特定电子邮件,则返回数组中的下一个值。
async function checkInviteUserStatus(email) {
const tablecontent = this.getTableContent();
// read all text of table tds into One-dimensional Array
let txts = await tablecontent.all(by.css('tr > td')).getText();
// find the index of email insides the Array
let index = txts.indexOf(email);
// if email exists in the Array, then return the txts[index + 1]
// which is the value of status
return index > -1 ? txts[index + 1]: 'Not find such email in table';
}
Email
或者,您可以通过使用表中已知的列索引来节省迭代所有 td 的时间Status
。根据您给出的代码,我可以想象您的表格至少包括两列,如下所示:
async function checkInviteUserStatus(email) {
const tablecontent = this.getTableContent();
const util = require('util');
// assume column index of Email is 1
// you should change to your value
let colIndexEmail = 1;
// read all text of the Email column into One-dimensional Array
let css = util.format('tr > td:nth-child(%s)', colIndexEmail + 1);
let emails = await tablecontent.all(by.css(css).getText();
// find the index of email insides the Array
// the index hints the table row index that includes the email
let rowIndex = emails.indexOf(email);
// if email exists in the Array, then find the next column on the same row
css = util.format('tr:nth-child(%s) > td:nth-child(%s)', rowIndex + 1, colIndexEmail + 1 + 1);
return rowIndex > -1 ? await tablecontent.element(by.css(css)).getText(): 'Not find such email in table';
}
myProfile.checkInviteUserStatus('test12.535116665229136@test.com').then(t => {
console.log('testing ' + t);
});
推荐阅读
- db2 - DB2 临时表 - 在 DB2 Z OS 中检索插入的数据
- python - 我可以使用 pd.DataFrame() 定义多个数据框吗?
- javascript - 在 javascript 上制作“Atari Breakout”类型的游戏。想知道如何在无需手动操作的情况下创建它们的想法
- flutter - 3 个小部件位置:图像顶部和图像加文本底部全宽
- flutter - 在测试和尝试设计付费墙时,Revenuecat 产品为空
- r - 如何创建以函数参数命名的对象并将它们全部绑定?
- amazon-web-services - 如何在 azure 管道中部署 Elastic beanstalk
- git - 由于缓存凭据错误,无法将代码推送到 Azure DevOps
- django - 酒店预订系统的按日期价格数据库设计(存储每个日期与在运行时计算)
- html - HTML 中的 meta 运行硬刷新