Search code examples
automationcypress

Cypress - how to get a dynamic value in table


I'm trying to get the dynamic value from a table that is currently '62' right now but will change. I tried:

 cy.get('tbody>tr').eq(0)
   .find("td").eq(1)
   .then(text => {
    const rowText = text;
    cy.log(rowText)

But it's outputing

<td.MuiTableCell-root.MuiTableCell-body.MuiTableCell-alignCenter.MuiTableCell-sizeMedium.css-1cjito2> 

instead of 62

enter image description here


Solution

  • The variable yielded by your .find('td').eq(1) is not a text variable, but a JQuery Element. You'll need to abstract that value. You can do that by cy.invoke(), or using the JQuery .text() function.

    cy.get('tbody>tr').eq(0)
       .find("td").eq(1)
       .invoke('text')
       .then(text => {
         cy.log(text);
       });
    
    cy.get('tbody>tr').eq(0)
       .find("td").eq(1)
       .then($el => {
         cy.log($el.text());
       });