मुख्य कॉन्टेंट में जाएँ

respond

Always respond with same overwrite.

Usage
mock.respond(overwrites, { header, statusCode, fetchResponse })
Parameters
NameTypeDetails
overwritesMockOverwritepayload to overwrite the response
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('should demonstrate response overwrite with static data', async () => {
const mock = await browser.mock('https://todo-backend-express-knex.herokuapp.com/', {
method: 'get'
})

mock.respond([{
title: 'Injected (non) completed Todo',
order: null,
completed: false
}, {
title: 'Injected completed Todo',
order: null,
completed: true
}], {
statusCode: 200,
fetchResponse: true // default
})

await browser.url('https://todobackend.com/client/index.html?https://todo-backend-express-knex.herokuapp.com/')

await $('#todo-list li').waitForExist()
console.log(await $$('#todo-list li').map(el => el.getText()))
// outputs: "[ 'Injected (non) completed Todo', 'Injected completed Todo' ]"
})

it('should demonstrate response overwrite with dynamic data', async () => {
const mock = await browser.mock('https://todo-backend-express-knex.herokuapp.com/')

mock.respond((request) => {
if (request.body.username === 'test') {
return { ...request.body, foo: 'bar' }
}
return request.body
}, {
statusCode: () => 200,
headers: () => ({ foo: 'bar }),
fetchResponse: false // do not fetch real response
})
})

Welcome! How can I help?

WebdriverIO AI Copilot