Cypress - Variables


In Cypress, there is usage of variables like var, let, and const. While working with closures,we can use the objects that were obtained without assignment. But, this is not the case,when we are working with mutable objects.

When an object modifies its characteristics, we may need to compare its prior value to its new value.

Code Implementation

We can do the code implementation by using the below mentioned command −

cy.get('.btn').then(($span) => {
   // value capture before button click and stored in const
   const n = parseInt($span.text())
   cy.get('b').click().then(() => {
      // value capture after button click and stored in const
      const m = parseInt($span.text())
      // comparison
      expect(n).to.eq(m)
   })
})

In the above case, we are using const variables since object $span is undergoing change. While dealing with mutable objects and its value, it is recommended to use variables of type const.

Advertisements