Search code examples
cypressassertion

Unable to assert a value in Cypress


I have an input field where I type the value 1000, and when I try to assert the value, the assertion fails: https://i.sstatic.net/4WVhK.png

My code cy.get('#insurance_cover_money').type(1000).should('have.value', '1 000')

HTML: https://i.sstatic.net/0KS7b.png

Thank you!


Solution

  • You can try this.

    cy.get('#insurance_cover_money').type('1000').should('have.value', '1000')
    

    If the above is still not working you can do two things:

    1. Log the value and then copy-paste it into the should assertion.
    cy.get('#insurance_cover_money')
      .type('1000')
      .invoke('val')
      .then((val) => {
        cy.log(val)
      })
    
    1. Extract the value in the first get and then pass the same value pass into the should assertion.
    cy.get('#insurance_cover_money')
      .type('1000')
      .should('not.have.value', '0')
      .invoke('val')
      .then((val) => {
        cy.get('#insurance_cover_money').should('have.value', val)
      })
    

    You can also try increasing the timeout:

    cy.get('#insurance_cover_money', {timeout: 8000})
      .type('1000')
      .should('have.value', '1000')
    cy.get('#insurance_cover_money', {timeout: 8000})
      .type('1000')
      .should('have.value', '1 000')
    

    In case you want to remove spaces and assert, you can do like this:

    cy.get('#insurance_cover_money')
      .type('1000')
      .invoke('val')
      .then((val) => val.replace(/\s/g, ''))
      .should('eq', '1000')