मुख्य सामग्री पर जाएं

respondOnce (केवल एक बार प्रतिक्रिया देना)

केवल एक बार दिए गए ओवरराइट के साथ प्रतिक्रिया दें। आप respondOnce को कई बार लगातार कॉल कर सकते हैं और यह आपके द्वारा अंतिम परिभाषित प्रतिक्रिया से शुरू होगा। यदि आप केवल respondOnce का उपयोग करते हैं और संसाधन को मॉक को परिभाषित करने से अधिक बार कॉल किया जाता है, तो यह वापस मूल संसाधन पर लौट जाता है।

उपयोग
mock.respondOnce(overwrites, { header, statusCode, fetchResponse })
पैरामीटर्स
नामप्रकारविवरण
overwritesMockOverwriteप्रतिक्रिया को ओवरराइट करने के लिए पेलोड
params
वैकल्पिक
MockResponseParamsओवरराइट करने के लिए अतिरिक्त प्रतिक्रिया पैरामीटर्स
params.header
वैकल्पिक
Objectविशिष्ट हेडर्स को ओवरराइट करें
params.statusCode
वैकल्पिक
Numberप्रतिक्रिया स्टेटस कोड को ओवरराइट करें
params.fetchResponse
वैकल्पिक
Booleanमॉक किए गए डेटा के साथ प्रतिक्रिया देने से पहले वास्तविक प्रतिक्रिया प्राप्त करें
उदाहरण
respondOnce.js
async function getToDos () {
await $('#todo-list li').waitForExist()
return $$('#todo-list li').map(el => el.getText())
}

it('should demonstrate the respondOnce command', async () => {
const mock = await browser.mock('https://todo-backend-express-knex.herokuapp.com/', {
method: 'get'
})

mock.respondOnce([{
title: '3'
}, {
title: '2'
}, {
title: '1'
}])

mock.respondOnce([{
title: '2'
}, {
title: '1'
}])

mock.respondOnce([{
title: '1'
}])

await browser.url('https://todobackend.com/client/index.html?https://todo-backend-express-knex.herokuapp.com/')
console.log(await getToDos()) // outputs [ '3', '2', '1' ]
await browser.url('https://todobackend.com/client/index.html?https://todo-backend-express-knex.herokuapp.com/')
console.log(await getToDos()) // outputs [ '2', '1' ]
await browser.url('https://todobackend.com/client/index.html?https://todo-backend-express-knex.herokuapp.com/')
console.log(await getToDos()) // outputs [ '1' ]
await browser.url('https://todobackend.com/client/index.html?https://todo-backend-express-knex.herokuapp.com/')
console.log(await getToDos()) // outputs actual resource response
})

Welcome! How can I help?

WebdriverIO AI Copilot