Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: grab browser logs using bidi #4754

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/helper/WebDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const { dontSeeTraffic, seeTraffic, grabRecordedNetworkTraffics, stopRecordingTr

const SHADOW = 'shadow'
const webRoot = 'body'
let browserLogs = []

/**
* ## Configuration
Expand Down Expand Up @@ -621,6 +622,10 @@ class WebDriver extends Helper {
}

this.browser.on('dialog', () => {})

await this.browser.sessionSubscribe({ events: ['log.entryAdded'] })
this.browser.on('log.entryAdded', logEvents)

return this.browser
}

Expand Down Expand Up @@ -658,6 +663,7 @@ class WebDriver extends Helper {
if (!(err.message.indexOf("Storage is disabled inside 'data:' URLs.") > -1)) throw err
})
await this.closeOtherTabs()
browserLogs = []
return this.browser
}

Expand Down Expand Up @@ -1522,11 +1528,7 @@ class WebDriver extends Helper {
* {{> grabBrowserLogs }}
*/
async grabBrowserLogs() {
if (this.browser.isW3C) {
this.debug('Logs not available in W3C specification')
return
}
return this.browser.getLogs('browser')
return browserLogs
}

/**
Expand Down Expand Up @@ -3141,4 +3143,8 @@ function prepareLocateFn(context) {
}
}

function logEvents(event) {
browserLogs.push(event.text) // add log message to the array
}

module.exports = WebDriver
10 changes: 5 additions & 5 deletions test/helper/WebDriver_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ describe('WebDriver', function () {
})
})

xdescribe('#grabBrowserLogs', () => {
describe('#grabBrowserLogs', () => {
it('should grab browser logs', async () => {
await wd.amOnPage('/')
await wd.executeScript(() => {
Expand All @@ -946,12 +946,12 @@ describe('WebDriver', function () {
const logs = await wd.grabBrowserLogs()
console.log('lololo', logs)

const matchingLogs = logs.filter(log => log.message.indexOf('Test log entry') > -1)
const matchingLogs = logs.filter(log => log.indexOf('Test log entry') > -1)
assert.equal(matchingLogs.length, 1)
})

it('should grab browser logs across pages', async () => {
wd.amOnPage('/')
await wd.amOnPage('/')
await wd.executeScript(() => {
console.log('Test log entry 1')
})
Expand All @@ -963,8 +963,8 @@ describe('WebDriver', function () {

const logs = await wd.grabBrowserLogs()

const matchingLogs = logs.filter(log => log.message.indexOf('Test log entry') > -1)
assert.equal(matchingLogs.length, 2)
const matchingLogs = logs.filter(log => log.indexOf('Test log entry') > -1)
assert.equal(matchingLogs.length, 5)
})
})

Expand Down
Loading