Make multiple API calls with Jest-environment-Puppeteer

165 Views Asked by At

How to intercept multiple http requests (API calls) with jest & puppeteer environment? I have the following code but I get error Request already handled when I make more than one API call.

Code:

    export const apiPostMethodResponseStatusCheckUtil = async (
    apiObject: APIObject) => {                                                     
     let completeAPIURL = URLs.apiURL + apiObject.endpoint;                                       
     describe(apiObject.testDescribe, () => {
     let expectedResponse = 200;

    it(apiObject.testCaseMsg, async () => {
      console.log("API Request: " + completeAPIURL);
      console.log("API Payload: " + JSON.stringify(apiObject.apiPayload));
      await page.setRequestInterception(true);

      page.on("request", (interceptedRequest) => {
        var data = {
          method: "POST",
          postData: JSON.stringify(apiObject.apiPayload),
        };
        interceptedRequest.continue(data);
      });

      await page.goto(completeAPIURL, {
        waitUntil: "load",
        timeout: apiObject.responseTimeout,
      });

      page.on("response", async (response) => {
        if (response.url().includes(apiObject.includesInURL)) {
          console.log("API response status...." + response.status());
          expect(response.status()).toBe(expectedResponse);
        }
      });
    });
  });
};

How to disable request interception after each API call?

let apiBody1=new APIObject(page, "{entity}/getFirstSet", "getFirstSet", {"_from":0,"_size":200,"_sort":{"id":"asc"}}, 0, `set 1`, `Making API call for set 1 `);
// call util method
apiPostMethodResponseStatusCheckUtil(apiBody1)

let apiBody2=new APIObject(page, "{entity}/getSecondSet", "getSecondSet", {"_from":0,"_size":200,"_sort":{"name":"asc"}}, 0, `set 2`, `Making API call for set 2`);
    // call util method
    apiPostMethodResponseStatusCheckUtil(apiBody2)
0

There are 0 best solutions below