首页 > 解决方案 > 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);
});

标签: angularprotractorangular6ui-automation

解决方案


您使用了一种复杂的方法。您可以将所有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);
});

推荐阅读