17

In a Cypress.io test, while checking for the 'data' displayed in a table after applying the filter, it throws "CypressError: Timed out retrying: Cannot read property 'eq' of undefined". Can someone please advise how to fix the problem in below test? Table HTML image added below.

describe('Filter Test', function() {
    it.only('Check if the records are filtered successfully', function() {
        cy.visit('http://www.seleniumeasy.com/test/table-search-filter-demo.html')          
        cy.get('button').contains('Filter').click()
        cy.get('input[placeholder="Username"]').type('jacobs')  
        cy.get('table').should(($tr) => {
        const $tds = $tr.find('td') // find all the tds
        expect($tds.cells.eq(0)).to.contain('jacobs')   
        })

    })      

})

enter image description here

enter image description here

3
  • Can you console.log($tds) to see if it has cells object inside ?
    – DarknessZX
    Aug 16, 2018 at 10:43
  • @DarknessZX : Yes I could see the table object inside
    – soccerway
    Aug 16, 2018 at 10:49
  • @DarknessZX: Added the console image for reference
    – soccerway
    Aug 16, 2018 at 10:53

1 Answer 1

40

There are multiple ways to do this, but the contains() function is by far the simplest in this case:

cy.get('table').contains('td', 'jacobs');

This will get the table element and assert that it contains a td tag with the text jacobs.


It's worth noting that contains() also acts as a selector, and in typical Cypress fashion you can continue chaining off it, like so:

cy.get('table').contains('td', 'jacobs').should('be.visible');

cy.get('table').contains('td', 'jacobs').then(elem => {
    // Do something with this specific element...
});

// etc...
3
  • Will try that today evening and reply
    – soccerway
    Aug 16, 2018 at 21:55
  • @Joshua Wade - Hi Joshua, is there any way dynamically filter non zeros from a td ?
    – Prany
    Sep 23, 2019 at 10:10
  • Hi @Prany, try this: cy.get('.my-table').get('td').each(elem => {}); - Cypress will loop through each td element and run the provided lambda function with the raw Javascript element passed into elem. Now, in the curly braces, you can just do this (or similar, note this is untested): if (parseFloat(elem.textContent) !== 0) {/*element does *not* contain zero*/} else {/*element contains zero*/} Sep 23, 2019 at 13:24

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.