PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 3, 2022

[FIXED] How to get data displaying in a table <td> in Cypress.io test?

 August 03, 2022     cypress, html-table, javascript     No comments   

Issue

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


Solution

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...


Answered By - Joshua Wade
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing