Understanding a Cypress Promise and Resolving a promis

 

Hello all, since we are back in a 10 day lock down in Sri Lanka, I decided to write a small blog post for my Lockdown blog post series. In this post let's try to understand the concept of  Promise and Resolving a promise with Cypress.

In contrast to Selenium, Cypress which is build on top of NodeJS is asynchronous by nature. As a result, Cypress will not wait for a specific step to execute before the next step will be executed.

The asynchronous nature of Cypress will results in the script being in one of three status of Pending,Resolved or Rejection. This asynchronous nature will result in no guarantee of how the script will execute.


Cypress provides with a solution for controlling this asynchronous behavior with the .then() which will wait for a promise to be resolved.


A promise resolved means a step is successfully executed, a promise is rejected means there is an error in a specific step, and a promise in pending status means a specific step is not yet executed.


As the below code example, the cy.get will only be executed once the cy.visit step promise is resolved. 


it(‘some test case’){


  cy.visit(‘/somepath’).then(() =>{

    return cy.get(‘.some selector’)

  })

}


Finally, if you gave a non cypress code that calls a cypress code then at that point we need to resolve that using the .then() method to ensure that the code will not fail.


However, in their official documentation it is mentioned that they have taken care of handling this asynchronous behaviour, thus making the life of developers and testers easy.

Comments