Skip to content

Commit e14c264

Browse files
[JS] add support for selenium manager (#11189)
* [js] run CI with drivers from Selenium Manager * [JS] add selenium-manager edge support Co-authored-by: titusfortner <[email protected]>
1 parent 4861218 commit e14c264

File tree

8 files changed

+179
-19
lines changed

8 files changed

+179
-19
lines changed

.github/workflows/ci-javascript.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,17 @@ jobs:
5353
java-version: 11
5454
- name: Setup Fluxbox
5555
run: sudo apt-get -y install fluxbox
56-
- name: Setup Firefox and GeckoDriver
57-
uses: ./.github/actions/setup-firefox
56+
- name: Setup Firefox
57+
uses: abhi1693/setup-[email protected]
5858
with:
59+
browser: firefox
5960
version: ${{ matrix.version }}
6061
if: |
6162
matrix.browser == 'firefox'
62-
- name: Setup Chrome and ChromeDriver
63-
uses: ./.github/actions/setup-chrome
63+
- name: Setup Chrome
64+
uses: browser-actions/setup-chrome@latest
6465
with:
65-
version: ${{ matrix.version }}
66+
chrome-version: ${{ matrix.version }}
6667
if: |
6768
matrix.browser == 'chrome'
6869
- name: Start XVFB
@@ -98,9 +99,10 @@ jobs:
9899
java-version: 11
99100
- name: Setup Fluxbox
100101
run: sudo apt-get -y install fluxbox
101-
- name: Setup Firefox and GeckoDriver
102-
uses: ./.github/actions/setup-firefox
102+
- name: Setup Firefox
103+
uses: abhi1693/setup-[email protected]
103104
with:
105+
browser: firefox
104106
version: ${{ matrix.version }}
105107
- name: Start XVFB
106108
run: Xvfb :99 &

common/manager/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ exports_files(
2626
"//java/test/org/openqa/selenium/firefox:__pkg__",
2727
"//py:__pkg__",
2828
"//rb:__pkg__",
29+
"//javascript/node/selenium-webdriver:__pkg__",
2930
"//dotnet/src/webdriver:__pkg__",
3031
],
3132
)

javascript/node/selenium-webdriver/BUILD.bazel

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ SRC_FILES = [
2323
"remote/*.js",
2424
"testing/*.js",
2525
"devtools/*.js",
26+
"common/*.js"
2627
])
2728

2829
pkg_npm(
@@ -31,6 +32,9 @@ pkg_npm(
3132
srcs = SRC_FILES,
3233
deps = [
3334
":license",
35+
":manager-linux",
36+
":manager-macos",
37+
":manager-windows",
3438
"//javascript/node/selenium-webdriver/lib/atoms:find-elements",
3539
"//javascript/node/selenium-webdriver/lib/atoms:get_attribute",
3640
"//javascript/node/selenium-webdriver/lib/atoms:is_displayed",
@@ -141,3 +145,22 @@ genrule(
141145
":cdp-srcs-generator-" + n,
142146
],
143147
) for n in BROWSER_VERSIONS]
148+
149+
150+
copy_file(
151+
name = "manager-linux",
152+
src = "//common/manager:linux/selenium-manager",
153+
out = "bin/linux/selenium-manager",
154+
)
155+
156+
copy_file(
157+
name = "manager-windows",
158+
src = "//common/manager:windows/selenium-manager.exe",
159+
out = "bin/windows/selenium-manager.exe",
160+
)
161+
162+
copy_file(
163+
name = "manager-macos",
164+
src = "//common/manager:macos/selenium-manager",
165+
out = "bin/macos/selenium-manager",
166+
)

javascript/node/selenium-webdriver/chrome.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
const io = require('./io')
131131
const { Browser } = require('./lib/capabilities')
132132
const chromium = require('./chromium')
133+
const { driverLocation } = require('./common/seleniumManager')
133134

134135
/**
135136
* Name of the ChromeDriver executable.
@@ -150,20 +151,35 @@ class ServiceBuilder extends chromium.ServiceBuilder {
150151
/**
151152
* @param {string=} opt_exe Path to the server executable to use. If omitted,
152153
* the builder will attempt to locate the chromedriver on the current
153-
* PATH.
154+
* PATH. If the chromedriver is not available in path, selenium-manager will
155+
* download the chromedriver
154156
* @throws {Error} If provided executable does not exist, or the chromedriver
155157
* cannot be found on the PATH.
156158
*/
157159
constructor(opt_exe) {
158160
let exe = opt_exe || locateSynchronously()
161+
162+
if (!exe) {
163+
console.log(
164+
` The ChromeDriver could not be found on the current PATH, trying Selenium Manager`
165+
)
166+
167+
try {
168+
exe = driverLocation(Browser.CHROME)
169+
} catch (err) {
170+
console.log(`Unable to obtain driver using Selenium Manager: ${err}`)
171+
}
172+
}
173+
159174
if (!exe) {
160175
throw Error(
161-
`The ChromeDriver could not be found on the current PATH. Please ` +
162-
`download the latest version of the ChromeDriver from ` +
163-
`http://chromedriver.storage.googleapis.com/index.html and ensure ` +
164-
`it can be found on your PATH.`
176+
`The ChromeDriver could not be found on the current PATH.
177+
Please download the latest version of the ChromeDriver
178+
from http://chromedriver.storage.googleapis.com/index.html
179+
and ensure it can be found on your PATH.`
165180
)
166181
}
182+
167183
super(exe)
168184
}
169185
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Licensed to the Software Freedom Conservancy (SFC) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The SFC licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/**
21+
* Wrapper for getting information from the Selenium Manager binaries
22+
*/
23+
24+
const { platform } = require('process')
25+
const path = require('path')
26+
const fs = require('fs')
27+
const execSync = require('child_process').execSync
28+
29+
/**
30+
* currently supported browsers for selenium-manager
31+
* @type {string[]}
32+
*/
33+
const Browser = ['chrome', 'firefox', 'edge']
34+
35+
/**
36+
* Determines the path of the correct Selenium Manager binary
37+
* @returns {string}
38+
*/
39+
function getBinary() {
40+
const directory = {
41+
darwin: 'macos',
42+
win32: 'windows',
43+
cygwin: 'windows',
44+
linux: 'linux',
45+
}[platform]
46+
47+
const file =
48+
directory === 'windows' ? 'selenium-manager.exe' : 'selenium-manager'
49+
50+
const filePath = path.join(__dirname, '..', '/bin', directory, file)
51+
52+
if (!fs.existsSync(filePath)) {
53+
throw new Error(`Unable to obtain Selenium Manager`)
54+
}
55+
56+
return filePath
57+
}
58+
59+
/**
60+
* Determines the path of the correct driver
61+
* @param {Browser|string} browser name to fetch the driver
62+
* @returns {string} path of the driver location
63+
*/
64+
65+
function driverLocation(browser) {
66+
if (!Browser.includes(browser.toLocaleString())) {
67+
throw new Error(
68+
`Unable to locate driver associated with browser name: ${browser}`
69+
)
70+
}
71+
72+
let args = [getBinary(), '--browser', browser]
73+
let result
74+
75+
try {
76+
result = execSync(args.join(' ')).toString()
77+
} catch (e) {
78+
throw new Error(
79+
`Error executing command with ${args} : Error ${e.stderr.toString()}`
80+
)
81+
}
82+
83+
if (!result.startsWith('INFO\t')) {
84+
throw new Error(`Unsuccessful command executed ${args}}`)
85+
}
86+
87+
return result.replace('INFO\t', '').trim()
88+
}
89+
90+
// PUBLIC API
91+
module.exports = { driverLocation }

javascript/node/selenium-webdriver/edge.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
const { Browser } = require('./lib/capabilities')
8181
const io = require('./io')
8282
const chromium = require('./chromium')
83+
const { driverLocation } = require('./common/seleniumManager')
8384

8485
/**
8586
* Name of the EdgeDriver executable.
@@ -106,6 +107,19 @@ class ServiceBuilder extends chromium.ServiceBuilder {
106107
*/
107108
constructor(opt_exe) {
108109
let exe = opt_exe || locateSynchronously()
110+
111+
if (!exe) {
112+
console.log(
113+
`The WebDriver for Edge could not be found on the current PATH, trying Selenium Manager`
114+
)
115+
116+
try {
117+
exe = driverLocation('edge')
118+
} catch (err) {
119+
console.log(`Unable to obtain driver using Selenium Manager: ${err}`)
120+
}
121+
}
122+
109123
if (!exe) {
110124
throw Error(
111125
`The WebDriver for Edge could not be found on the current PATH. Please download the ` +
@@ -114,6 +128,7 @@ class ServiceBuilder extends chromium.ServiceBuilder {
114128
`and ensure it can be found on your PATH.`
115129
)
116130
}
131+
117132
super(exe)
118133
this.setLoopback(true)
119134
}

javascript/node/selenium-webdriver/firefox.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ const webdriver = require('./lib/webdriver')
127127
const zip = require('./io/zip')
128128
const { Browser, Capabilities } = require('./lib/capabilities')
129129
const { Zip } = require('./io/zip')
130+
const { driverLocation } = require('./common/seleniumManager')
130131

131132
/**
132133
* Thrown when there an add-on is malformed.
@@ -459,16 +460,27 @@ function locateSynchronously() {
459460
*/
460461
function findGeckoDriver() {
461462
let exe = locateSynchronously()
463+
464+
if (!exe) {
465+
console.log(
466+
`The ${GECKO_DRIVER_EXE} executable could not be found on the current PATH, trying Selenium Manager`
467+
)
468+
469+
try {
470+
exe = driverLocation(Browser.FIREFOX)
471+
} catch (err) {
472+
console.log(`Unable to obtain driver using Selenium Manager: ${err}`)
473+
}
474+
}
475+
462476
if (!exe) {
463477
throw Error(
464-
'The ' +
465-
GECKO_DRIVER_EXE +
466-
' executable could not be found on the current ' +
467-
'PATH. Please download the latest version from ' +
468-
'https://github.com/mozilla/geckodriver/releases/ ' +
469-
'and ensure it can be found on your PATH.'
478+
`The ${GECKO_DRIVER_EXE} executable could not be found on the current PATH.
479+
Please download the latest version from https://github.com/mozilla/geckodriver/releases/
480+
and ensure it can be found on your PATH.`
470481
)
471482
}
483+
472484
return exe
473485
}
474486

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ describe('WebDriver', function () {
228228
.expect(CName.NEW_SESSION)
229229
.withParameters({
230230
capabilities: {
231-
alwaysMatch: { browserName: 'firefox'},
231+
alwaysMatch: { browserName: 'firefox' },
232232
firstMatch: [{}],
233233
},
234234
})

0 commit comments

Comments
 (0)