Assertions and types of assertions in cypress: An Quick Overview

HPE
Reading Time: 2 minutes

In this blog, we are going to discuss cypress, assertions, and types of assertions in cypress.

Cypress:  It is a next-generation front-end testing tool constructed for the modern web. The idea behind creating cypress was to aim at the key pain points that the testers face while executing their testing. For example, synchronization issues, and the inconsistency of tests due to elements not available in the DOM.

Cypress is built on Node.js and comes packed as an npm module. Since it is built on node.js, the tests we write using cypress are in javaScript only. However, most of the coding can be done by using Cypress inbuilt commands, which are way easier to understand.

Assertion: It determines the state of the application whether it is the same as what we are expecting or not. If the assertion fails, then the test case fails and stops the execution.

Cypress has more than one type of assertion obtained from various libraries like Mocha, Chai, and so on. The assertion types are explicit and implicit.

  1. Implicit Assertion: These are in-built assertions. If an assertion is applicable to the object obtained from the parent command in a chain,we can call it as the implicit assertion. The popular implicit assertions include .and/.should.
/// <reference types="cypress" />
 
   it('Implicit Assertion', function (){

      // test step to launch a URL

      cy.visit("https://example.cypress.io/")
       
        cy.contains('get').click()

        // Implicit assertion using should

        cy.get('#query-btn').should('contain','Button')

        .should('have.class','query-btn')
 
        cy.get('#query-btn')

        .invoke('attr', 'id').should('equal', 'query-btn')

       
       
        // Chaining the assertion using and
 
        cy.get('#query-btn').should('contain','Button').and

        ('have.class','query-btn')

   });
  1. Explicit Assertion: These are not in-built assertions.If an assertion is applicable to an object directly, we can call it as the explicit assertion. The popular explicit assertions include assert/expect.
/// <reference types="cypress" />
 
it('Explicit Assertion', function (){

   // test step to launch a URL
 
   cy.visit("https://example.cypress.io/")

 // Explicit assertion

   expect(true).to.be.true
 
let name = 'cypress';

expect(name).to.be.equal('cypress')
 
// Using Assert
      assert.equal(7,7,'Not Equal')
  });

Default Assertions:

Cypress has default assertions as follows:

  • cy.visit () − Expects the page to show the content with 200 status code.
  • cy.request () − Expects the remote server to be available and sends a response.
  • cy.contains () − Expects the web element with its properties to be available in DOM.
  • cy.get () − Expects the web element to be available in DOM.
  • .find () − Expects the web element to be available in DOM.
  • .type () − Expects the web element to turn to a type-able state.
  • .click () − Expects the web element to turn to a clickable state.
  • .its () − Expects for a web element property on the existing subject.

References :

https://docs.cypress.io/guides/references/assertions

https://www.tutorialspoint.com/cypress/cypress_assertions.htm


Knoldus-blog-footer-image

Written by 

I am a software Consultant-QA at knoldus Inc in test automation studio. She is familiar with the core concepts of manual testing and automated testing using Selenium and cypress. She is always eager to acquire new tech skills and learn new & advanced concepts to advance herself. She likes to watch web series and listening music.