Cypress – Assertions

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.

Implicit Assertions

If an assertion is applicable to the object obtained from the parent command in a chain, it is known as the implicit assertion. The popular implicit assertions include .and/.should.

These commands cannot be used as standalone. Generally, they are used when we have to verify multiple checks on a particular object.

Let us illustrate implicit assertion with an example given below −

// test suite
describe('Adglob', function () {
   it('Scenario 1', function (){
      // test step to launch a URL
      cy.visit("https://Adglob.in/videotutorials/index.php")
              // assertion to validate count of sub-elements and class attribute value
              cy.get('.toc chapters').find('li').should('have.length',5)
              .and('have.class', 'dropdown')
   });
});

The output logs show two assertions obtained with should and commands.

Explicit Assertions

If an assertion is applicable to an object directly, it is known as the explicit assertion. The popular explicit assertions include assert/expect.

The command for the explicit assertion is as follows −

// test suite
describe('Adglob', function () {
// it function to identify test
   it('Scenario 1', function (){
      // test step to launch a URL
      cy.visit("https://accounts.google.com")
              // identify element
      cy.get('h1#headingText').find('span').then(function(e){
         const t = e.text()
         // assertion expect
         expect(t).to.contains('Sign')
      })
   })
})

Execution Results

The output is given below −

The output logs show assertions directly applied to objects with the expected command.

Cypress has Default Assertions that are internally handled and do not require to be invoked specifically.

Few examples are 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.

Other Cypress assertions

The other Cypress assertions are as follows −

length

It checks the count of elements obtained from the previously chained command.

For example,

cy.get('#txt-fld').should('have.length',5)

value

It checks whether the web element has a certain value.

For example,

cy.get('#txt-fld').should('have.length',5)

value

It checks whether the web element has a certain value.

For example,

cy.get(' #txt-fld').should('have.value', 'Cypress')

class

It checks whether the web element possesses a certain class.

For example,

cy.get('#txt-fld'').should('have.class', 'txt')

contain

It checks whether the web element possesses a certain text.

For example,

cy.get('#txt-fld'').should('contain', 'Cypress')

visible

It checks whether the web element is visible.

For example,

cy.get('#txt-fld'').should('be.visible')

exist

It checks whether the web element is available in Document Object Model (DOM).

For example,

cy.get('#txt-fld'').should('not.exist');

css

It checks whether the web element possesses a certain CSS property.

For example,

cy.get('#txt-fld'').should('have.css', 'display', 'block');

This Post Has 2 Comments

Leave a Reply