Skip to content

Commit

Permalink
Armk/selenium3 backward com (#730)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ArminazK and christian-bromann authored Nov 16, 2022
1 parent baabbad commit 562f80d
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 52 deletions.
6 changes: 5 additions & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function myFn() {
await selenium.install({
// check for more recent versions of selenium here:
// https://selenium-release.storage.googleapis.com/index.html
version: '3.141.59',
version: process.env.SELENIUM_VERSION || '4.4.0',
baseURL: 'https://selenium-release.storage.googleapis.com',
drivers: {
chrome: {
Expand Down Expand Up @@ -120,3 +120,7 @@ If you're getting this error, it means that you didn't shut down the server succ
```shell
pkill -f selenium-standalone
```

## Set `selenium-standalone` Version as NodeJS environment parameter

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)
2 changes: 1 addition & 1 deletion lib/default-config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = () => {
const config = {
baseURL: 'https://github.com/SeleniumHQ/selenium/releases/download',
version: '4.0.0',
version: process.env.SELENIUM_VERSION || '4.4.0',
drivers: {
chrome: {
version: 'latest',
Expand Down
6 changes: 3 additions & 3 deletions lib/get-selenium-status-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ exports.getSeleniumStatusUrl = function (seleniumArgs, opts) {
const nodeConfigArg = seleniumArgs.indexOf('-nodeConfig');

// args prefix differs for selenium3 and selenium4
let argsPrefix = '--';
if (!isSelenium4(opts.version)) {
argsPrefix = '-';
let argsPrefix = '-';
if (isSelenium4(opts.version)) {
argsPrefix = '--';
}
const portArg = seleniumArgs.indexOf(`${argsPrefix}port`);
const hostArg = seleniumArgs.indexOf(`${argsPrefix}host`);
Expand Down
32 changes: 21 additions & 11 deletions lib/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,46 @@ async function start(_opts) {
*/
if (fsPaths.chrome) {
args.push('-Dwebdriver.chrome.driver=' + fsPaths.chrome.installPath);
opts.seleniumArgs.push('-I');
opts.seleniumArgs.push('chrome');
if (isSelenium4(opts.version)) {
opts.seleniumArgs.push('-I');
opts.seleniumArgs.push('chrome');
}
}

if (process.platform === 'win32' && fsPaths.ie) {
args.push('-Dwebdriver.ie.driver=' + fsPaths.ie.installPath);
opts.seleniumArgs.push('-I');
opts.seleniumArgs.push('internet explorer');
if (isSelenium4(opts.version)) {
opts.seleniumArgs.push('-I');
opts.seleniumArgs.push('internet explorer');
}
} else {
delete fsPaths.ie;
}

if (process.platform === 'win32' && fsPaths.edge) {
args.push('-Dwebdriver.edge.driver=' + fsPaths.edge.installPath);
opts.seleniumArgs.push('-I');
opts.seleniumArgs.push('edge');
if (isSelenium4(opts.version)) {
opts.seleniumArgs.push('-I');
opts.seleniumArgs.push('edge');
}
} else {
delete fsPaths.edge;
}

if (fsPaths.firefox) {
args.push('-Dwebdriver.gecko.driver=' + fsPaths.firefox.installPath);
opts.seleniumArgs.push('-I');
opts.seleniumArgs.push('firefox');
if (isSelenium4(opts.version)) {
opts.seleniumArgs.push('-I');
opts.seleniumArgs.push('firefox');
}
}

if (fsPaths.chromiumedge) {
args.push('-Dwebdriver.edge.driver=' + fsPaths.chromiumedge.installPath);
opts.seleniumArgs.push('-I');
opts.seleniumArgs.push('edge');
if (isSelenium4(opts.version)) {
opts.seleniumArgs.push('-I');
opts.seleniumArgs.push('edge');
}
} else {
delete fsPaths.chromiumedge;
}
Expand All @@ -126,7 +136,7 @@ async function start(_opts) {
// (Safari does exist on Windows, but it is no longer supported. And while it seems like it's possible
// to install Safari on Linux, apparently you need to use a compatability layer like WINE. This code could
// theoretically be updated to support other OSes besides macOS)
if (process.platform === 'darwin') {
if (process.platform === 'darwin' && isSelenium4(opts.version)) {
// SafariDriver is already bundled with Safari, so there's nothing that needs to be downloaded
opts.seleniumArgs.push('-I');
opts.seleniumArgs.push('safari');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"scripts": {
"start": "DEBUG=selenium-standalone:* ./bin/selenium-standalone install && DEBUG=selenium-standalone:* ./bin/selenium-standalone start",
"test": "./bin/selenium-standalone install && mocha",
"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'",
"docker:build": "run-s docker:build:*",
"docker:build:latest": "docker build -t webdriverio/${npm_package_name}:latest --cache-from webdriverio/${npm_package_name}:latest .",
"docker:build:tag": "docker build -t webdriverio/${npm_package_name}:${npm_package_version} --cache-from webdriverio/${npm_package_name}:${npm_package_version} .",
Expand Down
133 changes: 98 additions & 35 deletions test/programmatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ describe('programmatic use', function () {
});
};

const testStartSelenium3 = function (done, options, callback) {
const selenium = require('../');
selenium
.start(options)
.catch(done)
.then((cp) => {
cp.kill();
if (callback(cp) !== false) {
done();
}
});
};

it('should install', (done) => {
testInstall(done, {}, (log) => {
if (!containsChrome(log)) {
Expand All @@ -63,53 +76,103 @@ describe('programmatic use', function () {
});
});

it('should start', (done) => {
testStart(done, {}, (log) => {
if (!containsChrome(log)) {
done(new Error('Chrome driver should be loaded'));
return false;
}
});
it(`should start`, (done) => {
if (process.env.SELENIUM_VERSION.includes('3.141.59')) {
testStartSelenium3(done, {}, (cp) => {
if (cp.spawnargs && !cp.spawnargs.some(containsChrome)) {
done(new Error('Chrome driver should be loaded'));
return false;
}
});
} else {
testStart(done, {}, (log) => {
if (!containsChrome(log)) {
done(new Error('Chrome driver should be loaded'));
return false;
}
});
}
});

it('should start with custom seleniumArgs', (done) => {
testStart(done, { seleniumArgs: ['--port', '12345'] }, (log) => {
if (!containsChrome(log)) {
done(new Error('Chrome driver should be loaded'));
return false;
}
});
if (process.env.SELENIUM_VERSION.includes('3.141.59')) {
testStartSelenium3(done, { seleniumArgs: ['-port', '12345'] }, (cp) => {
if (cp.spawnargs && !cp.spawnargs.some(containsChrome)) {
done(new Error('Chrome driver should be loaded'));
return false;
}
});
} else {
testStart(done, { seleniumArgs: ['--port', '12345'] }, (log) => {
if (!containsChrome(log)) {
done(new Error('Chrome driver should be loaded'));
return false;
}
});
}
});

it('should start with the given drivers', (done) => {
testStart(done, { drivers: { firefox: {} } }, (log) => {
if (containsChrome(log)) {
done(new Error('Chrome driver should not be loaded'));
return false;
}
});
if (process.env.SELENIUM_VERSION.includes('3.141.59')) {
testStartSelenium3(done, { drivers: { firefox: {} } }, (cp) => {
if (cp.spawnargs && cp.spawnargs.some(containsChrome)) {
done(new Error('Chrome driver should be loaded'));
return false;
}
});
} else {
testStart(done, { drivers: { firefox: {} } }, (log) => {
if (containsChrome(log)) {
done(new Error('Chrome driver should not be loaded'));
return false;
}
});
}
});

it('should start and merge drivers', (done) => {
const options = { seleniumArgs: ['--port', '4445'], drivers: { chrome: {} } };
testStart(done, options, (log) => {
if (!containsChrome(log)) {
done(new Error('Chrome driver should be loaded'));
return false;
}
});
if (process.env.SELENIUM_VERSION.includes('3.141.59')) {
const options = { seleniumArgs: ['-port', '4445'], drivers: { chrome: {} } };
testStartSelenium3(done, options, (cp) => {
if (cp.spawnargs && !cp.spawnargs.some(containsChrome)) {
done(new Error('Chrome driver should be loaded'));
return false;
}
});
} else {
const options = { seleniumArgs: ['--port', '4445'], drivers: { chrome: {} } };
testStart(done, options, (log) => {
if (!containsChrome(log)) {
done(new Error('Chrome driver should be loaded'));
return false;
}
});
}
});

it('should start with singleDriverStart options', (done) => {
testStart(
done,
{ singleDriverStart: 'firefox', drivers: { chrome: {}, firefox: {} }, seleniumArgs: ['--port', '4446'] },
(log) => {
if (containsChrome(log)) {
done(new Error('Chrome driver should not be loaded'));
return false;
if (process.env.SELENIUM_VERSION.includes('3.141.59')) {
testStartSelenium3(
done,
{ singleDriverStart: 'firefox', drivers: { chrome: {}, firefox: {} }, seleniumArgs: ['-port', '4446'] },
(cp) => {
if (cp.spawnargs && cp.spawnargs.some(containsChrome)) {
done(new Error('Chrome driver should be loaded'));
return false;
}
}
}
);
);
} else {
testStart(
done,
{ singleDriverStart: 'firefox', drivers: { chrome: {}, firefox: {} }, seleniumArgs: ['--port', '4446'] },
(log) => {
if (containsChrome(log)) {
done(new Error('Chrome driver should not be loaded'));
return false;
}
}
);
}
});
});

0 comments on commit 562f80d

Please sign in to comment.