Cypress 迭代表行获取单元格中的特定元素

juv*_*uve 5 javascript cypress

我试图迭代表行并获取包含特定值的每一行,但它对我不起作用。我用来.each()迭代行,并在其中的.within()每个行上$el使用,cy.get('td').eq(1).contains('hello')但我得到断言错误:

超时重试:期望在元素中找到内容:'hello':<td>但从未找到。

当我 console.log 时cy.get('td').eq(1),它会在每一行中生成所需的单元格并且测试通过,所以我不明白为什么链接.contains()不起作用......

it('get element in table', () => {
  cy.visit('http://localhost:3000/');
  cy.get('tbody tr').each(($el) => {
    cy.wrap($el).within(() => {
      cy.get('td').eq(1).contains('hello') // contains() doesn't work
    })
  })
});
Run Code Online (Sandbox Code Playgroud)
<table>
  <thead>
    <tr>
      <th>Month</th>
      <th>Savings</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>hello</td>
      <td>$80</td>
    </tr>
     <tr>
      <td>$10</td>
      <td>hello</td>
    </tr>
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

Ami*_*mit 3

should('have.text', text)应该管用

cy.get('td').eq(1).should('have.text', 'hello')
Run Code Online (Sandbox Code Playgroud)

如果文本周围有空格,请使用contain.text

cy.get('td').eq(1).should('contain.text', 'hello')
Run Code Online (Sandbox Code Playgroud)