Skip to content

Commit

Permalink
Allow to listen for JavaScript exceptions (#8753)
Browse files Browse the repository at this point in the history
* Add cdp js exception event handler

* Add cdp js exception event handler and test

* Remove newline
  • Loading branch information
raju249 committed Oct 1, 2020
1 parent 4839225 commit d912be8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
23 changes: 23 additions & 0 deletions javascript/node/selenium-webdriver/chromium.js
Expand Up @@ -824,6 +824,29 @@ class Driver extends webdriver.WebDriver {
)
}

/**
*
* @param connection
* @param callback
* @returns {Promise<void>}
*/
async onLogException(connection, callback) {
await connection.execute('Runtime.enable', 1, {}, null)

this._wsConnection.on('message', (message) => {
const params = JSON.parse(message)

if (params.method === 'Runtime.exceptionThrown') {
const exceptionEventParams = params['params']
let event = {
exceptionDetails: exceptionEventParams['exceptionDetails'],
timestamp: new Date(exceptionEventParams['timestamp']),
}

callback(event)
}
})
}
/**
* Sends a DevTools command to change the browser's download directory.
*
Expand Down
14 changes: 12 additions & 2 deletions javascript/node/selenium-webdriver/test/chrome/devtools_test.js
Expand Up @@ -95,14 +95,24 @@ test.suite(
)
})

describe('Console.log events', function () {
it('calls the event listener', async function () {
describe('JS CDP events', function () {
it('calls the event listener for console.log', async function () {
const cdpConnection = await driver.createCDPConnection('page')
await driver.onLogEvent(cdpConnection, function(event) {
assert.equal(event['args'][0]['value'], 'here')
})
await driver.executeScript('console.log("here")')
})

it('calls the event listener for js exceptions', async function () {
const cdpConnection = await driver.createCDPConnection('page')
await driver.onLogException(cdpConnection, function(event) {
assert.equal(event['exceptionDetails']['stackTrace']['callFrames'][0]['functionName'], 'onmouseover')
})
await driver.get(test.Pages.javascriptPage)
let element = driver.findElement({id: 'throwing-mouseover'})
await element.click()
})
})

describe('Basic Auth Injection', function () {
Expand Down

0 comments on commit d912be8

Please sign in to comment.