Vai al contenuto principale

Log del Browser

Quando esegui i test, il browser potrebbe registrare informazioni importanti che potrebbero interessarti o su cui potresti voler fare delle asserzioni.

Quando utilizzi WebDriver Bidi, che è il modo predefinito con cui WebdriverIO automatizza il browser, puoi sottoscriverti agli eventi provenienti dal browser. Per gli eventi di log, devi ascoltare log.entryAdded', ad esempio:

await browser.sessionSubscribe({ events: ['log.entryAdded'] })

/**
* returns: {"type":"console","method":"log","realm":null,"args":[{"type":"string","value":"Hello Bidi"}],"level":"info","text":"Hello Bidi","timestamp":1657282076037}
*/
browser.on('log.entryAdded', (entryAdded) => console.log('received %s', entryAdded))

In un test puoi semplicemente aggiungere gli eventi di log a un array e verificare quell'array una volta completata l'azione, ad esempio:

import type { local } from 'webdriver'

describe('should log when doing a certain action', () => {
const logs: string[] = []

function logEvents (event: local.LogEntry) {
logs.push(event.text) // add log message to the array
}

before(async () => {
await browser.sessionSubscribe({ events: ['log.entryAdded'] })
browser.on('log.entryAdded', logEvents)
})

it('should trigger the console event', () => {
// trigger the browser send a message to the console
...

// assert if log was captured
expect(logs).toContain('Hello Bidi')
})

// clean up listener afterwards
after(() => {
browser.off('log.entryAdded', logEvents)
})
})

Tieni presente che puoi utilizzare questo metodo per recuperare i messaggi di errore e verificare se la tua applicazione ha riscontrato errori.

Welcome! How can I help?

WebdriverIO AI Copilot