如何断言 table 中是否存在文本

How to assert whether a text is present in table

我是量角器的新手,想断言新添加的行是否包含我添加的文本。

我还想断言特定文本是否属于 header 部分。

我已经尝试了一些,但我的代码失败了。

下面是我的代码

 var row=element.all(by.repeater('dataRow in displayedCollection'));
   var col=row.all(by.tagName('td'));
   col.each(function(item)
   {
       item.getText().then(function(text)
       {
           

       })
   })       
    
   

})

下面是HTML代码

下面是UI

Sergey 是正确的,使用 async/await 会容易得多,但我将如何使用您的示例。

假设新添加的行始终是 table 中的最后一行,您可以这样做:

const rows = element.all(by.repeater('dataRow in displayedCollection'));
const relevantRow = rows.last();

relevantRow.getText().then(test => {
  expect(text).toContain(mySampletext);
});

如果新行不总是最后一行,那么查找它的方式会有所不同。例如,如果每次添加一行时 table 都按字母顺序排序,那么您需要过滤行列表以找到您要查找的行,然后检查文本是否与您期望的相匹配成为。

您实际上不需要获取每个 td 元素,除非您想验证每列中的文本是否正确。在那种情况下,您可能会这样做:

const expectedText = ['someText', 'in order', 'by', 'columns'];
const rows = element.all(by.repeater('dataRow in displayedCollection'));
const relevantRow = rows.last();

const columns = relevantRow.$$('td');
let coumnTextMatches = true;
columns.each((col, index) => {
  col.getText().then(text => {
    columntextMatches = expectedText[index] === text;
  });
});

expect(columntextMatches).toEqual(true);

这可能完全准确,也可能不完全准确。我只是在没有测试的情况下随心所欲地完成了它,但它应该非常接近类似的东西。

对于 header 来说,情况类似。

  • 获取所有header个元素
  • 遍历每一个并检查文本是否符合您的预期