Wait for API after button click in Cypress Last Updated : 24 Oct, 2024 Comments Improve Suggest changes Like Article Like Report When writing end-to-end tests, it's common to trigger API requests through user actions such as button clicks. Cypress provides a powerful way to wait for these requests and their responses using cy.intercept() and cy.wait(). By doing this, you ensure that your test doesn’t proceed until the API request completes and the response is processed.ExampleHTML CodeHere’s an example HTML page where a button triggers an API call: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>API Wait Example</title> <script> function fetchData() { fetch('https://jsonplaceholder.typicode.com/posts/1') .then(response => response.json()) .then(data => { document.getElementById('result').innerText = `Title: ${data.title}`; }); } </script> </head> <body> <button id="fetch-btn" onclick="fetchData()">Fetch Data</button> <div id="result"></div> </body> </html> Cypress Test CodeExample 1: Wait for an API response after clicking the button. JavaScript describe('Wait for API response after button click', () => { it('should wait for the API call to complete', () => { cy.visit('http://localhost:3000'); // Load your local HTML page // Intercept the API request cy.intercept('GET', 'https://jsonplaceholder.typicode.com/posts/1').as('getPost'); // Click the button to trigger the API request cy.get('#fetch-btn').click(); // Wait for the API request to finish cy.wait('@getPost'); // Assert that the API response has updated the result on the page cy.get('#result').should('contain', 'Title'); }); }); ExplanationHTML Code: A button with an id of #fetch-btn triggers a JavaScript function that sends a fetch request to the API and updates the #result div with the response data.Cypress Test Code:cy.intercept('GET', 'https://jsonplaceholder.typicode.com/posts/1').as('getPost'); sets up an interceptor to listen for the API request made to the specified URL and assigns it an alias @getPost.cy.get('#fetch-btn').click(); clicks the button, which triggers the API request.cy.wait('@getPost'); waits for the API request to complete before proceeding.cy.get('#result').should('contain', 'Title'); asserts that the #result div contains the word "Title", verifying that the API response was processed and displayed.OutputWhen the test is run:Cypress will wait for the API request to complete after the button click.Once the request is complete, it will verify that the page content has been updated with the response data.In the Cypress test runner, you can see the intercepted request and its response details, ensuring that the request was captured and processed.OutputConclusionUsing cy.intercept() and cy.wait() in Cypress allows you to handle asynchronous API calls effectively. This ensures that your tests wait for the necessary responses before making assertions, leading to more reliable and accurate tests. Comment More infoAdvertise with us C codersaksyevt Follow Improve Article Tags : Software Testing Testing Tools Cypress Similar Reads Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp 10 min read What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here 11 min read Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing, 3 min read Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is 8 min read Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is 7 min read Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa 15+ min read Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le 4 min read Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ 8 min read SDLC MODELSWaterfall Model - Software EngineeringThe Waterfall Model is a Traditional Software Development Methodology. It was first introduced by Winston W. Royce in 1970. It is a linear and sequential approach to software development that consists of several phases. This classical waterfall model is simple and idealistic. It is important because 13 min read What is Spiral Model in Software Engineering?The Spiral Model is one of the most important SDLC model. The Spiral Model is a combination of the waterfall model and the iterative model. It provides support for Risk Handling. The Spiral Model was first proposed by Barry Boehm. This article focuses on discussing the Spiral Model in detail.Table o 9 min read What is a Hybrid Work Model?Hybrid means a thing made by a combination of two different elements and the resulting hybrid element acquires characteristics of both underline elements. The following topics of the hybrid model will be discussed here:What is the Hybrid Model?Why the Hybrid Model?When To Use a Hybrid ModelProcess o 13 min read Prototyping Model - Software EngineeringPrototyping Model is a way of developing software where an early version, or prototype, of the product is created and shared with users for feedback. The Prototyping Model concept is described below: Table of ContentWhat is Prototyping Model?Phases of Prototyping ModelTypes of Prototyping ModelsAdva 7 min read SDLC V-Model - Software EngineeringThe SDLC V-Model is a type of Software Development Life Cycle (SDLC). It is a method that includes testing and validation alongside each development phase. It creates a structure like the letter 'V,' which includes various phases that we will discuss in detail.Phases of SDLC V-ModelThe V-Model, whic 9 min read TYPES OF TESTINGManual Testing - Software TestingManual testing is an important part of software development. Unlike automated testing, it involves a person actively using the software to find bugs and issues. This hands-on approach helps ensure the software works as intended and meets user needs. Table of ContentWhat is Manual Testing Types of Ma 14 min read Automation Testing - Software TestingAutomated Testing means using special software for tasks that people usually do when checking and testing a software product. Nowadays, many software projects use automation testing from start to end, especially in agile and DevOps methods. This means the engineering team runs tests automatically wi 15+ min read Like