Handling of Javascript Async code in Test Automation

Sample Javascript Code
Execution Result

How to run JavaScript test code in sequence

  • Callback Functions
  • Promise
  • Async/Await

Promise

  • Pending — Initial state
  • Fulfilled — Successful completion of asynchronous code
  • Rejected — Failure occurs due to timeouts or any other reasons
Promise workflow
  • onfulfilled — The callback to execute when the Promise is resolved.
  • onrejected — The callback to execute when the Promise is rejected.
Promise chaining example

Async/Await

Async function
The output of the above function
Async/Await example
Output

Asynchronous Code in Cypress

Asynchronous Code in Playwright with Javascript/TypeScript

Example test with await keyword
Error Message
Console log of promise status
Await keyword added on goto statement
Test result

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sanjeev Kumar

Sanjeev Kumar

7+ years of experience in Automation Testing.