Cypress can perform interception matching with RouteMatcher. Specifying the method and URL to match is also achieved by the routematcher object into .intercept().
cy.intercept({ method: 'GET', url: '**/users' })
cy.intercept('GET', '**/users')
- Both of this yield the same result.
cy.intercept({method:'GET', URL:'https://reqres.in/api/users?page=2'})
cy.visit('https://reqres.in/')
cy.xpath('//span[@class="URL"]').click()
Or
cy.intercept('GET','https://reqres.in/api/users?page=2')
cy.visit('https://reqres.in/')
cy.xpath('//span[@class="url"]').click()
Both will yield the GET method response in dom.
cy.intercept({
pathname: '/endpoint',
query: {
q: 'query',
},
}).as('alias')
- We can also match the type of request with pathname and query as a parameter.
cy.intercept({
pathname: '/api/users?page=2',
query: {
q: 'michael',
},
}).as('searchForTerms')
cy.visit('https://reqres.in/')
cy.xpath('//span[@class="url"]').click()
- We can also match URLs with this method.
cy.intercept(
{
url: /^http:\/\/api\.example\.com\/.*\/(edit|save)/,
headers: {
'x-requested-with': 'Client',
},
}
})
- This RegExp matches any URL beginning with'http://api.example.com/' and ending with '/edit' or '/save'.
- It must also contain this header 'Client'.
cy.intercept('/error', { times: 1 }, { forceNetworkError: true })
- This will cause on request to '/error.'
- Receive a network error, and subsequent requests will not match with this.
- Log in to post comments