diff --git a/javascript/node/selenium-webdriver/firefox.js b/javascript/node/selenium-webdriver/firefox.js index b76a42fda5e2b..81a2d6fc77dad 100644 --- a/javascript/node/selenium-webdriver/firefox.js +++ b/javascript/node/selenium-webdriver/firefox.js @@ -411,6 +411,13 @@ class Options extends Capabilities { } return this } + + /** + * Enables moz:debuggerAddress for firefox cdp + */ + enableDebugger() { + return this.set('moz:debuggerAddress', true) + } } /** diff --git a/javascript/node/selenium-webdriver/lib/webdriver.js b/javascript/node/selenium-webdriver/lib/webdriver.js index 59f668ab3afbd..364c05c45c899 100644 --- a/javascript/node/selenium-webdriver/lib/webdriver.js +++ b/javascript/node/selenium-webdriver/lib/webdriver.js @@ -36,6 +36,7 @@ const fs = require('fs') const { Capabilities } = require('./capabilities') const path = require('path') const { NoSuchElementError } = require('./error') +const cdpTargets = ['page', 'browser'] // Capability names that are defined in the W3C spec. const W3C_CAPABILITY_NAMES = new Set([ @@ -1189,7 +1190,7 @@ class WebDriver { * Creates a new WebSocket connection. * @return {!Promise} A new CDP instance. */ - async createCDPConnection() { + async createCDPConnection(target) { const caps = await this.getCapabilities() const seCdp = caps['map_'].get('se:cdp') const vendorInfo = @@ -1198,8 +1199,7 @@ class WebDriver { caps['map_'].get('moz:debuggerAddress') || new Map() const debuggerUrl = seCdp || vendorInfo['debuggerAddress'] || vendorInfo - this._wsUrl = await this.getWsUrl(debuggerUrl) - + this._wsUrl = await this.getWsUrl(debuggerUrl, target, caps) return new Promise((resolve, reject) => { try { this._wsConnection = new WebSocket(this._wsUrl) @@ -1222,19 +1222,38 @@ class WebDriver { /** * Retrieves 'webSocketDebuggerUrl' by sending a http request using debugger address * @param {string} debuggerAddress + * @param target + * @param caps * @return {string} Returns parsed webSocketDebuggerUrl obtained from the http request */ - async getWsUrl(debuggerAddress) { + async getWsUrl(debuggerAddress, target, caps) { + if (target && cdpTargets.indexOf(target.toLowerCase()) === -1) { + throw new error.InvalidArgumentError('invalid target value') + } + if (debuggerAddress.match(/\/se\/cdp/)) { - return debuggerAddress + return debuggerAddress; + } + + let path + if (target === 'page' && caps['map_'].get('browserName')!=='firefox' ){ + path = '/json' + } else if(target === 'page' && caps['map_'].get('browserName')==='firefox'){ + path = '/json/list' + } + else { + path = '/json/version' } - const path = '/json/version' let request = new http.Request('GET', path) let client = new http.HttpClient('http://' + debuggerAddress) let response = await client.send(request) - return JSON.parse(response.body)['webSocketDebuggerUrl'] + if (target.toLowerCase() === 'page') { + return JSON.parse(response.body)[0]['webSocketDebuggerUrl'] + } else { + return JSON.parse(response.body)['webSocketDebuggerUrl'] + } } /** diff --git a/javascript/node/selenium-webdriver/test/chrome/devtools_test.js b/javascript/node/selenium-webdriver/test/chrome/devtools_test.js index 9ec9dd8342a28..afa6b05a46834 100644 --- a/javascript/node/selenium-webdriver/test/chrome/devtools_test.js +++ b/javascript/node/selenium-webdriver/test/chrome/devtools_test.js @@ -73,14 +73,14 @@ test.suite( }) it('sends Page.enable command using devtools', async function () { - const cdpConnection = await driver.createCDPConnection() + const cdpConnection = await driver.createCDPConnection('page') cdpConnection.execute('Page.enable', 1, {}, function (_res, err) { assert(!err) }) }) it('sends Network and Page command using devtools', async function () { - const cdpConnection = await driver.createCDPConnection() + const cdpConnection = await driver.createCDPConnection('page') cdpConnection.execute('Network.enable', 1, {}, function (_res, err) { assert(!err) }) @@ -97,7 +97,7 @@ test.suite( describe('JS CDP events', function () { it('calls the event listener for console.log', async function () { - const cdpConnection = await driver.createCDPConnection() + const cdpConnection = await driver.createCDPConnection('page') await driver.onLogEvent(cdpConnection, function (event) { assert.strictEqual(event['args'][0]['value'], 'here') }) @@ -105,7 +105,7 @@ test.suite( }) it('calls the event listener for js exceptions', async function () { - const cdpConnection = await driver.createCDPConnection() + const cdpConnection = await driver.createCDPConnection('page') await driver.onLogException(cdpConnection, function (event) { assert.strictEqual( event['exceptionDetails']['stackTrace']['callFrames'][0][ @@ -122,7 +122,7 @@ test.suite( describe('JS DOM events', function () { it('calls the event listener on dom mutations', async function () { - const cdpConnection = await driver.createCDPConnection() + const cdpConnection = await driver.createCDPConnection('page') await driver.logMutationEvents(cdpConnection, function (event) { assert.strictEqual(event['attribute_name'], 'style') assert.strictEqual(event['current_value'], '') @@ -140,7 +140,7 @@ test.suite( describe('Basic Auth Injection', function () { it('denies entry if username and password do not match', async function () { - const pageCdpConnection = await driver.createCDPConnection() + const pageCdpConnection = await driver.createCDPConnection('page') await driver.register('random', 'random', pageCdpConnection) await driver.get(fileServer.Pages.basicAuth) @@ -149,7 +149,7 @@ test.suite( }) it('grants access if username and password are a match', async function () { - const pageCdpConnection = await driver.createCDPConnection() + const pageCdpConnection = await driver.createCDPConnection('page') await driver.register('genie', 'bottle', pageCdpConnection) await driver.get(fileServer.Pages.basicAuth) diff --git a/javascript/node/selenium-webdriver/test/devtools_test.js b/javascript/node/selenium-webdriver/test/devtools_test.js index 461b19e55feea..77ebc2b7395b4 100644 --- a/javascript/node/selenium-webdriver/test/devtools_test.js +++ b/javascript/node/selenium-webdriver/test/devtools_test.js @@ -35,14 +35,14 @@ suite( afterAll(async () => await driver.quit()) it('sends Page.enable command using devtools', async function () { - const cdpConnection = await driver.createCDPConnection() + const cdpConnection = await driver.createCDPConnection('page') cdpConnection.execute('Page.enable', 1, {}, function (_res, err) { assert(!err) }) }) it('sends Network and Page command using devtools', async function () { - const cdpConnection = await driver.createCDPConnection() + const cdpConnection = await driver.createCDPConnection('page') cdpConnection.execute('Network.enable', 1, {}, function (_res, err) { assert(!err) }) @@ -59,7 +59,7 @@ suite( describe('JS CDP events', function () { it('calls the event listener for console.log', async function () { - const cdpConnection = await driver.createCDPConnection() + const cdpConnection = await driver.createCDPConnection('page') await driver.onLogEvent(cdpConnection, function (event) { assert.strictEqual(event['args'][0]['value'], 'here') }) @@ -67,7 +67,7 @@ suite( }) it('calls the event listener for js exceptions', async function () { - const cdpConnection = await driver.createCDPConnection() + const cdpConnection = await driver.createCDPConnection('page') await driver.onLogException(cdpConnection, function (event) { assert.strictEqual( event['exceptionDetails']['stackTrace']['callFrames'][0][ @@ -84,7 +84,7 @@ suite( describe('JS DOM events', function () { it('calls the event listener on dom mutations', async function () { - const cdpConnection = await driver.createCDPConnection() + const cdpConnection = await driver.createCDPConnection('page') await driver.logMutationEvents(cdpConnection, function (event) { assert.strictEqual(event['attribute_name'], 'style') assert.strictEqual(event['current_value'], '') @@ -103,7 +103,7 @@ suite( describe('Basic Auth Injection', function () { ignore(browsers(Browser.SAFARI, Browser.FIREFOX)). it('denies entry if username and password do not match', async function () { - const pageCdpConnection = await driver.createCDPConnection() + const pageCdpConnection = await driver.createCDPConnection('page') await driver.register('random', 'random', pageCdpConnection) await driver.get(fileServer.Pages.basicAuth) @@ -116,7 +116,7 @@ suite( ignore(browsers(Browser.SAFARI, Browser.FIREFOX)). it('grants access if username and password are a match', async function () { - const pageCdpConnection = await driver.createCDPConnection() + const pageCdpConnection = await driver.createCDPConnection('page') await driver.register('genie', 'bottle', pageCdpConnection) await driver.get(fileServer.Pages.basicAuth) @@ -128,7 +128,7 @@ suite( describe("Network Interception", function () { ignore(browsers(Browser.SAFARI, Browser.FIREFOX)). it('Allows network requests to be captured and mocked', async function () { - const connection = await driver.createCDPConnection() + const connection = await driver.createCDPConnection('page') let url = fileServer.whereIs("/cheese") let httpResponse = new HttpResponse(url) httpResponse.addHeaders("Content-Type", "UTF-8")