Cypress Assertion: A Comprehensive Guide to Making Assertions in Cypress



Download & Install cypress real quick

cypress-assertions.png

Cypress is a popular JavaScript testing framework that allows developers to write end-to-end tests for web applications. One of the key features of Cypress is its powerful assertion library, which allows developers to make assertions about the behavior of their application.

In this article, we'll take a closer look at Cypress assertions, including what they are, how they work, and best practices for making assertions in Cypress.



Cypress Assertion: A Comprehensive Guide to Making Assertions in Cypress



What are Cypress Assertions?



Cypress assertions are statements that allow developers to check whether their application behaves as expected. In Cypress, assertions are made using the should() function, which is chained to a command that selects an element on the page. For example, to check if a button is visible, you would use the should('be.visible') assertion:


cy.get('button').should('be.visible')



Cypress provides a wide range of assertions that developers can use to check the behavior of their application. Some of the most common assertions include:

should('have.text', 'expected text'): checks if an element has the expected text.
should('be.visible'): checks if an element is visible.
should('not.exist'): checks if an element does not exist.
should('have.class', 'class-name'): checks if an element has a specific class.
should('have.attr', 'attribute', 'value'): checks if an element has a specific attribute with a given value.




How do Cypress Assertions Work?



Cypress assertions work by chaining the should() function to a command that selects an element on the page. The should() function takes a string argument that specifies the assertion to be made.

For example, to check if an element has the expected text, you would use the should('have.text', 'expected text') assertion. If the assertion is true, the test passes. If the assertion is false, the test fails.

Here's an example of how assertions work in Cypress:


cy.get('button').should('be.visible')
cy.get('input').should('have.value', 'Hello, World!')
cy.get('div').should('have.class', 'container')
In this example, we're using the should() function to make three different assertions. The first assertion checks if a button is visible, the second assertion checks if an input field has the value "Hello, World!", and the third assertion checks if a div has the class "container". If any of these assertions fail, the test will fail.


Best Practices for Making Assertions in Cypress



To make effective assertions in Cypress, it's important to follow some best practices. Here are a few tips to keep in mind:


1. Use meaningful assertion messages: When making assertions, use messages that clearly describe what you're checking. This will make it easier to understand why a test failed if it does fail.

2. Use .should() instead of .then(): In Cypress, it's recommended to use the should() function instead of the then() function when making assertions. The should() function retries the assertion until it passes or times out, while the then() function does not.

3. Use multiple assertions per test: To ensure that your application behaves as expected, it's a good practice to make multiple assertions in a single test. This will help to catch any unexpected behavior that might have been missed by a single assertion.

4. Use custom assertions: Cypress allows you to create your own custom assertions, which can be useful for making complex assertions or for reuse in multiple tests. Custom assertions can be defined using the addMethod() function.

5. Use negative assertions sparingly: While negative assertions like should('not.exist') can be useful, they should be used sparingly. Negative assertions can make tests harder to read and understand, and can also be less reliable than positive assertions.

6. Test both positive and negative scenarios: When making assertions, it's important to test both positive and negative scenarios. This will ensure that your application behaves as expected in all possible cases.


Conclusion



Cypress assertions are a powerful tool for testing web applications. By using the should() function, developers can make assertions about the behavior of their application and ensure that it behaves as expected.

To make effective assertions in Cypress, it's important to follow best practices like using meaningful assertion messages, using multiple assertions per test, and testing both positive and negative scenarios.

With these best practices in mind, you'll be able to write more reliable and effective tests using Cypress.





Read Next :