メインコンテンツにスキップ

ブラウザログ

テストを実行する際、ブラウザは関心のある重要な情報をログに記録したり、アサーションの対象としたい情報を出力したりすることがあります。

WebDriver Bidiを使用する場合(WebdriverIOがブラウザを自動化するデフォルトの方法)、ブラウザから送信されるイベントを購読できます。ログイベントの場合は、log.entryAdded'をリッスンします。例:

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))

テストでは、ログイベントを配列にプッシュして、アクションが完了したらその配列に対してアサーションを行うことができます:

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)
})
})

この方法を使用して、エラーメッセージを取得し、アプリケーションにエラーが発生したかどうかを確認できることに注意してください。

Welcome! How can I help?

WebdriverIO AI Copilot