Vai al contenuto principale

mock

Simula la risposta di una richiesta. Puoi definire un mock basato su un modello di corrispondenza URLPattern e i corrispondenti header e codice di stato. Chiamare il metodo mock restituisce un oggetto stub che puoi utilizzare per modificare la risposta della risorsa web.

Con l'oggetto stub puoi quindi restituire una risposta personalizzata o far fallire la richiesta.

Ci sono 3 modi per modificare la risposta:

  • restituire un oggetto JSON personalizzato (per simulare richieste API)
  • sostituire la risorsa web con un file locale (servire un file JavaScript modificato) o
  • reindirizzare la risorsa a un URL diverso
informazione

Nota che l'utilizzo del comando mock richiede il supporto per WebDriver Bidi. Questo è generalmente il caso quando si eseguono test localmente in un browser basato su Chromium o su Firefox, così come se si utilizza Selenium Grid v4 o superiore. Se esegui test nel cloud, assicurati che il tuo provider cloud supporti WebDriver Bidi.

informazione

L'URLPattern è una tecnologia sperimentale e non ancora supportata in alcuni ambienti, ad esempio Node.js. Consigliamo di importare un polyfill fino a quando la funzione non sarà più ampiamente supportata.

Utilizzo
browser.mock(url, { method, requestHeaders, responseHeaders, postData, statusCode })
Parametri
NomeTipoDettagli
urlStringurl da simulare
filterOptions
opzionale
MockFilterOptionsfiltra la risorsa mock con opzioni aggiuntive
filterOptions.method
opzionale
String, Functionfiltra la risorsa per metodo HTTP
filterOptions.requestHeaders
opzionale
Object, Functionfiltra la risorsa per specifici header di richiesta
filterOptions.responseHeaders
opzionale
Object, Functionfiltra la risorsa per specifici header di risposta
filterOptions.postData
opzionale
String, Functionfiltra la risorsa per i dati postData della richiesta
filterOptions.statusCode
opzionale
Number, Functionfiltra la risorsa per il codice di stato della risposta
Esempio
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,
requestHeaders: { '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,
requestHeaders: (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://webdriver.io')
await browser.url('https://google.com')
console.log(await browser.getTitle()) // returns "WebdriverIO · Next-gen browser and mobile automation test framework for Node.js"
})
Restituisce
  • <Mock> return: un oggetto mock per modificare la risposta

Welcome! How can I help?

WebdriverIO AI Copilot