mock
Mock the response of a request. You can define a mock based on a matching glob and corresponding header and status code. Calling the mock method returns a stub object that you can use to modify the response of the web resource.
With the stub object you can then either return a custom response or have the request fail.
There are 3 ways to modify the response:
- return a custom JSON object (for stubbing API request)
- replace web resource with a local file (service a modified JavaScript file) or
- redirect resource to a different url
інформація
Note that using the mock
command requires support for Chrome DevTools protocol.
That support is given if you run tests locally in Chromium based browser or if
you use a Selenium Grid v4 or higher. This command can not be used when running
automated tests in the cloud. Find out more in the Automation Protocols section.
Usage
browser.mock(url, { method, headers, responseHeaders, postData, statusCode })
Parameters
Name | Type | Details |
---|---|---|
url | String , RegExp | url to mock |
filterOptions optional | MockFilterOptions | filter mock resource by additional options |
filterOptions.method | String , Function | filter resource by HTTP method |
filterOptions.headers | Object , Function | filter resource by specific request headers |
filterOptions.responseHeaders | Object , Function | filter resource by specific response headers |
filterOptions.postData | String , Function | filter resource by request postData |
filterOptions.statusCode | Number , Function | filter resource by response statusCode |