How to get div 'text' value in Cypress test using jquery

soccerway picture soccerway · Sep 25, 2018 · Viewed 31.2k times · Source

Using Jquery in Cypress.io test, how to get the div 'text' value called 'Wildness' from the below html tag. I have tried below in my Cypress test, but it is throwing undefined in console.

const $divText = Cypress.$('.ibxudA .WildnessText-kRKTej').text()
         cy.wrap($divText)
           .should("eq", "Wildness")

Answer

Maccurt picture Maccurt · Sep 27, 2018

I might try this:

cy.get(".ibxudA").find('.WildnessText-kRKTej').invoke('text').then((text) => {
    expect(text.trim()).equal('Wildness')
});

or

 cy.get(".ibxudA").find('.WildnessText-kRKTej').should('have.text',"Wildness")

This might be a similar question: How do you check the equality of the inner text of a element using cypress?