I'm trying to iterate through table rows and get each row which includes a specific value,
but it doesn't work for me.
I'm using .each()
to iterate the rows and .within()
on each $el
,
inside that, I use cy.get('td').eq(1).contains('hello')
but I the get assertion error:
Timed out retrying: Expected to find content: 'hello' within the element:
<td>
but never did.
when I console.log cy.get('td').eq(1)
it yields the desired cell in each row and the test passes, so I don't understand why chaining .contains()
doesn't work...
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
})
})
});
<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>
should('have.text', text)
should work
cy.get('td').eq(1).should('have.text', 'hello')
If there's whitespace around text, use contain.text
cy.get('td').eq(1).should('contain.text', 'hello')