How to assert behaviors and properties with Chai assertions on Cypress

 

How to assert behaviors and properties with Chai assertions on Cypress

In this blog post we will look into a concept of Chai assertions when used with Cypress. When writing Cypress test we need to assert the output of the flow that we automate. This can be the behavior or the property.

Let's say we have a checkbox on our web page and we want to click on the checkbox and verify if the checkbox is actually checked. For this type of situations we use be. because we want to check the behavior. For an example we can write as below.

cy.get('#mychkbox').check().should('be.checked')

Let's say we want to click on the checkbox and validate the property. For validating a properly we need to use have. In our above example once we click if we want to assert the value of the property then we need to write the code as below.

cy.get('#mychkbox').check().should('be.checked').and('have.value','bluecolor')

In the above line of code we can see .and this is the way multiple assertions can be concatenated rather than repeating the keyword "should".


Comments