cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Intercepting ajax request with "async: false" cause cypress to freeze

Open Donaab opened this issue 2 years ago • 6 comments

Current behavior

We have code (JS/jQuery) invoking the POST request:

$.ajax({
    type: 'POST',
    url: '/web/api2/v1/term/{0}'.format(tbId),
    dataType: 'json',
    contentType: 'application/json',
    cache: false,
    async: false,
    data: JSON.stringify({
        conceptId: conceptId,
        lang: input.attr('name'),
        text: input.val()
    }),
    success: function (data) {
        ...
    },
    error: function (req, status) {
        ...
    }
});

When we run tests on production we then intercept all the calls to add additional header and only on the request triggered like this cypress completely freeze. Screenshots and Debug logz are attached. Logs.txt caprese 2022-06-19 08-22-05

Desired behavior

Cypress should intercept the request.

Test code to reproduce

application code (JS/jQuery) invoking the POST request

$.ajax({
    type: 'POST',
    url: '/web/api2/v1/term/{0}'.format(tbId),
    dataType: 'json',
    contentType: 'application/json',
    cache: false,
    async: false,
    data: JSON.stringify({
        conceptId: conceptId,
        lang: input.attr('name'),
        text: input.val()
    }),
    success: function (data) {
        ...
    },
    error: function (req, status) {
        ...
    }
});

test.spec.js

cy.get('#tb-insert-button').click();
  cy.get('#tb-insert-tbody').find(`input[type="text"][name=${targetLang}]`).type(term);
  cy.get('#tb-insert-submit').click();`

index.js

`beforeEach('Adds headers to web012 env', () => {
  if (Cypress.env('ENVIRONMENT') === 'web012') {
    cy.log(Cypress.env('ENVIRONMENT'));
    cy.intercept(/^https:\/\/some\.domain\.com\/web\/(?!assets).*/, (req) => {
      req.headers['X-Memsource-Web-Canary-Request'] = 'any';
    });
  }
});

Cypress Version

9.6.2

Other

No response

Donaab avatar Jun 19 '22 06:06 Donaab

This happens to me also. This is my ajax function:

var request = $.ajax({ 
		url: "../User/AjaxFindUser", 
		type: "POST", 
		data: "", 
		dataType: "json",
		async: false
}); 

And this is my interceptor:

cy.intercept('http://localhost' + '/**', (req) => {
            req.headers['GID'] = 'F0000001';
        });

As reported above, the request stays at pending and the test runner freezes completely, I cannot click anything and I have to close it. I'm using Cypress v10.2.0

imagen

jesusmsr avatar Jun 23 '22 13:06 jesusmsr

We believe the reason is the async: false which according to the documentation https://api.jquery.com/jquery.ajax/#jQuery-ajax-settings "Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active." What is exactly happens with cypress.

Donaab avatar Jun 23 '22 19:06 Donaab

@Donaab thank you for the report, I can reproduce this. The interception of synchronous requests seems to lock up cypress indefinitely. If I don't intercept the synchronous request, the app will hang while the request executes but continues functioning after the request completes. I can also safely intercept the request if I provide a stubbed response.

I also put together a small reproduction as part of my investigation: https://github.com/tbiethman/cypress-test-tiny/tree/sync-request-intercept

As far as I can tell, when the app makes the synchronous request, it's not able to handle events emitted from the middleware that allow the interception to be possible, which in result never allows the request to complete. More investigation will be required to determine how we should be handling these types of requests; we definitely shouldn't allow Cypress to get into a scenario that we know will cause a hang.

tbiethman avatar Jun 23 '22 23:06 tbiethman

Any news on this?

dloez avatar Jul 15 '22 06:07 dloez

Hi @Donaab, I'm reviewing this with the team this morning and it looks like we wouldn't be able to handle this in the way you're likely hoping for. Cypress is blocked by the synchronous request and cannot modify the headers during that intercept. We could, however, at least handle this situation a bit more gracefully by not hanging (possibly through some sort of fast failure). This is not new behavior, but how things would have always worked in this situation.

rockhold avatar Aug 02 '22 14:08 rockhold

Is there any way to skip interception of async:false requests? I'm trying to follow this sample code for throttling all requests, but I keep getting stuck with pending requests and Cypress frozen. https://docs.cypress.io/api/commands/intercept#Throttle-or-delay-response-all-incoming-responses

nogwater avatar Nov 22 '22 21:11 nogwater

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

cypress-app-bot avatar May 22 '23 19:05 cypress-app-bot

Also ran into this and wasted an incredible amount of time because cypress does not at least fail gracefully for this case. Even if this is not solved such that synchronous requests can be intercepted, cypress should be fixed to not hang and at least report a timeout or something on the bad request.

sterling avatar Feb 29 '24 18:02 sterling