Skip to content

Commit

Permalink
[Js] Cdp support (SeleniumHQ#8440)
Browse files Browse the repository at this point in the history
This adds support CDP support to the JS client and allows people to connect use CDP tooling, like how puppeteer does things, for better automation.
  • Loading branch information
raju249 authored Jul 16, 2020
1 parent de6d288 commit a5536df
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ javascript/node/selenium-webdriver/lib/atoms/find-elements.js
javascript/node/selenium-webdriver/lib/atoms/get-attribute.js
javascript/node/selenium-webdriver/lib/atoms/is-displayed.js
javascript/safari-driver/node_modules/
javascript/webdriver/devtools/types/
.idea/vcs.xml
.idea/workspace.xml
.idea/dictionaries/
Expand Down
1 change: 1 addition & 0 deletions common/devtools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ exports_files(
visibility = [
"//java/client/src/org/openqa/selenium/devtools:__pkg__",
"//py:__pkg__",
"//javascript/node/selenium-webdriver:__pkg__"
],
)
41 changes: 41 additions & 0 deletions javascript/node/selenium-webdriver/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary", "pkg_npm")
load("@npm_bazel_jasmine//:index.bzl", "jasmine_node_test")
load("//:copy_file.bzl", "copy_file")

SRC_FILES = [
"CHANGES.md",
Expand All @@ -14,6 +15,7 @@ SRC_FILES = [
"net/*.js",
"remote/*.js",
"testing/*.js",
"devtools/*.js"
])

pkg_npm(
Expand Down Expand Up @@ -67,6 +69,7 @@ jasmine_node_test(
"@npm//rimraf",
"@npm//tmp",
"@npm//sinon",
"@npm//ws",
],
local = True,
templated_args = ["--node_options=--require=$$(rlocation $(rootpath :tools/init_jasmine.js))"],
Expand All @@ -88,3 +91,41 @@ genrule(
],
cmd = "cp $(locations //:license) $(@D)",
)

nodejs_binary(
name = "cdp-srcs-generator",
entry_point = "devtools/generator/protocol-dts-generator.js",
data = [
":browser_protocol",
":js_protocol"
],
)

copy_file(
name = "browser_protocol",
src = "//common/devtools:browser_protocol.json",
out = "devtools/generator/browser_protocol.json",
)

copy_file(
name = "js_protocol",
src = "//common/devtools:js_protocol.json",
out = "devtools/generator/js_protocol.json",
)

genrule(
name="create-cdp-srcs",
srcs = [
":browser_protocol",
":js_protocol",
],
outs=[
"devtools/generator/protocol.d.js",
"devtools/generator/protocol-mapping.d.js",
"devtools/generator/protocol-proxy-api.d.js",
],
cmd = "$(location :cdp-srcs-generator) $(location :browser_protocol) $(location :js_protocol) $(OUTS)",
tools=[
":cdp-srcs-generator"
],
)
35 changes: 35 additions & 0 deletions javascript/node/selenium-webdriver/chromium.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ const error = require('./lib/error');
const promise = require('./lib/promise');
const Symbols = require('./lib/symbols');
const webdriver = require('./lib/webdriver');
const WebSocket = require('ws');
const cdp = require('./devtools/CDPConnection');
const remote = require('./remote');


Expand Down Expand Up @@ -670,6 +672,39 @@ class Driver extends webdriver.WebDriver {
.setParameter('params', params));
}

async createCDPConnection() {
const caps = await this.getCapabilities()
const seOptions = caps['map_'].get('se:options') || new Map();
const vendorInfo = caps['map_'].get(this.VENDOR_COMMAND_PREFIX + ':chromeOptions') || new Map();
const debuggerUrl = seOptions['cdp'] || vendorInfo['debuggerAddress'];
this._wsUrl = await this.getWsUrl(debuggerUrl);
return new Promise((resolve, reject) => {
try {
this._wsConnection = new WebSocket(this._wsUrl);
} catch (err) {
reject(err);
return
}

this._wsConnection.on('open', () => {
this._cdpConnection = new cdp.CdpConnection(this._wsConnection);
resolve(this._cdpConnection);
})

this._wsConnection.on('error', (error) => {
reject(error);
})
});
}

async getWsUrl(debuggerAddress) {
let request = new http.Request('GET', '/json/version');
let client = new http.HttpClient("http://" + debuggerAddress);
let url;
let response = await client.send(request);
url = JSON.parse(response.body)['webSocketDebuggerUrl'];
return url;
}
/**
* Set a permission state to the given value.
*
Expand Down
35 changes: 35 additions & 0 deletions javascript/node/selenium-webdriver/devtools/CDPConnection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Software Freedom Conservancy (SFC) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The SFC licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class CDPConnection {
constructor(wsConnection) {
this._wsConnection = wsConnection;
}

execute(method, params, callback) {
const message = {
method,
params: params || {}
};

this._wsConnection.send(JSON.stringify(message), callback);
}
}

exports.CdpConnection = CDPConnection;
Loading

0 comments on commit a5536df

Please sign in to comment.