跳至主要內容

僅回應一次

僅使用給定的覆寫來回應一次。您可以連續多次呼叫 respondOnce,它將從您最後定義的回應開始。如果您只使用 respondOnce,並且資源被呼叫的次數超過了定義的模擬次數,則它將恢復為原始資源。

用法
mock.respondOnce(overwrites, { header, statusCode, fetchResponse })
參數
名稱類型詳細資料
覆寫模擬覆寫覆寫回應的酬載
參數
選填
模擬回應參數覆寫的其他回應參數
params.header
選填
物件覆寫特定標頭
params.statusCode
選填
數字覆寫回應狀態碼
params.fetchResponse
選填
布林值在使用模擬資料回應之前,先擷取實際回應
範例
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
})

歡迎!我能如何幫您?

WebdriverIO AI Copilot