Skip to content

Commit ca5deb0

Browse files
raju249harsha509
andauthored
Move cdp code to webdriver.js (#9131)
* Move cdp code to webdriver.js * Fix and run tests for devtools Co-authored-by: Sri Harsha <[email protected]>
1 parent 30e33a9 commit ca5deb0

File tree

2 files changed

+257
-256
lines changed

2 files changed

+257
-256
lines changed

javascript/node/selenium-webdriver/chromium.js

-256
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ const command = require('./lib/command')
8282
const error = require('./lib/error')
8383
const Symbols = require('./lib/symbols')
8484
const webdriver = require('./lib/webdriver')
85-
const WebSocket = require('ws')
86-
const cdp = require('./devtools/CDPConnection')
8785
const remote = require('./remote')
88-
const cdpTargets = ['page', 'browser']
89-
const fs = require('fs')
9086

9187
/**
9288
* Custom command names supported by Chromium WebDriver.
@@ -709,151 +705,6 @@ class Driver extends webdriver.WebDriver {
709705
)
710706
}
711707

712-
/**
713-
* Creates a new WebSocket connection.
714-
* @return {!Promise<resolved>} A new CDP instance.
715-
*/
716-
async createCDPConnection(target) {
717-
const caps = await this.getCapabilities()
718-
const seOptions = caps['map_'].get('se:options') || new Map()
719-
const vendorInfo =
720-
caps['map_'].get(this.VENDOR_COMMAND_PREFIX + ':chromeOptions') ||
721-
new Map()
722-
const debuggerUrl = seOptions['cdp'] || vendorInfo['debuggerAddress']
723-
this._wsUrl = await this.getWsUrl(debuggerUrl, target)
724-
725-
return new Promise((resolve, reject) => {
726-
try {
727-
this._wsConnection = new WebSocket(this._wsUrl)
728-
} catch (err) {
729-
reject(err)
730-
return
731-
}
732-
733-
this._wsConnection.on('open', () => {
734-
this._cdpConnection = new cdp.CdpConnection(this._wsConnection)
735-
resolve(this._cdpConnection)
736-
})
737-
738-
this._wsConnection.on('error', (error) => {
739-
reject(error)
740-
})
741-
})
742-
}
743-
744-
/**
745-
* Retrieves 'webSocketDebuggerUrl' by sending a http request using debugger address
746-
* @param {string} debuggerAddress
747-
* @param {string} target
748-
* @return {string} Returns parsed webSocketDebuggerUrl obtained from the http request
749-
*/
750-
async getWsUrl(debuggerAddress, target) {
751-
if (target && cdpTargets.indexOf(target.toLowerCase()) === -1) {
752-
throw new error.InvalidArgumentError('invalid target value')
753-
}
754-
let path = '/json/version'
755-
756-
if (target === 'page') {
757-
path = '/json'
758-
}
759-
let request = new http.Request('GET', path)
760-
let client = new http.HttpClient('http://' + debuggerAddress)
761-
let response = await client.send(request)
762-
let url = JSON.parse(response.body)['webSocketDebuggerUrl']
763-
if (target.toLowerCase() === 'page') {
764-
url = JSON.parse(response.body)[0]['webSocketDebuggerUrl']
765-
}
766-
767-
return url
768-
}
769-
770-
/**
771-
* Sets a listener for Fetch.authRequired event from CDP
772-
* If event is triggered, it enter username and password
773-
* and allows the test to move forward
774-
* @param {string} username
775-
* @param {string} password
776-
* @param connection CDP Connection
777-
*/
778-
async register(username, password, connection) {
779-
await connection.execute(
780-
'Network.setCacheDisabled',
781-
this.getRandomNumber(1, 10),
782-
{
783-
cacheDisabled: true,
784-
},
785-
null
786-
)
787-
788-
this._wsConnection.on('message', (message) => {
789-
const params = JSON.parse(message)
790-
791-
if (params.method === 'Fetch.authRequired') {
792-
const requestParams = params['params']
793-
connection.execute(
794-
'Fetch.continueWithAuth',
795-
this.getRandomNumber(1, 10),
796-
{
797-
requestId: requestParams['requestId'],
798-
authChallengeResponse: {
799-
response: 'ProvideCredentials',
800-
username: username,
801-
password: password,
802-
},
803-
}
804-
)
805-
} else if (params.method === 'Fetch.requestPaused') {
806-
const requestPausedParams = params['params']
807-
connection.execute(
808-
'Fetch.continueRequest',
809-
this.getRandomNumber(1, 10),
810-
{
811-
requestId: requestPausedParams['requestId'],
812-
}
813-
)
814-
}
815-
})
816-
817-
await connection.execute(
818-
'Fetch.enable',
819-
1,
820-
{
821-
handleAuthRequests: true,
822-
},
823-
null
824-
)
825-
}
826-
827-
/**
828-
*
829-
* @param connection
830-
* @param callback
831-
* @returns {Promise<void>}
832-
*/
833-
async onLogEvent(connection, callback) {
834-
await connection.execute(
835-
'Runtime.enable',
836-
this.getRandomNumber(1, 10),
837-
{},
838-
null
839-
)
840-
841-
this._wsConnection.on('message', (message) => {
842-
const params = JSON.parse(message)
843-
844-
if (params.method === 'Runtime.consoleAPICalled') {
845-
const consoleEventParams = params['params']
846-
let event = {
847-
type: consoleEventParams['type'],
848-
timestamp: new Date(consoleEventParams['timestamp']),
849-
args: consoleEventParams['args'],
850-
}
851-
852-
callback(event)
853-
}
854-
})
855-
}
856-
857708
/**
858709
* Set a permission state to the given value.
859710
*
@@ -872,113 +723,6 @@ class Driver extends webdriver.WebDriver {
872723
)
873724
}
874725

875-
/**
876-
*
877-
* @param connection
878-
* @param callback
879-
* @returns {Promise<void>}
880-
*/
881-
async onLogException(connection, callback) {
882-
await connection.execute(
883-
'Runtime.enable',
884-
this.getRandomNumber(1, 10),
885-
{},
886-
null
887-
)
888-
889-
this._wsConnection.on('message', (message) => {
890-
const params = JSON.parse(message)
891-
892-
if (params.method === 'Runtime.exceptionThrown') {
893-
const exceptionEventParams = params['params']
894-
let event = {
895-
exceptionDetails: exceptionEventParams['exceptionDetails'],
896-
timestamp: new Date(exceptionEventParams['timestamp']),
897-
}
898-
899-
callback(event)
900-
}
901-
})
902-
}
903-
904-
/**
905-
* @param connection
906-
* @param callback
907-
* @returns {Promise<void>}
908-
*/
909-
async logMutationEvents(connection, callback) {
910-
await connection.execute(
911-
'Runtime.enable',
912-
this.getRandomNumber(1, 10),
913-
{},
914-
null
915-
)
916-
await connection.execute(
917-
'Page.enable',
918-
this.getRandomNumber(1, 10),
919-
{},
920-
null
921-
)
922-
923-
await connection.execute(
924-
'Runtime.addBinding',
925-
this.getRandomNumber(1, 10),
926-
{
927-
name: '__webdriver_attribute',
928-
},
929-
null
930-
)
931-
932-
let mutationListener = ''
933-
try {
934-
// Depending on what is running the code it could appear in 2 different places which is why we try
935-
// here and then the other location
936-
mutationListener = fs
937-
.readFileSync(
938-
'./javascript/node/selenium-webdriver/lib/atoms/mutation-listener.js',
939-
'utf-8'
940-
)
941-
.toString()
942-
} catch {
943-
mutationListener = fs
944-
.readFileSync('./lib/atoms/mutation-listener.js', 'utf-8')
945-
.toString()
946-
}
947-
948-
this.executeScript(mutationListener)
949-
950-
await connection.execute(
951-
'Page.addScriptToEvaluateOnNewDocument',
952-
this.getRandomNumber(1, 10),
953-
{
954-
source: mutationListener,
955-
},
956-
null
957-
)
958-
959-
this._wsConnection.on('message', async (message) => {
960-
const params = JSON.parse(message)
961-
if (params.method === 'Runtime.bindingCalled') {
962-
let payload = JSON.parse(params['params']['payload'])
963-
let elements = await this.findElements({
964-
css: '*[data-__webdriver_id=' + payload['target'],
965-
})
966-
967-
if (elements.length === 0) {
968-
return
969-
}
970-
971-
let event = {
972-
element: elements[0],
973-
attribute_name: payload['name'],
974-
current_value: payload['value'],
975-
old_value: payload['oldValue'],
976-
}
977-
callback(event)
978-
}
979-
})
980-
}
981-
982726
/**
983727
* Sends a DevTools command to change the browser's download directory.
984728
*

0 commit comments

Comments
 (0)