首页 > 解决方案 > 柏树 | 检查搜索关键字是否与显示的 trs 的任何 td 的值匹配

问题描述

我正在测试一种搜索功能,该功能根据用户的名字、姓氏、角色或电子邮件过滤输入的字符。

我有一个tablewith multiple tr,每个tr都有多个td.

我想断言返回的行包含td与输入的关键字匹配的任何值。

我创建了以下帮助函数,但cy.get('td').contains(searchKeyword);导致浏览器挂断。任何想法可能是这里的解决方案。

 assertCorrectFilterResults(searchKeyword: string) {
            this.elements.tableBody().find("tr").then(rows => {
                rows.toArray().forEach(row => {
                    cy.get('td').contains(searchKeyword);
                })
            });
        };

我的助手受到此处提到的解决方案的启发How to get the total number of Rows in a table | 柏

表结构 在此处输入图像描述

标签: datatablescypress

解决方案


如果您直接想查看td元素,则可以直接遍历它们。

为了与searchKeyword

cy.get('tbody.MuiTableBody-root tr td').each(($ele) => {
  if($ele.text().trim() == searchKeyword){
    expect($ele.text().trim()).to.equal(searchKeyword)  //Assertion for exact text
  }
})

对于部分匹配searchKeyword

cy.get('tbody.MuiTableBody-root tr td').each(($ele) => {
  if($ele.text().trim().includes(searchKeyword)){
    expect($ele.text().trim()).to.include(searchKeyword)  //Assertion for partial text
  }
})

推荐阅读