How to intercept cy.request? Is it possible? #18706
Replies: 6 comments 2 replies
-
Hello guys. Any help here? |
Beta Was this translation helpful? Give feedback.
-
I do not believe you can debug a cy.request with an intercept. |
Beta Was this translation helpful? Give feedback.
-
I do have a similar question though. Is it possible to intercept a api call to a 3rd party service when doing a cy.request.
|
Beta Was this translation helpful? Give feedback.
-
Just to add to this, I also have a few use cases that'd benefit from intercepting manual request calls. |
Beta Was this translation helpful? Give feedback.
-
same here, would be very helpful to have that functionality |
Beta Was this translation helpful? Give feedback.
-
I'm not sure why you may need to intercept a request made by the test, but But if you want to intercept a request You'll have to set the intercept and then use cy.intercept('POST', 'http://XXXXXX/api/_testing/createUser')
.as('user')
.then(() => {
fetch('http://XXXXXX/api/_testing/createUser' , {method: 'POST'})
.then((r) => r.json())
.then((body) => {
// make any assertions here
})
})
cy.wait('@user').its('response.body') You can also wrap the cy.intercept('POST', 'http://XXXXXX/api/_testing/createUser')
.as('user')
.then(() => {
cy.wrap(fetch('http://XXXXXX/api/_testing/createUser' , {method: 'POST'}))
.invoke('json')
.should('have.property', 'user')
})
cy.wait('@user') You can also use the window.fetch. cy.intercept('POST', 'http://XXXXXX/api/_testing/createUser')
.as('user')
cy.window()
.invoke('fetch', 'http://XXXXXX/api/_testing/createUser', {method: 'POST'}) // not exactly sure how to include method in this example
.invoke('json')
.then((body) => {
cy.get('@user')
.its('response.body')
.should('deep.equal', body)
}) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
I am trying to intercept a request from cy.request(). But the URL does not match. Here is a simple example.
The endpoint is a helper method in PHP to create the user. The response is returning as expected.
Is it possible to intercept a request from cy.request()?
If not, is there any workaround for this process? I am using variables but it is not advisable.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions