简体   繁体   中英

Angular 6 protractor returns undefined for string even it return promise

[![I have a table which has multiple rows and 5 columns each, I am trying to look for an email ID and then return the status if email is available. here is the code FILE 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);
});

You used a complex approach. You can get text of all td into an Array, then find the index of the specific email from the Array, and return the next value in the Array if the specific email exists in the Array.

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';

}

Or you can save the time to iterate all td by use known column index of Email and Status in your table. From you given code, I can image your table includes two columns at least as following:

在此处输入图片说明

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM