Can we intercept the same API call twice while a webpage is being loaded using Cypress?

Is there a way in Cypress to intercept the same API call twice when loading a webpage, using aliasing and the wait method?

cy.intercept('POST', '/SomeUrl', {
    statusCode: 200,
    fixture: 'jsonname.json',
    times: 1,
}).as('alias');
cy.intercept('POST', '/SomeUrl', {
    statusCode: 200,
    fixture: 'jsonname2.json',
    times: 1,
}).as('alias2');

cy.visit('https://www.url.com')
   .wait('@alias')
   .wait('@alias2')

The output in the Cypress UI Routes tab:

Method URL Stubbed Alias #
POST /SomeU r l Yes alias -
POST /SomeU r l Yes alias2 2

Answer №2

This issue has been resolved using the following code:

cy.intercept('POST', '/AnotherEndpoint', (req) => {
     if (req.body.Objectstatus === 1) {
     req.reply({
         statusCode: 200,
         fixture: 'data.json',
     });
}
     if (req.body.Objectstatus  === 1) {
         req.reply({
         statusCode: 200,
         fixture: 'data2.json',
     });
     }
   }).as('mockData');
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to select all options in an Angular mat-select component using a Boolean function

Is there a way to enable Select All functionality in Angular mat-select by using Boolean functions without the need for form tags or form builders? Component.html <div layout="column" class="mat-cloumn w-25"> <mat-form-field ...

Is there an automatic bottom padding feature?

Currently, I am facing a challenge in fitting the loader into the container without it being overridden by the browser. Using padding-bottom is not an ideal solution as it results in the loader appearing un-resized and unprofessional. Any suggestions or co ...

The GET Request made for an Image in an Angular Component results in a 404 error message indicating

I have been working on uploading and fetching images from my mongodb database. Utilizing multer for the image uploads, along with express and mongoose for handling GET/POST requests has been successful so far. The process involves uploading the image to th ...

Can I modify a global array by updating a dynamically created array in the ngOnInit method of Angular?

Are there any suggestions on how to make a dynamic array available globally in Angular? I am currently using this codepen () which stores clicked countries in an array. The issue is that the array is nested within a function in ngOnInit and I need it to b ...

Obtaining a value from within an Angular 'then' block

I have a unique issue that I haven't been able to find a solution for on StackOverflow: Within an Angular 6 service, I am trying to call a function from another service using TypeScript. Here is the code snippet: Service1: myArray: Array<IMyInte ...

Applying Type Constraints in Typescript Arrays Based on Other Values

Uncertain about how to phrase this inquiry. I have a specific situation where an object is involved. Key1 represents the name, while key2 stands for options. The type of options is determined by the value of the name. The existing solution works fine than ...

Attempting to successfully upload this Angular 7 form to my TypeScript code. Making use of ngForm and [(ngModel)] to achieve this

I am having trouble passing form information using the onSubmit() function. It seems to be undefined when I try to execute it initially. Could there be a syntax error that I'm missing? <form class="gf-formbox" name="credentials" (ngSubmit)="onSubm ...

Angular filter by boolean values

Trying to create a filter based on user status, with "Active" for online and "Inactive" for offline. However, filtering by string is presenting challenges as typing "active" still shows offline users due to the presence of "inactive" within it. I am lookin ...

Tips for integrating React with Cloudinary using Typescript

I'm currently immersed in a TypeScript and React project. Attempting to implement react cloudinary for image uploading and URL retrieval. Encountering errors when trying to convert the provided JavaScript code to TypeScript (tsx). The sample code I w ...

Missing from the TypeScript compilation are Angular5's polyfills.ts and main.ts files

Here is the structure of my Angular5 project. https://i.stack.imgur.com/tmbE7.png Within both tsconfig.app.json and package.json, there is a common section: "include": [ "/src/main.ts", "/src/polyfills.ts" ] Despite trying various solu ...

Run each test individually, ensuring compatibility across all browsers

Currently, I am creating my E2E tests using testcafe with a test backend that does not have support for concurrency. This means that if two tests are running in parallel, the test backend crashes. When I run tests against a single browser, they are execut ...

arrange object array in descending order based on an object's numerical value within it (using Angular-Typescript)

Looking for assistance with sorting an array of user objects based on their scores. Each user object contains properties like userId, userName, and score. My goal is to create a leaderboard where the user with the highest score appears at the top, followed ...

Interacting with TypeScript properties

In my Angular 2 project, I have a specific object definition that includes properties for officeId, year, pageNumber, and pageSize. export class MyFilter { public officeId: string; public year: number; pageNumber: number; pageSize: number; ...

What is the method for activating a hook after a state change in a functional component?

I'm currently working on a custom pagination hook that interacts with an API to fetch data similar to React Query. The concept is straightforward. I aim to invoke this hook whenever a specific state (referred to as cursor) undergoes a change. Below i ...

Ensuring consistency between our front-end Typescript types and the corresponding types in our back-end source code

In our development process, we utilize Java for backend operations and TypeScript for frontend tasks, each residing in its own repository. Lately, I've made a shift in the way we handle data exchange between server and client. Instead of sending indi ...

Can you identify the type of component being passed in a Higher Order Component?

I am currently in the process of converting a protectedRoute HOC from Javascript to TypeScript while using AWS-Amplify. This higher-order component will serve as a way to secure routes that require authentication. If the user is not logged in, they will b ...

A Typescript function that safely extracts object properties using currying

My goal is to add typings for the prop function. It follows a curried pattern where the key is passed first and the object is passed last. I attempted an approach where I partially apply it to a key, then try to restrict it to an object containing that pr ...

Example of Signature in TypeScript Function Declaration

While going through this documentation, I found myself puzzled by the concept of having a parameter that can be both an object and a function in JavaScript. type DescribableFunction = { description: string; (a: any): boolean; }; function doSomething( ...

The Angular 6 test command using npm has encountered a failure

I've been encountering a disconnect error while attempting to run Angular test cases. With over 1500 test cases written, it appears that the sheer volume may be causing this issue. Is there a way to resolve the disconnect error when running such a lar ...

Encountering TypeScript error TS2339 while mocking a React component with Jest

When attempting to mock a React component using Jest, I encountered an issue where TypeScript was not recognizing the mocked methods and showing a TS2339 error. Below is the test code snippet causing the problem: jest.mock('./features/News/News' ...