Reputation: 11
How can I grab a text from a Webpage in Cypress using Typescript.
Thank you!
Upvotes: 1
Views: 222
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