Uploaded June 2023 | Updated September 2026, 1 week ago
Cypress retries querying commands like cy.contains and cy.get. You can attach assertions to the command to retry the command until the assertion passes: cy.get('#output').should('have.text', '42') You can even have ".should(callback)" to write your own logic to check the current subject element. For example, you could store all values you see during retries and then later validate the seen values:
cy.log('**shows 5 messages**')
const messages = new Set()
cy.contains('button', 'Start').click()
cy.get('#output').should(function ($el) {
const text = $el.text()
messages.add(text)
expect(text).to.equal('42')
})
cy.wrap(messages).then(Array.from).should('have.length.gte', 5)
Find this recipe at glebbahmutov.com/cypress-examples
Cypress retries querying commands like cy.contains and cy.get. You can attach assertions to the command to retry the command until the assertion passes: cy.get('#output').should('have.text', '42') You can even have ".should(callback)" to write your own logic to check the current subject element. For example, you could store all values you see during retries and then later validate the seen values:
cy.log('**shows 5 messages**')
const messages = new Set()
cy.contains('button', 'Start').click()
cy.get('#output').should(function ($el) {
const text = $el.text()
messages.add(text)
expect(text).to.equal('42')
})
cy.wrap(messages).then(Array.from).should('have.length.gte', 5)
Find this recipe at glebbahmutov.com/cypress-examples









![Escape Regular Expression Text When Using cy.contains Command
This video shows how to escape special characters like $, [], and ^ when creating regular expressions to use with cy.contains command. You can escape the characters yourself or use the Lodash method Cypress._.escapeRegExp bundled with Cypress to do it for you
const suffix = $/day
const regex = new RegExp(Cypress._.escapeRegExp(suffix))
cy.contains(regex).should(have.id, rate)
Find the full recipe at https://glebbahmutov.com/cypress-examples Escape Regular Expression Text When Using cy.contains Command](https://i.ytimg.com/vi/hMzqiSUVGwA/mqdefault.jpg)
