如何在 Protractor 测试中遍历 table 中的每一行?

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

我正在尝试编写一个 Protractor 测试,它在 table.

中的每一行的第 4 <td> 内打印文本

目前,我可以使用以下代码打印第一行中包含的文本:

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

我创建了以下函数来尝试遍历 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);
        });
    });
}

但是当我运行这个的时候,我收到这个错误信息:

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

谁能告诉我哪里出错了以及如何正确编写这个循环?

可能某些行不包含 4 个 tds,这就是引发错误的原因。你为什么不直接获取所有元素并获取他们的文本。

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