Tutorials Hut

  • Cypress tutorials




  • Default Assertion in Cypress

    Assertion is used to verify a state of elements, objects and application that as per functionality is true.

    Cypress commands automatically retry their assertions.The assertion behavior can be modified as per the use case.

    We will cover below topics of assertions in cypress below:

    Default Assertion in cypress

    When not to assert in Cypress?

    Many Cypress commands have default built in Assertions and do not require to be invoked specifically.

    Few of the commands that have default assertions are:

      • cy.visit() : expects the page to send text/html content with a 200 status code.
      • cy.request() :expects the remote server to exist and provide a response.
      • cy.contains() :expects the element with content to eventually exist in the DOM.
      • cy.get() :expects the element to eventually exist in the DOM.
      • .find() : expects the element to eventually exist in the DOM.
      • .type() :expects the element to eventually be in a typeable state.
      • .click() :expects the element to eventually be in an actionable state.
      • .its() :expects to find a property on the current subject.

    Some Example to explain default assertion:

      1. Default assertion example:

     // there is a default assertion here

      // .get() —verifies that button must exist in the DOM before proceeding

      // .click() — verifies the button must be “actionable”

    cy.get('button').click()

    2. Reverse default assertion:

    In some scenarios you need to verify that element does not exist or wait till it does not exist. You can simply reverse the default assertion by adding that assertion and Cypress will reverse its rules waiting for elements to exist. .should(‘not.exist’)   is added to achieve this. // and now make sure this #alertMsg does not exist in the DOM // and automatically wait until it’s gone!
    cy.get('#alertMsg').should('not.exist')
    *Default assertion waits up to 4 seconds for it to exist in the DOM for cy.get()* Cypress gives different default timeout values for different commands.

    Modify timeout of Default Assertions

      • By Default the timeout set for default assertion is 4 seconds.
      • For Example if there is a command cy.get(‘.button’) , cypress will wait for 4 seconds for the element to exist in DOM, otherwise it will fail.
      • This default time can be modified using “timeout”.
    cy.get('.button', { timeout: 10000 })
    .should('be.visible')
    .and('contain', 'Tutorials')

    cypress waits up to 10 seconds for ‘.button’ element to exist in the DOM

    and waits up to 10 seconds for it to be visible

    and waits up to 10 seconds for it to contain the text: Tutorials

    Notice that this timeout has flowed down to all assertions and Cypress will now wait up to 10 seconds total for all of them to pass.

    NOTE:  The timeout parameter always goes inside the command not the assertion. Otherwise it will give an error.














  • Leave a Reply

    Your email address will not be published. Required fields are marked *