SDET Unicorns

Cypress Assertions

Cypress Assertions

Table of Content

In this tutorial, we will cover different types of Cypress Assertions such as the default, implicit and explicit assertions and talk about the difference between the should and the expect assertions.

Cypress uses the Chai assertion library as well as the extensions of Sinon & jQuery to provide you with dozens of powerful assertions for free.

Default Assertions

Cypress comes with many default assertions that can be used without having to explicitly define assertions, such as –

  • cy.visit(): every-time you visit a page, Cypress expects the page to return with a 200 status code
  • cy.get(): the get command expects the element to exist in the DOM first before trying to access it

Implicit Assertions

The implicit assertions used the should() or the and() commands when making assertions. This is the preferable way of making assertions in Cypress.

cy.get('h1').should('have.text', 'My Heading')

You can even chain multiple assertions together –

cy.get('h1')
   .should('have.text', 'My Heading')
   .and('have.class', 'highlighted')

Explicit Assertions

You should use the Explicit assertions when you would like to make multiple assertions for the same subject or when you would like to manipulate your subject before making your assertion. For explicit assertions, you will use the expect command.

cy.get("h1.elementor-heading-title").should(($heading) => {
    // manipulate your text here and assert below
    expect($heading.text()).to.eq("Think different. Make different.");
    // add further expect assertions here
  });

Check out the video below to see learn more about Cypress Assertions –


I hope this post helped you out, let me know if you have any questions in the comments below!

Happy testing! 

My Resources

Thrive Suite: The all-in-one WordPress theme I used to create my blog.
Jasper AI: My favorite AI writing tool.
Surfer SEO:  A tool to help you rank your content on the first page of Google.

Write Content Faster

5/5

Write blog posts that rank for affiliate terms!

Join our mailing list

Unlock the Secrets to Becoming a SDET Pro in the Industry!

Stay ahead of the curve! Join our mailing list for exclusive insights, tips, and the latest industry updates delivered straight to your inbox

Table of Content

Related Post

7 Principles of Software Testing

7 Principles of Software Testing with Examples

Software testing plays a crucial role in the software development life cycle as it helps ensure the quality, reliability, and performance of the final product. Software testing is not just about finding defects; it also provides valuable insights into the overall quality of the software. By conducting effective software testing, it shows how well the application behaves in different scenarios and whether it meets user expectations.

Read More »
Differences Between CI/CD and DevOps

Key Differences Between CI/CD and DevOps

Software development is far from a simple journey that involves merely executing code and launching applications. It often presents challenges, as it’s rare to get everything right from the start. Each time you implement new or frequent code changes, you inadvertently introduce potential issues that can be time-consuming to resolve. This complexity necessitates a structured approach to ensure smooth production.

Read More »