Skip to content

Commit

Permalink
[js] Fixes cdp for Chrome and Firefox (#9909)
Browse files Browse the repository at this point in the history
* replacing targets page and browser for ff and chrome

* Placing await

* Updating devtools_tests

* Removing extra checkes

Co-authored-by: David Burns <david.burns@theautomatedtester.co.uk>
  • Loading branch information
harsha509 and AutomatedTester committed Oct 13, 2021
1 parent 614a408 commit 3782f97
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
7 changes: 7 additions & 0 deletions javascript/node/selenium-webdriver/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ class Options extends Capabilities {
}
return this
}

/**
* Enables moz:debuggerAddress for firefox cdp
*/
enableDebugger() {
return this.set('moz:debuggerAddress', true)
}
}

/**
Expand Down
33 changes: 26 additions & 7 deletions javascript/node/selenium-webdriver/lib/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -1189,7 +1190,7 @@ class WebDriver {
* Creates a new WebSocket connection.
* @return {!Promise<resolved>} A new CDP instance.
*/
async createCDPConnection() {
async createCDPConnection(target) {
const caps = await this.getCapabilities()
const seCdp = caps['map_'].get('se:cdp')
const vendorInfo =
Expand All @@ -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)
Expand All @@ -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']
}
}

/**
Expand Down
14 changes: 7 additions & 7 deletions javascript/node/selenium-webdriver/test/chrome/devtools_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -97,15 +97,15 @@ 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')
})
await driver.executeScript('console.log("here")')
})

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][
Expand All @@ -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'], '')
Expand All @@ -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)
Expand All @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions javascript/node/selenium-webdriver/test/devtools_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -59,15 +59,15 @@ 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')
})
await driver.executeScript('console.log("here")')
})

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][
Expand All @@ -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'], '')
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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")
Expand Down

0 comments on commit 3782f97

Please sign in to comment.