Skip to content

Commit f59e90f

Browse files
authored
Fix conditional driver loading (#670)
* Revert "Revert "Adjustments to `start` Options (#660)"" This reverts commit f52ad8b. * Update test suite to detect when Chrome didn't load The commit with a problem caused no drivers to load but the tests did not detect this as they were looking to see what args were specified not what was the result of those args with regard to what drivers loaded. This commit updates the test suite to better detect the problem. It captures the stdout and checks it for Chrome instead of just checking the args. With this in place I now have a failing test with my problematic code from before. * Make selenium use only the drivers specified This removes the previous attempt to do this, `--detect-drivers`. That option would be better named as "--load-drivers" as it won't load any drivers regardless if specified the path via the CLI. According to this discussion: SeleniumHQ/selenium#8928 The `-I` driver is what is needed to specify what drivers should be loaded. Therefore for each driver we are providing an explicit path we are also indicating that driver should be loaded. Any without a -I will not get loaded. I updated the failing test so it does explicitly load the firefox driver (to avoid the jar throwing an error about no drivers being loaded) and then have the test verify that Chrome is not in the output indicating that other drivers such as Chrome did not load. The possible values for `-I` doesn't seem well documented (that I could find). The code seems to compare the CLI value to the "display name" for each driver info class: https://github.com/SeleniumHQ/selenium/blob/9546dba7a5aefe45ff8fe2bbc1039819dde10672/java/src/org/openqa/selenium/grid/node/config/NodeOptions.java#L460 Therefore to get a complete list of the right values I just looked at all the implementations of the "getDisplayName" method. I have personally manually tested chrome, firefox and chromium edge. I don't have a win32 system to test IE and the legacy version of Edge but based on the display name it *should* work just fine.
1 parent 9b8a0d2 commit f59e90f

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

lib/start.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ async function start(_opts) {
2727
opts.seleniumArgs = [];
2828
}
2929

30+
if (!opts.spawnOptions) {
31+
opts.spawnOptions = {};
32+
}
33+
3034
if (!opts.version) {
3135
opts.version = defaultConfig.version;
3236
}
@@ -84,26 +88,36 @@ async function start(_opts) {
8488
*/
8589
if (fsPaths.chrome) {
8690
args.push('-Dwebdriver.chrome.driver=' + fsPaths.chrome.installPath);
91+
opts.seleniumArgs.push('-I');
92+
opts.seleniumArgs.push('chrome');
8793
}
8894

8995
if (process.platform === 'win32' && fsPaths.ie) {
9096
args.push('-Dwebdriver.ie.driver=' + fsPaths.ie.installPath);
97+
opts.seleniumArgs.push('-I');
98+
opts.seleniumArgs.push('internet explorer');
9199
} else {
92100
delete fsPaths.ie;
93101
}
94102

95103
if (process.platform === 'win32' && fsPaths.edge) {
96104
args.push('-Dwebdriver.edge.driver=' + fsPaths.edge.installPath);
105+
opts.seleniumArgs.push('-I');
106+
opts.seleniumArgs.push('edge');
97107
} else {
98108
delete fsPaths.edge;
99109
}
100110

101111
if (fsPaths.firefox) {
102112
args.push('-Dwebdriver.gecko.driver=' + fsPaths.firefox.installPath);
113+
opts.seleniumArgs.push('-I');
114+
opts.seleniumArgs.push('firefox');
103115
}
104116

105117
if (fsPaths.chromiumedge) {
106118
args.push('-Dwebdriver.edge.driver=' + fsPaths.chromiumedge.installPath);
119+
opts.seleniumArgs.push('-I');
120+
opts.seleniumArgs.push('edge');
107121
} else {
108122
delete fsPaths.chromiumedge;
109123
}
@@ -117,6 +131,10 @@ async function start(_opts) {
117131
throw new Error(`Port ${seleniumStatusUrl.port} is already in use.`);
118132
}
119133

134+
if (!opts.spawnOptions.stdio) {
135+
opts.spawnOptions.stdio = 'ignore';
136+
}
137+
120138
debug('Spawning Selenium Server process', opts.javaPath, args);
121139
const selenium = spawn(opts.javaPath, args, opts.spawnOptions);
122140
await checkStarted(selenium, seleniumStatusUrl.toString());

test/programmatic.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,27 @@ describe('programmatic use', function () {
2121
}
2222
});
2323
};
24-
const testStart = function (done, options, callback) {
24+
const testStart = function (done, rawOptions, callback) {
2525
const selenium = require('../');
26+
const stdio = [
27+
'ignore', // stdin
28+
'pipe', // stdout
29+
'ignore', // stderr
30+
];
31+
const options = Object.assign({}, rawOptions);
32+
options.spawnOptions = Object.assign({ stdio }, options.spawnOptions);
2633
selenium
2734
.start(options)
2835
.catch(done)
2936
.then((cp) => {
3037
cp.kill();
31-
if (callback(cp) !== false) {
32-
done();
33-
}
38+
let stdout = '';
39+
cp.stdout.on('data', (chunk) => (stdout += chunk));
40+
cp.stdout.on('end', () => {
41+
if (callback(stdout) !== false) {
42+
done();
43+
}
44+
});
3445
});
3546
};
3647

@@ -53,26 +64,26 @@ describe('programmatic use', function () {
5364
});
5465

5566
it('should start', (done) => {
56-
testStart(done, {}, (cp) => {
57-
if (cp.spawnargs && !cp.spawnargs.some(containsChrome)) {
67+
testStart(done, {}, (log) => {
68+
if (!containsChrome(log)) {
5869
done(new Error('Chrome driver should be loaded'));
5970
return false;
6071
}
6172
});
6273
});
6374

6475
it('should start with custom seleniumArgs', (done) => {
65-
testStart(done, { seleniumArgs: ['--port', '12345'] }, (cp) => {
66-
if (cp.spawnargs && !cp.spawnargs.some(containsChrome)) {
76+
testStart(done, { seleniumArgs: ['--port', '12345'] }, (log) => {
77+
if (!containsChrome(log)) {
6778
done(new Error('Chrome driver should be loaded'));
6879
return false;
6980
}
7081
});
7182
});
7283

7384
it('should start with the given drivers', (done) => {
74-
testStart(done, { drivers: {} }, (cp) => {
75-
if (cp.spawnargs && cp.spawnargs.some(containsChrome)) {
85+
testStart(done, { drivers: { firefox: {} } }, (log) => {
86+
if (containsChrome(log)) {
7687
done(new Error('Chrome driver should not be loaded'));
7788
return false;
7889
}
@@ -81,8 +92,8 @@ describe('programmatic use', function () {
8192

8293
it('should start and merge drivers', (done) => {
8394
const options = { seleniumArgs: ['--port', '4445'], drivers: { chrome: {} } };
84-
testStart(done, options, (cp) => {
85-
if (cp.spawnargs && !cp.spawnargs.some(containsChrome)) {
95+
testStart(done, options, (log) => {
96+
if (!containsChrome(log)) {
8697
done(new Error('Chrome driver should be loaded'));
8798
return false;
8899
}
@@ -93,8 +104,8 @@ describe('programmatic use', function () {
93104
testStart(
94105
done,
95106
{ singleDriverStart: 'firefox', drivers: { chrome: {}, firefox: {} }, seleniumArgs: ['--port', '4446'] },
96-
(cp) => {
97-
if (cp.spawnargs && cp.spawnargs.some(containsChrome)) {
107+
(log) => {
108+
if (containsChrome(log)) {
98109
done(new Error('Chrome driver should not be loaded'));
99110
return false;
100111
}

0 commit comments

Comments
 (0)