cypress
cypress copied to clipboard
Intercepting ajax request with "async: false" cause cypress to freeze
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
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
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
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 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.
Any news on this?
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.
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
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.
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.