Skip to content

Commit 502aafa

Browse files
harsha509AutomatedTester
authored andcommitted
[js] Fixes cdp for Chrome and Firefox (SeleniumHQ#9909)
* replacing targets page and browser for ff and chrome * Placing await * Updating devtools_tests * Removing extra checkes Co-authored-by: David Burns <[email protected]>
1 parent 4a0b3b6 commit 502aafa

File tree

4 files changed

+48
-22
lines changed

4 files changed

+48
-22
lines changed

javascript/node/selenium-webdriver/firefox.js

+7
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,13 @@ class Options extends Capabilities {
411411
}
412412
return this
413413
}
414+
415+
/**
416+
* Enables moz:debuggerAddress for firefox cdp
417+
*/
418+
enableDebugger() {
419+
return this.set('moz:debuggerAddress', true)
420+
}
414421
}
415422

416423
/**

javascript/node/selenium-webdriver/lib/webdriver.js

+26-7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const fs = require('fs')
3636
const { Capabilities } = require('./capabilities')
3737
const path = require('path')
3838
const { NoSuchElementError } = require('./error')
39+
const cdpTargets = ['page', 'browser']
3940

4041
// Capability names that are defined in the W3C spec.
4142
const W3C_CAPABILITY_NAMES = new Set([
@@ -1189,7 +1190,7 @@ class WebDriver {
11891190
* Creates a new WebSocket connection.
11901191
* @return {!Promise<resolved>} A new CDP instance.
11911192
*/
1192-
async createCDPConnection() {
1193+
async createCDPConnection(target) {
11931194
const caps = await this.getCapabilities()
11941195
const seCdp = caps['map_'].get('se:cdp')
11951196
const vendorInfo =
@@ -1198,8 +1199,7 @@ class WebDriver {
11981199
caps['map_'].get('moz:debuggerAddress') ||
11991200
new Map()
12001201
const debuggerUrl = seCdp || vendorInfo['debuggerAddress'] || vendorInfo
1201-
this._wsUrl = await this.getWsUrl(debuggerUrl)
1202-
1202+
this._wsUrl = await this.getWsUrl(debuggerUrl, target, caps)
12031203
return new Promise((resolve, reject) => {
12041204
try {
12051205
this._wsConnection = new WebSocket(this._wsUrl)
@@ -1222,19 +1222,38 @@ class WebDriver {
12221222
/**
12231223
* Retrieves 'webSocketDebuggerUrl' by sending a http request using debugger address
12241224
* @param {string} debuggerAddress
1225+
* @param target
1226+
* @param caps
12251227
* @return {string} Returns parsed webSocketDebuggerUrl obtained from the http request
12261228
*/
1227-
async getWsUrl(debuggerAddress) {
1229+
async getWsUrl(debuggerAddress, target, caps) {
1230+
if (target && cdpTargets.indexOf(target.toLowerCase()) === -1) {
1231+
throw new error.InvalidArgumentError('invalid target value')
1232+
}
1233+
12281234
if (debuggerAddress.match(/\/se\/cdp/)) {
1229-
return debuggerAddress
1235+
return debuggerAddress;
1236+
}
1237+
1238+
let path
1239+
if (target === 'page' && caps['map_'].get('browserName')!=='firefox' ){
1240+
path = '/json'
1241+
} else if(target === 'page' && caps['map_'].get('browserName')==='firefox'){
1242+
path = '/json/list'
1243+
}
1244+
else {
1245+
path = '/json/version'
12301246
}
12311247

1232-
const path = '/json/version'
12331248
let request = new http.Request('GET', path)
12341249
let client = new http.HttpClient('http://' + debuggerAddress)
12351250
let response = await client.send(request)
12361251

1237-
return JSON.parse(response.body)['webSocketDebuggerUrl']
1252+
if (target.toLowerCase() === 'page') {
1253+
return JSON.parse(response.body)[0]['webSocketDebuggerUrl']
1254+
} else {
1255+
return JSON.parse(response.body)['webSocketDebuggerUrl']
1256+
}
12381257
}
12391258

12401259
/**

javascript/node/selenium-webdriver/test/chrome/devtools_test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ test.suite(
7373
})
7474

7575
it('sends Page.enable command using devtools', async function () {
76-
const cdpConnection = await driver.createCDPConnection()
76+
const cdpConnection = await driver.createCDPConnection('page')
7777
cdpConnection.execute('Page.enable', 1, {}, function (_res, err) {
7878
assert(!err)
7979
})
8080
})
8181

8282
it('sends Network and Page command using devtools', async function () {
83-
const cdpConnection = await driver.createCDPConnection()
83+
const cdpConnection = await driver.createCDPConnection('page')
8484
cdpConnection.execute('Network.enable', 1, {}, function (_res, err) {
8585
assert(!err)
8686
})
@@ -97,15 +97,15 @@ test.suite(
9797

9898
describe('JS CDP events', function () {
9999
it('calls the event listener for console.log', async function () {
100-
const cdpConnection = await driver.createCDPConnection()
100+
const cdpConnection = await driver.createCDPConnection('page')
101101
await driver.onLogEvent(cdpConnection, function (event) {
102102
assert.strictEqual(event['args'][0]['value'], 'here')
103103
})
104104
await driver.executeScript('console.log("here")')
105105
})
106106

107107
it('calls the event listener for js exceptions', async function () {
108-
const cdpConnection = await driver.createCDPConnection()
108+
const cdpConnection = await driver.createCDPConnection('page')
109109
await driver.onLogException(cdpConnection, function (event) {
110110
assert.strictEqual(
111111
event['exceptionDetails']['stackTrace']['callFrames'][0][
@@ -122,7 +122,7 @@ test.suite(
122122

123123
describe('JS DOM events', function () {
124124
it('calls the event listener on dom mutations', async function () {
125-
const cdpConnection = await driver.createCDPConnection()
125+
const cdpConnection = await driver.createCDPConnection('page')
126126
await driver.logMutationEvents(cdpConnection, function (event) {
127127
assert.strictEqual(event['attribute_name'], 'style')
128128
assert.strictEqual(event['current_value'], '')
@@ -140,7 +140,7 @@ test.suite(
140140

141141
describe('Basic Auth Injection', function () {
142142
it('denies entry if username and password do not match', async function () {
143-
const pageCdpConnection = await driver.createCDPConnection()
143+
const pageCdpConnection = await driver.createCDPConnection('page')
144144

145145
await driver.register('random', 'random', pageCdpConnection)
146146
await driver.get(fileServer.Pages.basicAuth)
@@ -149,7 +149,7 @@ test.suite(
149149
})
150150

151151
it('grants access if username and password are a match', async function () {
152-
const pageCdpConnection = await driver.createCDPConnection()
152+
const pageCdpConnection = await driver.createCDPConnection('page')
153153

154154
await driver.register('genie', 'bottle', pageCdpConnection)
155155
await driver.get(fileServer.Pages.basicAuth)

javascript/node/selenium-webdriver/test/devtools_test.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ suite(
3535
afterAll(async () => await driver.quit())
3636

3737
it('sends Page.enable command using devtools', async function () {
38-
const cdpConnection = await driver.createCDPConnection()
38+
const cdpConnection = await driver.createCDPConnection('page')
3939
cdpConnection.execute('Page.enable', 1, {}, function (_res, err) {
4040
assert(!err)
4141
})
4242
})
4343

4444
it('sends Network and Page command using devtools', async function () {
45-
const cdpConnection = await driver.createCDPConnection()
45+
const cdpConnection = await driver.createCDPConnection('page')
4646
cdpConnection.execute('Network.enable', 1, {}, function (_res, err) {
4747
assert(!err)
4848
})
@@ -59,15 +59,15 @@ suite(
5959

6060
describe('JS CDP events', function () {
6161
it('calls the event listener for console.log', async function () {
62-
const cdpConnection = await driver.createCDPConnection()
62+
const cdpConnection = await driver.createCDPConnection('page')
6363
await driver.onLogEvent(cdpConnection, function (event) {
6464
assert.strictEqual(event['args'][0]['value'], 'here')
6565
})
6666
await driver.executeScript('console.log("here")')
6767
})
6868

6969
it('calls the event listener for js exceptions', async function () {
70-
const cdpConnection = await driver.createCDPConnection()
70+
const cdpConnection = await driver.createCDPConnection('page')
7171
await driver.onLogException(cdpConnection, function (event) {
7272
assert.strictEqual(
7373
event['exceptionDetails']['stackTrace']['callFrames'][0][
@@ -84,7 +84,7 @@ suite(
8484

8585
describe('JS DOM events', function () {
8686
it('calls the event listener on dom mutations', async function () {
87-
const cdpConnection = await driver.createCDPConnection()
87+
const cdpConnection = await driver.createCDPConnection('page')
8888
await driver.logMutationEvents(cdpConnection, function (event) {
8989
assert.strictEqual(event['attribute_name'], 'style')
9090
assert.strictEqual(event['current_value'], '')
@@ -103,7 +103,7 @@ suite(
103103
describe('Basic Auth Injection', function () {
104104
ignore(browsers(Browser.SAFARI, Browser.FIREFOX)).
105105
it('denies entry if username and password do not match', async function () {
106-
const pageCdpConnection = await driver.createCDPConnection()
106+
const pageCdpConnection = await driver.createCDPConnection('page')
107107

108108
await driver.register('random', 'random', pageCdpConnection)
109109
await driver.get(fileServer.Pages.basicAuth)
@@ -116,7 +116,7 @@ suite(
116116

117117
ignore(browsers(Browser.SAFARI, Browser.FIREFOX)).
118118
it('grants access if username and password are a match', async function () {
119-
const pageCdpConnection = await driver.createCDPConnection()
119+
const pageCdpConnection = await driver.createCDPConnection('page')
120120

121121
await driver.register('genie', 'bottle', pageCdpConnection)
122122
await driver.get(fileServer.Pages.basicAuth)
@@ -128,7 +128,7 @@ suite(
128128
describe("Network Interception", function () {
129129
ignore(browsers(Browser.SAFARI, Browser.FIREFOX)).
130130
it('Allows network requests to be captured and mocked', async function () {
131-
const connection = await driver.createCDPConnection()
131+
const connection = await driver.createCDPConnection('page')
132132
let url = fileServer.whereIs("/cheese")
133133
let httpResponse = new HttpResponse(url)
134134
httpResponse.addHeaders("Content-Type", "UTF-8")

0 commit comments

Comments
 (0)