1

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

Thank you!

  • 1
    Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Oct 18 '21 at 08:25

1 Answers1

2

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)
  })
})
Alapan Das
  • 17,144
  • 3
  • 29
  • 52