How to Perform Assertions in Cypress

Reading Time: 3 minutes

Hello Readers, Today we will learn about assertions their significance, and how to use them in Cypress.

What is Cypress?

The Cypress tool is a JavaScript testing automation solution used for web automation.

It enables teams to create web test automation scripts. This solution aims to facilitate frontend developers and test automation engineers to write web tests in the web language that is JavaScript for web test automation.

What are Assertions?

Assertions are the validation steps that determine whether the specified step of the automated test case succeeded or not.

In general, Assertions validate the desired state of your elements, objects, or application under test

Advantages

  • It provides better observability into the design and hence helps in easier debugging of test failures.
  • Used for both dynamic simulations as well as in formal verification of the design.
  • Provide functional coverage on input stimulus and validate that a design property is in fact simulated.
  • Help testers make decisions on specific parts of the code which are guaranteed to return specific, error-free results.

Assertions in Cypress

Cypress integrates multiple assertions from various JS assertion libraries such as Chai, jQuery, etc. We can broadly classify all of these into two segments.

Implicit Assertions

When the assertion applies to the object the parent chained command provides, it’s called an Implicit assertion.

Additionally, this category of assertions generally includes commands such as .should() and .and().

As these commands don’t stand independently and always depend on the previously chained parent command.

Example Code

Let’s understand from an Example mentioned below for Implicit Assertion.

In the above example, we are demonstrating the use of Implicit functions through the steps mentioned below:-

  1. Create a sample test for https://example.cypress.io
  2. Test the following cases
       /* Command for AutoSuggestions in Cypress */

/// <reference types="Cypress" />        

it('Implicit Assertion Example',function()
{
    /* this will redirct to the target url we want to visit. */

    cy.visit('https://example.cypress.io')  
    

/* Get the DOM element containing the text */
    
  cy.contains('get').click()   


 /* Get a Particular element of the page */   
   
   cy.get('#query-btn')


 /* Should Contains - Trying to select an element */   
  
    .should('contain','Button')


    cy.get('#query-btn')


 /* Should have -The current subject is yielded  */    
    
.should('have.class','btn-primary') 

    cy.get('#query-btn')


 /* Should - be enabled, visible, equal  */   

    .should('be.enabled')

     cy.get('#query-btn')

    .should('be.visible')

    cy.get('#query-btn')

    .invoke('attr','id')

    .should('equal','query-btn')  

 /*And - Used for Chaining Multiple Assertions  */  
 
    cy.get('#query-btn')
      
    .should('contain','Button')

    .and('have.class','btn-primary')

 
/*  Explicit Assertion */

      
/*Expect - Expecting some Value  */

    
    expect(true).to.be.true

    let name = "Hitesh"

    expect(name).to.be.eql("Hitesh")


/* Assert - Checks whether the Actual result matches the expected result  */ 

   
    assert.equal(10,10,'Not Equal!')
    
    assert.strictEqual(10,10,'Not Equal')


})
  

Output

To Display an Output in cypress, Go to terminal and type the following command

npx cypress open

This will Display the File to show the output

To make an assertion about the current subject, use the .should() command.

To chain multiple assertions together, use the .and() command.

Explicit Assertions

When there is a need to pass an explicit subject for the assertion, it falls under the category of Explicit assertion.

This category contains the commands such as “expect()” and “assert()“, which allow you to pass an explicit subject/object.

Example code

To make a BDD assertion about a specified subject, use expect.

To make a TDD assertion about a specified subject, use assert.

Output

References

https://docs.cypress.io/guides/overview/why-cypress

Written by 

I am a software Consultant at knoldus Inc in Test automation studio. He is familiar with the concepts of manual testing and automated testing using Postman. He is always eager to learn advanced concepts to update himself. He likes singing and listening to music.

Leave a Reply