Manish B
Manish B

Reputation: 429

How to assert a specific text in cypress

Please see the below code.
I am trying to assert value 2 but my code is not working.

enter image description here

Upvotes: 0

Views: 1966

Answers (3)

Fody
Fody

Reputation: 31924

The bold tags <b> are interfering with your text evaluation. It's actually HTML inside

cy.get('#_evidon_message').then($el => { 
  const html = $el.html()
  console.log(html)         // yields same as screenshot
}) 

To access the inner <b> use additional commands, for example

cy.get('#_evidon_message')
  .find('b')
  .eq(2)
  .should('contain', '2')

Upvotes: 0

jjhelguero
jjhelguero

Reputation: 2555

Based on the information you provided, it isn't clear if you want to check the entire text within the div or just for the number before partners.

If you only want to validate there is a 2 within the entire string, then:

cy.get('#_evidon_message')
  .invoke('text')
  .should('include.text', '2')

You can use also use regex on the string as well.

cy.get('#_evidon_message')
  .invoke('text')
  .then(text => {
     const regexMatcher = /(\d+) months with (?<numPartners>(\d+)) partners/i
     const numPartners = text.match(regexMatcher)?.group?.numPartners
     expect(numPartners).to.eq(2)
  })

Upvotes: 0

Alapan Das
Alapan Das

Reputation: 18634

You do something like this:

cy.get('#_evidon_message').should('contain.text', '2')

Upvotes: 2

Related Questions