메인 컨텐츠로 건너뛰기

브라우저 로그

테스트를 실행할 때 브라우저는 관심이 있거나 확인하고 싶은 중요한 정보를 로그로 남길 수 있습니다.

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