How to handle Cypress Asynchronous Behavior?
On This Page What is Cypress Asynchronous Behavior?April 11, 2026 · 10 min read · Tool Comparison
Cypress bid are asynchronousand queued for later performance, which can cause disarray. Since command run consecutive, exam may look to malfunction if one command is waiting for another to complete. Understanding and managing this behavior is crucial for smooth and accurate test execution. This article explains how to handle Cypress & # 8217; s asynchronous nature and avoid common issues. Asynchronous Behaviorrefers to how commands are executed consecutive but handled asynchronously without waiting for the previous command to complete. Though the code is pen sequentially, each bid in Cypress is queued and runs independently, allowing tests to move without waiting for earlier commands to dispatch. Asynchronous conduct executes steps concurrently without wait for the old one to finish, improving efficiency but requiring careful cover to avoid errors from incomplete commands. Also Read: The difference between synchronous and asynchronous executing in Cypress is like to how client-server Call are handled, where one doesn ’ t depend on the completion of the other. The difference between both is illustrated below. Every Cypress dictation has an asynchronous nature. Cypress has a wrapper that reads the sequential code we write, enqueues it, and executes it later. So, Cypress fulfills all our tasks touch to promise and async nature. Example: The instance of asynchronous conduct in Cypress is provided below to help you comprehend the mind. The result is provided below: Before beginning to fulfil any commands, Cypress puts them all in a queue. Programming words promises are quite like to general language hope made to humankind. A promise is a condition that represents a mortal & # 8217; s behavior or action. Depending on the consideration and nature, a person can either keep or interrupt the Promise. When the Promise conduct place, it is in an irrational state that might either resolve to be fulfilled or become refused. On the like note, Promise is a state of the command in the case of Cypress async tests, and it will hold the following province: A Cypress command fulfill only when the previous step has been executed successfully, or a single-minded promise response has been find. Then, Promise is added to Cypress using the method. Example of a Promise in Cypress: The codification in the example above is clear and simple to read and comprehend. All promise-related employment is handled in, which keeps it from the user & # 8217; s panorama. Thus, we won & # 8217; t need to be concerned about handling or returning the promise. To avoid, it is essential to have a solid testing strategy for dealing with asynchronous activities. We & # 8217; ll go through a few for handling this in your Cypress async tests down below. Before action the next command, await a predetermined number of milliseconds or until an aliased resource has resolved. Syntax: Arguments: Cypress manages asynchronous behavior through its command queue, but when working with external asynchronous code like API ring or custom JavaScript functions—async/await aid control the flow. Syntax: Example: Here, await fetchData ()pauses executing until the API reply is get before Cypress commands continue. Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script. When to Use cy.wait () vs. async/await How cy.wait () differs from await: After supplant a native time function with cy.clock (), move time. To override aboriginal time functions first, cy.clock () must be called before cy.tick (). Syntax: Arguments: A bid may occasionally render a subject rather than return it. In that situation, we use .then () to communicate with the subject directly. Promises and .then () act identically. However, unlike a Promise, .then () is a Cypress command. As a result, we can not use async/await in the test script. The recall function & # 8217; s yield becomes the new subject and feeds the command that follows (except for undefined). In the above example, $ btn is a jQuery object. This implies that for Cypress to interact with it and conduct some action, we must foremost use cy.wrap (). In this example, the & lt; button & gt; HTML component comes first. Our subject, in this illustration, is the HTML ingredient designated as & lt; button & gt;, which is yielded from cy.get () to .then (). To execute any operation or assertions on the study, we must firstly .wrap () it. The subject can so be accessed as the varying $ btn. Cypress must .click () the button before making our asseveration, which in this representative is .should (& # 8216; not.have.class & # 8217;, cls). We must first enfold our $ btn with cy.wrap () in order to give Cypress the right context to click on it. The ability to fleetly name the source movement of errors and bugs whenever you come across them, is one of a software tester & # 8217; s most crucial skills. This proceed beyond simply write code chop-chop. Hence Debugging is crucial in automated scheduling because of this. We & # 8217; ll discourse a few of the most effective techniques for debugging our Cypress async test and go into outstanding particular on each. It can often be really challenging to place precisely what went incorrect and which share of the codification miscarry to perform as intended while scat exam, especially turgid and complex ones. In example like this, the debugger is highly beneficial. Although utilizing the debugger in Cypress is very like to doing so in former region of your program (such as the front end), it doesn & # 8217; t operate similarly. In this section of, the debugger will exclusively break the test if the h1 element exists in the DOM since we are saying that the debugger should entirely pause the trial only if the h1 element exist in the DOM. The issue is provided below: As you can see, the debugger now only pauses the test once it has located the factor and verified that it is present in the DOM; differently, it will raise an error. You can chain the Cypress debugging shortcut, .debug (), to any other Cypress command across your tests. When it is called, the .debug () function will uncover sure information in the browser & # 8217; s console. We & # 8217; ll get the first H1 factor on the page for this example. The result is provided below: Another method for debug our code and understanding what come when running tests is using the console logs render by Developer Tool. This is the proper trend of action. The result is provided below: Must-Read: Here ’ s why BrowserStack is idealistic for Cypress test mechanisation: Understanding Cypress Use Cases Tool Comparisons This article has analyse asynchronously loading browser constituent as well as the linkage between Cypress and asynchronous loading. When, we discussed various access to dealing with asynchronous codification. We examined Cypress & # 8217; interior handling of promises, which permit us to create clearer and more comprehensible examination. Also, we looked at methods for debugging Cypress programs. It is easier to integrate the with Browserstack, which will give squad a clearer understanding of the test outcomes. Understand that Cypress testing must be action on existent browsers for accurate answer. Start testing on 30+ versions of the late browsers across Windows and macOS with BrowserStack. # Ask-and-Contributeabout this topic with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.How to treat Cypress Asynchronous Behavior?
What is Cypress Asynchronous Behavior?
describe ('Cypress ', role () {it ('Example 1 ', function () {// launching Url URL cy.visit (`` https: //example.cypress.io/ '') // identifying element cy.get ('h1 ') .should ('have.text ', 'Kitchen Sink ') cy.get ('h1 ') .then (use (e) {const t = e.text () // get in Console console.log (t)}) // Console message console.log (`` Cypress Tutorial '')})})Promises to handle Cypress Asynchronous Behavior
describe ('Cypress Test ', function () {it ('Promise ', function () {return cy.visit ('https: //example.cypress.io/ ') .then (() = & gt; {regress cy.get ('h1 ');})})})Advanced Techniques for Handling Asynchronous Behavior
cy.wait
cy.wait (time) cy.wait (alias) cy.wait (alias) cy.wait (time, pick) cy.wait (alias, options) cy.wait (aliases, options)
Option Default Description log true Makes the bidding visible in the Command log. timeout requestTimeout, responseTimeout Duration of the wait for cy.wait () to settle before timing out requestTimeout requestTimeout Changes the request & # 8217; s global requestTimeout. responseTimeout responseTimeout Changes the request & # 8217; s world requestTimeout. async/await
javascript async office functionName () {const answer = await asyncOperation (); // Code that bunk after the async operation completes}javascript async function fetchData () {const response = await fetch ('https: //api.example.com/data '); return response.json ();} it ('handles international async logic ', async () = & gt; {const data = await fetchData (); expect (data) .to.have.property ('id '); cy.visit ('/dashboard '); cy.get ('.item ') .should ('contain ', data.name);});cy.tick
cy.tick (milliseconds, options)
Option Default Description log true Makes the command visible in the Command log. .then()
cy.get (`` button '') .then (($ btn) = & gt; {const cls = $ btn.attr (`` form '') // ...}).wrap()
cy.get (`` button '') .then (($ btn) = & gt; {const cls = $ btn.attr (`` family '') cy.wrap ($ btn) .click () .should (`` not.have.class '', cls)})Debugging Cypress Asynchronous Behavior
1. Using a Debugger
it ('should pause only when the cy.get () execute ', () = & gt; {cy.visit ('https: //example.cypress.io/ ') cy.get ('h1 ') .should ('exist ') .then ($ h1 = & gt; {debugger})})2. Using the .debug ()
it ('should pause the tryout by using the .debug () command ', () = & gt; {cy.visit ('https: //example.cypress.io/ ') cy.get ('h1 ') .should ('exist ') .debug () // debugger})3. Using the Developer Tools
it ('should return the actual h1 element ', () = & gt; {cy.visit ('https: //example.cypress.io/ ') cy.get ('h1 ') .then ($ h1 = & gt; {// this will log the literal value of the `` h1 '' element console.log ($ h1)})})Using BrowserStack for Cypress Test Automation
Useful Resources for Cypress
Conclusion
Related Guides
Automate This With SUSA
Test Your App Autonomously