How can I use jQuery in a Cypress.io test to retrieve the text value ‘Wildness’ from the following HTML tag? I attempted the code below in my Cypress test, but it’s logging ‘undefined’ in the console.
const $divText = Cypress.$(‘.ibxudA .WildnessText-kRKTej’).text()
cy.wrap($divText)
.should(“eq”, “Wildness”)
Hi Priyanka,
I hope this message finds you well. Regarding your inquiry about the provided code snippet, here’s a detailed explanation Try the below code snippet
cy.get(“.ibxudA”).find(‘.WildnessText-kRKTej’).should(‘have.text’,“Wildness”)
or
cy.get(“.ibxudA”).find(‘.WildnessText-kRKTej’).invoke(‘text’).then((text) => {
expect(text.trim()).equal(‘Wildness’)
});
If you have any further questions or need additional clarification, feel free to ask.
Hi Pryanka,
I appreciate your interest in optimizing your Cypress test scripts. Regarding your suggestion to use the ‘contains()’ API, it’s indeed a valuable recommendation for efficiently selecting HTML elements in Cypress.
I would suggest you use inbuilt cypress API ‘contains()’ like this:
cy.contains(‘Wildness’)
It will select the whole HTML tag for you.
Learn more detailedly about Cypress by following this guide : Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices