0

i am using the following code to get a text value from a < td > :

 let serviceName;
    cy.get('tr').eq(1).then(row => {
        cy.wrap(row).find('td').eq(1).invoke('prop', 'innerText').then(val => { serviceName = val });
    })

its working fine on some of pages and not on some others with the same table type/class/etc.. front end is built with Angular

using the same code like this also fails :

let serviceName;
    cy.get('tr').eq(1).find('td').eq(1).invoke('prop', 'innerText').then(val => { serviceName = val });
    })

1 Answers1

0

You can use aliases for this.

cy.get('tr')
  .eq(1)
  .find('td', {timeout: 5000})
  .eq(1)
  .should('be.visible')
  .invoke('prop', 'innerText')
  .as('text')

cy.get('@text').then((text) => {
  //access text here
})
Alapan Das
  • 17,144
  • 3
  • 29
  • 52
  • Yes i can thank you , but what i really want to know is why my previous code works sometimes and sometimes not i've read the docs and understood how cypress works as sync/async but why in some pages its ok to use .then() like that and some not – Wessam Juby Feb 08 '22 at 11:59
  • What is the error that you are getting ? – Alapan Das Feb 08 '22 at 11:59
  • no error, just the variable (let serviceName) stay null or empty as its never assigned – Wessam Juby Feb 08 '22 at 12:01
  • May be add an assertion like visible like I did in my answer. – Alapan Das Feb 08 '22 at 12:07
  • @AlapanDas I know this is an older post, but why does adding an assertion make a difference? I'm having issues with getting the prop value as well and it only seems to work when I add an assertion. Which doesn't make sense to me. Can you explain why that is please? – rmnrdi Aug 01 '23 at 11:39