Chuyển đến nội dung chính

Nhật ký trình duyệt

Khi chạy các bài kiểm thử, trình duyệt có thể ghi lại thông tin quan trọng mà bạn quan tâm hoặc muốn kiểm tra.

Khi sử dụng WebDriver Bidi, phương pháp mặc định mà WebdriverIO tự động hóa trình duyệt, bạn có thể đăng ký nhận các sự kiện từ trình duyệt. Đối với các sự kiện nhật ký, bạn cần lắng nghe log.entryAdded', ví dụ:

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

Trong một bài kiểm thử, bạn có thể đẩy các sự kiện nhật ký vào một mảng và kiểm tra mảng đó sau khi hành động của bạn hoàn tất, ví dụ:

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

Xin lưu ý rằng bạn có thể sử dụng phương pháp này để truy xuất thông báo lỗi và xác minh xem ứng dụng của bạn có gặp bất kỳ lỗi nào không.

Welcome! How can I help?

WebdriverIO AI Copilot