Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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")

<div class="sc-bMvGRv_onetwo">
<div>
<div class="abLeftSection">
<div class="abNewBtn-fTLJBK">
<button class="ifAKCX ohpWT" type="button">New</button>
</div>
<div class="kpLvEV" style="">
<div class="cWzXYZ">
<div class="OgGUG">
<div class="jsnnAD">
<svg class="dFvKsA"></svg>
</div>
</div>
<div class="ibxudA">First</div>
</div>
<div class="kpLvEV" style="">
<div class="bGADLM"><div class="OgGUG">
<div class="jsnnAD">
<svg class="dFvKsA"></svg>
</div>
</div>
<div class="ibxudA">
<div class="WildnessText-kRKTej" title="Wildness">Wildness</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
like image 886
soccerway Avatar asked Sep 25 '18 05:09

soccerway


2 Answers

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?

like image 199
Maccurt Avatar answered Sep 28 '22 21:09

Maccurt


cy.get("WildnessText-kRKTej").then(function($elem) {
     cy.log($elem.text())
})
like image 37
Singhak Avatar answered Sep 28 '22 22:09

Singhak