Passer au contenu principal

requestOnce

Only change request parameter once with given overwrite for the next request. You can call requestOnce multiple consecutive times and it will apply the overwrites in order. If you only use requestOnce and the resource is called more times a mock has been defined than it defaults back to the original resource.

Usage
mock.requestOnce({ header, cookies, method, url, header, statusCode, fetchResponse })
Parameters
NameTypeDetails
overwritesMockOverwritepayload to overwrite the response
overwrites.headerRecord<string, string>overwrite specific headers
overwrites.cookiesRecord<string, string>overwrite request cookies
overwrites.methodstringoverwrite request method
overwrites.urlstringoverwrite request url to initiate a redirect
params
optional
MockResponseParamsadditional respond parameters to overwrite
params.header
optional
Objectoverwrite specific headers
params.statusCode
optional
Numberoverwrite response status code
params.fetchResponse
optional
Booleanfetch real response before responding with mocked data
Example
respond.js
it('adds different auth headers to my API requests', async () => {
const mock = await browser.mock('https://application.com/api', {
method: 'get'
})

mock.requestOnce({
headers: { 'Authorization': 'Bearer token' }
})
mock.requestOnce({
headers: { 'Authorization': 'Another bearer token' }
})

await browser.url('https://application.com')
// ...
})

Welcome! How can I help?

WebdriverIO AI Copilot