Skip to content

Commit 562f80d

Browse files
Armk/selenium3 backward com (#730)
* backward compatibility no arg '-I' for selenium 3 * if true instead of false * added npm script to test selenium 3 and 4 changed programmatic.js to support selenium 3 and 4 removed params '-I' from selenium 3 * added documentation on process.env.SELENIUM_VERSION parameter * formatting * Update docs/API.md * Update docs/API.md Co-authored-by: Christian Bromann <git@bromann.dev>
1 parent baabbad commit 562f80d

File tree

6 files changed

+129
-52
lines changed

6 files changed

+129
-52
lines changed

docs/API.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function myFn() {
2626
await selenium.install({
2727
// check for more recent versions of selenium here:
2828
// https://selenium-release.storage.googleapis.com/index.html
29-
version: '3.141.59',
29+
version: process.env.SELENIUM_VERSION || '4.4.0',
3030
baseURL: 'https://selenium-release.storage.googleapis.com',
3131
drivers: {
3232
chrome: {
@@ -120,3 +120,7 @@ If you're getting this error, it means that you didn't shut down the server succ
120120
```shell
121121
pkill -f selenium-standalone
122122
```
123+
124+
## Set `selenium-standalone` Version as NodeJS environment parameter
125+
126+
You can set any version by `process.env.SELENIUM_VERSION=3.141.59` before starting selenium-standalone. Default values are here: [lib/default-config.js](../lib/default-config.js)

lib/default-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = () => {
22
const config = {
33
baseURL: 'https://github.com/SeleniumHQ/selenium/releases/download',
4-
version: '4.0.0',
4+
version: process.env.SELENIUM_VERSION || '4.4.0',
55
drivers: {
66
chrome: {
77
version: 'latest',

lib/get-selenium-status-url.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ exports.getSeleniumStatusUrl = function (seleniumArgs, opts) {
4848
const nodeConfigArg = seleniumArgs.indexOf('-nodeConfig');
4949

5050
// args prefix differs for selenium3 and selenium4
51-
let argsPrefix = '--';
52-
if (!isSelenium4(opts.version)) {
53-
argsPrefix = '-';
51+
let argsPrefix = '-';
52+
if (isSelenium4(opts.version)) {
53+
argsPrefix = '--';
5454
}
5555
const portArg = seleniumArgs.indexOf(`${argsPrefix}port`);
5656
const hostArg = seleniumArgs.indexOf(`${argsPrefix}host`);

lib/start.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,36 +88,46 @@ async function start(_opts) {
8888
*/
8989
if (fsPaths.chrome) {
9090
args.push('-Dwebdriver.chrome.driver=' + fsPaths.chrome.installPath);
91-
opts.seleniumArgs.push('-I');
92-
opts.seleniumArgs.push('chrome');
91+
if (isSelenium4(opts.version)) {
92+
opts.seleniumArgs.push('-I');
93+
opts.seleniumArgs.push('chrome');
94+
}
9395
}
9496

9597
if (process.platform === 'win32' && fsPaths.ie) {
9698
args.push('-Dwebdriver.ie.driver=' + fsPaths.ie.installPath);
97-
opts.seleniumArgs.push('-I');
98-
opts.seleniumArgs.push('internet explorer');
99+
if (isSelenium4(opts.version)) {
100+
opts.seleniumArgs.push('-I');
101+
opts.seleniumArgs.push('internet explorer');
102+
}
99103
} else {
100104
delete fsPaths.ie;
101105
}
102106

103107
if (process.platform === 'win32' && fsPaths.edge) {
104108
args.push('-Dwebdriver.edge.driver=' + fsPaths.edge.installPath);
105-
opts.seleniumArgs.push('-I');
106-
opts.seleniumArgs.push('edge');
109+
if (isSelenium4(opts.version)) {
110+
opts.seleniumArgs.push('-I');
111+
opts.seleniumArgs.push('edge');
112+
}
107113
} else {
108114
delete fsPaths.edge;
109115
}
110116

111117
if (fsPaths.firefox) {
112118
args.push('-Dwebdriver.gecko.driver=' + fsPaths.firefox.installPath);
113-
opts.seleniumArgs.push('-I');
114-
opts.seleniumArgs.push('firefox');
119+
if (isSelenium4(opts.version)) {
120+
opts.seleniumArgs.push('-I');
121+
opts.seleniumArgs.push('firefox');
122+
}
115123
}
116124

117125
if (fsPaths.chromiumedge) {
118126
args.push('-Dwebdriver.edge.driver=' + fsPaths.chromiumedge.installPath);
119-
opts.seleniumArgs.push('-I');
120-
opts.seleniumArgs.push('edge');
127+
if (isSelenium4(opts.version)) {
128+
opts.seleniumArgs.push('-I');
129+
opts.seleniumArgs.push('edge');
130+
}
121131
} else {
122132
delete fsPaths.chromiumedge;
123133
}
@@ -126,7 +136,7 @@ async function start(_opts) {
126136
// (Safari does exist on Windows, but it is no longer supported. And while it seems like it's possible
127137
// to install Safari on Linux, apparently you need to use a compatability layer like WINE. This code could
128138
// theoretically be updated to support other OSes besides macOS)
129-
if (process.platform === 'darwin') {
139+
if (process.platform === 'darwin' && isSelenium4(opts.version)) {
130140
// SafariDriver is already bundled with Safari, so there's nothing that needs to be downloaded
131141
opts.seleniumArgs.push('-I');
132142
opts.seleniumArgs.push('safari');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"scripts": {
1111
"start": "DEBUG=selenium-standalone:* ./bin/selenium-standalone install && DEBUG=selenium-standalone:* ./bin/selenium-standalone start",
12-
"test": "./bin/selenium-standalone install && mocha",
12+
"test": "export SELENIUM_VERSION=4.4.0&& ./bin/selenium-standalone install && mocha && export SELENIUM_VERSION=3.141.59&& ./bin/selenium-standalone install && mocha 'test/programmatic.js'",
1313
"docker:build": "run-s docker:build:*",
1414
"docker:build:latest": "docker build -t webdriverio/${npm_package_name}:latest --cache-from webdriverio/${npm_package_name}:latest .",
1515
"docker:build:tag": "docker build -t webdriverio/${npm_package_name}:${npm_package_version} --cache-from webdriverio/${npm_package_name}:${npm_package_version} .",

test/programmatic.js

Lines changed: 98 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ describe('programmatic use', function () {
4545
});
4646
};
4747

48+
const testStartSelenium3 = function (done, options, callback) {
49+
const selenium = require('../');
50+
selenium
51+
.start(options)
52+
.catch(done)
53+
.then((cp) => {
54+
cp.kill();
55+
if (callback(cp) !== false) {
56+
done();
57+
}
58+
});
59+
};
60+
4861
it('should install', (done) => {
4962
testInstall(done, {}, (log) => {
5063
if (!containsChrome(log)) {
@@ -63,53 +76,103 @@ describe('programmatic use', function () {
6376
});
6477
});
6578

66-
it('should start', (done) => {
67-
testStart(done, {}, (log) => {
68-
if (!containsChrome(log)) {
69-
done(new Error('Chrome driver should be loaded'));
70-
return false;
71-
}
72-
});
79+
it(`should start`, (done) => {
80+
if (process.env.SELENIUM_VERSION.includes('3.141.59')) {
81+
testStartSelenium3(done, {}, (cp) => {
82+
if (cp.spawnargs && !cp.spawnargs.some(containsChrome)) {
83+
done(new Error('Chrome driver should be loaded'));
84+
return false;
85+
}
86+
});
87+
} else {
88+
testStart(done, {}, (log) => {
89+
if (!containsChrome(log)) {
90+
done(new Error('Chrome driver should be loaded'));
91+
return false;
92+
}
93+
});
94+
}
7395
});
7496

7597
it('should start with custom seleniumArgs', (done) => {
76-
testStart(done, { seleniumArgs: ['--port', '12345'] }, (log) => {
77-
if (!containsChrome(log)) {
78-
done(new Error('Chrome driver should be loaded'));
79-
return false;
80-
}
81-
});
98+
if (process.env.SELENIUM_VERSION.includes('3.141.59')) {
99+
testStartSelenium3(done, { seleniumArgs: ['-port', '12345'] }, (cp) => {
100+
if (cp.spawnargs && !cp.spawnargs.some(containsChrome)) {
101+
done(new Error('Chrome driver should be loaded'));
102+
return false;
103+
}
104+
});
105+
} else {
106+
testStart(done, { seleniumArgs: ['--port', '12345'] }, (log) => {
107+
if (!containsChrome(log)) {
108+
done(new Error('Chrome driver should be loaded'));
109+
return false;
110+
}
111+
});
112+
}
82113
});
83114

84115
it('should start with the given drivers', (done) => {
85-
testStart(done, { drivers: { firefox: {} } }, (log) => {
86-
if (containsChrome(log)) {
87-
done(new Error('Chrome driver should not be loaded'));
88-
return false;
89-
}
90-
});
116+
if (process.env.SELENIUM_VERSION.includes('3.141.59')) {
117+
testStartSelenium3(done, { drivers: { firefox: {} } }, (cp) => {
118+
if (cp.spawnargs && cp.spawnargs.some(containsChrome)) {
119+
done(new Error('Chrome driver should be loaded'));
120+
return false;
121+
}
122+
});
123+
} else {
124+
testStart(done, { drivers: { firefox: {} } }, (log) => {
125+
if (containsChrome(log)) {
126+
done(new Error('Chrome driver should not be loaded'));
127+
return false;
128+
}
129+
});
130+
}
91131
});
92132

93133
it('should start and merge drivers', (done) => {
94-
const options = { seleniumArgs: ['--port', '4445'], drivers: { chrome: {} } };
95-
testStart(done, options, (log) => {
96-
if (!containsChrome(log)) {
97-
done(new Error('Chrome driver should be loaded'));
98-
return false;
99-
}
100-
});
134+
if (process.env.SELENIUM_VERSION.includes('3.141.59')) {
135+
const options = { seleniumArgs: ['-port', '4445'], drivers: { chrome: {} } };
136+
testStartSelenium3(done, options, (cp) => {
137+
if (cp.spawnargs && !cp.spawnargs.some(containsChrome)) {
138+
done(new Error('Chrome driver should be loaded'));
139+
return false;
140+
}
141+
});
142+
} else {
143+
const options = { seleniumArgs: ['--port', '4445'], drivers: { chrome: {} } };
144+
testStart(done, options, (log) => {
145+
if (!containsChrome(log)) {
146+
done(new Error('Chrome driver should be loaded'));
147+
return false;
148+
}
149+
});
150+
}
101151
});
102152

103153
it('should start with singleDriverStart options', (done) => {
104-
testStart(
105-
done,
106-
{ singleDriverStart: 'firefox', drivers: { chrome: {}, firefox: {} }, seleniumArgs: ['--port', '4446'] },
107-
(log) => {
108-
if (containsChrome(log)) {
109-
done(new Error('Chrome driver should not be loaded'));
110-
return false;
154+
if (process.env.SELENIUM_VERSION.includes('3.141.59')) {
155+
testStartSelenium3(
156+
done,
157+
{ singleDriverStart: 'firefox', drivers: { chrome: {}, firefox: {} }, seleniumArgs: ['-port', '4446'] },
158+
(cp) => {
159+
if (cp.spawnargs && cp.spawnargs.some(containsChrome)) {
160+
done(new Error('Chrome driver should be loaded'));
161+
return false;
162+
}
111163
}
112-
}
113-
);
164+
);
165+
} else {
166+
testStart(
167+
done,
168+
{ singleDriverStart: 'firefox', drivers: { chrome: {}, firefox: {} }, seleniumArgs: ['--port', '4446'] },
169+
(log) => {
170+
if (containsChrome(log)) {
171+
done(new Error('Chrome driver should not be loaded'));
172+
return false;
173+
}
174+
}
175+
);
176+
}
114177
});
115178
});

0 commit comments

Comments
 (0)