Intercept the request made on a particular URL and mock the response.

mockNetworkResponse() is only available when using Chrome or Edge drivers.

Usage

.mockNetworkResponse(urlToIntercept, {status, headers, body}, [callback])

Parameters

Name Type description
urlToIntercept string

URL to intercept and mock the response from.

response object

Response to return. Defaults: {status: 200, headers: {}, body: ''}.

callback
Optional
function

Callback function to be called when the command finishes.

Example

 describe('mock network response', function() {
   it('intercepts the request made to Google search and mocks its response', function() {
     browser
       .mockNetworkResponse('https://www.google.com/', {
         status: 200,
         headers: {
           'Content-Type': 'UTF-8'
         },
         body: 'Hello there!'
       })
       .navigateTo('https://www.google.com/')
       .pause(2000);
   });
 });

Recommended content