Skip to content
Advertisement

Cypress – how to properly wait for result of an imported JS function

I am new to Cypress (and naive to JS). I would like to write a JS library as a wrapper to 3rd party APIs. I write the API wrapper as an individual file (instead of using Cypress Custom functions) because I believe I can share the library with teams NOT using Cypress E2E tool.

The problem I am facing is “I cannot let my code to be executed sequentially in order”

From the result, I can see:

  1. the data didn’t return successfully
  2. it looks like the “getTestPlanIdByName:20974” were executed last, but I expect it should be executed before “line 01b testPlanId:{}”

I need to help to know the correct way to handle the flow sequentially in Cypress/Javascript, thanks.

enter image description here

API Library(api-util.js)

JavaScript

My Cypress code

JavaScript

Advertisement

Answer

Thank you all. Cypress flow isn’t 100% compatible with standard JS Promise (Wait for an own function (which returns a promise) before tests are executed). After relentless testings, I decided to use a Cypress Custom Command wrapper to wrap my in-house JS library. Though adding an extra layer may seem a little cumbersome. But I am satisfied with the result. Share my code here in case anyone might need it. 🙂

Cypress Code

JavaScript

Cypress Custom Command

JavaScript

In-house JS library

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement