From d912be8f325d3bed9758140b50599ee0619f1929 Mon Sep 17 00:00:00 2001 From: Rajendra kadam Date: Thu, 1 Oct 2020 14:29:12 +0530 Subject: [PATCH] Allow to listen for JavaScript exceptions (#8753) * Add cdp js exception event handler * Add cdp js exception event handler and test * Remove newline --- .../node/selenium-webdriver/chromium.js | 23 +++++++++++++++++++ .../test/chrome/devtools_test.js | 14 +++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/javascript/node/selenium-webdriver/chromium.js b/javascript/node/selenium-webdriver/chromium.js index 47aafa99a2f46..1743b9e344571 100644 --- a/javascript/node/selenium-webdriver/chromium.js +++ b/javascript/node/selenium-webdriver/chromium.js @@ -824,6 +824,29 @@ class Driver extends webdriver.WebDriver { ) } + /** + * + * @param connection + * @param callback + * @returns {Promise} + */ + 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. * diff --git a/javascript/node/selenium-webdriver/test/chrome/devtools_test.js b/javascript/node/selenium-webdriver/test/chrome/devtools_test.js index 265df39c94010..285f488864efe 100644 --- a/javascript/node/selenium-webdriver/test/chrome/devtools_test.js +++ b/javascript/node/selenium-webdriver/test/chrome/devtools_test.js @@ -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 () {