How to get the value of a text with Cypress?

Asked

Viewed 728 times

0

I need to capture the text of an element with Cypress. I am using:

const test = cy.get('elemento').invoke('text')
cy.log(test)

The log shows: Object{5}.

What do I have to do to get this log? Because I would like to compare the value of this variable with another value later.

NOTE: I also can’t do the comparison below:

expect(test).to.equal(test)

1 answer

0

Try to use it that way:

cy.get('elemento').invoke('text').then(($value) => {
  cy.log($value)
})

If you want to compare 2 values:

cy.get('elemento_1').invoke('text').then(($value_1) => {
  cy.get('elemento_2').invoke('text').then(($value_2) => { 
    expect(value_1).to.eq(value_2)
  })
})

Any questions, please follow the link in the Cypress documentation: Variables and Aliases

Browser other questions tagged

You are not signed in. Login or sign up in order to post.