跳至主要內容

mock

模擬請求的回應。 您可以根據匹配的 glob 和對應的標頭和狀態碼來定義模擬。 呼叫模擬方法會傳回一個 stub 物件,您可以使用該物件來修改 Web 資源的回應。

使用 stub 物件,您可以傳回自訂回應或讓請求失敗。

有 3 種方式可以修改回應

  • 傳回自訂 JSON 物件(用於 stub API 請求)
  • 用本機檔案替換 Web 資源(提供修改後的 JavaScript 檔案)或
  • 將資源重新導向至不同的 URL
資訊

請注意,使用 mock 命令需要支援 Chrome DevTools 協定。 如果您在基於 Chromium 的瀏覽器中在本機執行測試,或者如果您使用 Selenium Grid v4 或更高版本,則會提供該支援。 在雲端執行自動化測試時,**無法**使用此命令。 在自動化協定章節中了解更多資訊。

使用方式
browser.mock(url, { method, headers, responseHeaders, postData, statusCode })
參數
名稱類型詳細資訊
url字串要模擬的 URL
filterOptions
選用
MockFilterOptions依其他選項篩選模擬資源
filterOptions.method字串函式依 HTTP 方法篩選資源
filterOptions.headers物件函式依特定請求標頭篩選資源
filterOptions.responseHeaders物件函式依特定回應標頭篩選資源
filterOptions.postData字串函式依請求 postData 篩選資源
filterOptions.statusCode數字函式依回應狀態碼篩選資源
範例
mock.js
it('should mock network resources', async () => {
// via static string
const userListMock = await browser.mock('**' + '/users/list')
// or as regular expression
const userListMock = await browser.mock(/https:\/\/(domainA|domainB)\.com\/.+/)
// you can also specifying the mock even more by filtering resources
// by request or response headers, status code, postData, e.g. mock only responses with specific
// header set and statusCode
const strictMock = await browser.mock('**', {
// mock all json responses
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
responseHeaders: { 'Cache-Control': 'no-cache' },
postData: 'foobar'
})

// comparator function
const apiV1Mock = await browser.mock('**' + '/api/v1', {
statusCode: (statusCode) => statusCode >= 200 && statusCode <= 203,
headers: (headers) => headers['Authorization'] && headers['Authorization'].startsWith('Bearer '),
responseHeaders: (headers) => headers['Impersonation'],
postData: (data) => typeof data === 'string' && data.includes('foo')
})
})

it('should modify API responses', async () => {
// filter by method
const todoMock = await browser.mock('**' + '/todos', {
method: 'get'
})

// mock an endpoint with a fixed fixture
todoMock.respond([{
title: 'Injected Todo',
order: null,
completed: false,
url: "http://todo-backend-express-knex.herokuapp.com/916"
}])

// respond with different status code or header
todoMock.respond([{
title: 'Injected Todo',
order: null,
completed: false,
url: "http://todo-backend-express-knex.herokuapp.com/916"
}], {
statusCode: 404,
headers: {
'x-custom-header': 'foobar'
}
})
})

it('should modify text assets', async () => {
const scriptMock = await browser.mock('**' + '/script.min.js')
scriptMock.respond('./tests/fixtures/script.js')
})

it('should redirect web resources', async () => {
const headerMock = await browser.mock('**' + '/header.png')
headerMock.respond('https://media.giphy.com/media/F9hQLAVhWnL56/giphy.gif')

const pageMock = await browser.mock('https://google.com/')
pageMock.respond('https://webdriverio.dev.org.tw')
await browser.url('https://google.com')
console.log(await browser.getTitle()) // returns "WebdriverIO · Next-gen browser and mobile automation test framework for Node.js"
})

歡迎!我能如何協助您?

WebdriverIO AI Copilot