Friday, February 23, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  141] [ 4]  / answers: 1 / hits: 11276  / 4 Years ago, wed, april 22, 2020, 12:00:00

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>




More From » cypress

 Answers
6

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')

[#4069] Monday, April 20, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
martina

Total Points: 101
Total Questions: 103
Total Answers: 111

Location: Seychelles
Member since Mon, Jun 28, 2021
3 Years ago
;