Avijeet Deb
Avijeet Deb

Reputation: 11

Grabbing a text from a web page in Cypress

How can I grab a text from a Webpage in Cypress using Typescript.

Thank you!

Upvotes: 1

Views: 222

Answers (1)

Alapan Das
Alapan Das

Reputation: 18626

If you want assert an exact text you can use:

cy.get('selector').should('have.text', 'some text')

If you want assert an partial text you can use:

cy.get('selector').should('include.text', 'some text')

If you want to save the text in a variable and use it later you can use aliases.

cy.get('selector').invoke('text').as('text1')
//Some other commands
cy.get('@text1').then((text) => {
  cy.log(text) //Prints the text
})

If you want to Compare two texts, you can:

cy.get('selector').invoke('text').then((text1) => {
  cy.get('selector').invoke('text').then((text2) => {
    expect(text1).to.equal(text2)
  })
})

Upvotes: 2

Related Questions