user9847788
user9847788

Reputation: 2445

How to loop through each row within a table in a Protractor test?

I am trying to write a Protractor test that prints the text within the 4th <td> of each row in a table.

Currently, I'm able to print the text contained in the first row using this code:

element(by.css('td:nth-child(4)')).getText().then((text) => {
    console.log('Text', text);
});

I created the below function to try loop through each row within the table:

function checkPrices() {
    element.all(by.tagName("tr")).each((item) => {
        item.element(by.css('td:nth-child(4)')).getText().then((text) => {
            console.log('Text', text);
        });
    });
}

But when I run this, I receive this error message:

Failed: No element found using locator: By(css selector, td:nth-child(4))

Can someone please tell me where I'm going wrong & how I can write this loop correctly?

Upvotes: 0

Views: 109

Answers (1)

Andrei
Andrei

Reputation: 12021

maybe some row doesn't contain 4 tds, that is why error is thrown. why won't you just get all emenents and get their text.

expect(element.all(by.css('td:nth-child(4)')).getText()).toEqual(['text1', 'text2', 'text3'])

Upvotes: 1

Related Questions