Skip to content

Commit cf142b0

Browse files
committed
[js] For consistency with java, the file detector should ignore directory
paths. Fixes #1814
1 parent bfbe973 commit cf142b0

File tree

4 files changed

+72
-11
lines changed

4 files changed

+72
-11
lines changed

javascript/node/selenium-webdriver/CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## v2.53.1
2+
3+
* FIXED: for consistency with the other language bindings, `remote.FileDetector`
4+
will ignore paths that refer to a directory.
5+
16
## v2.53.0
27

38
### Change Summary

javascript/node/selenium-webdriver/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "selenium-webdriver",
3-
"version": "2.53.0",
3+
"version": "2.53.1",
44
"description": "The official WebDriver JavaScript bindings from the Selenium project",
55
"license": "Apache-2.0",
66
"keywords": [

javascript/node/selenium-webdriver/remote/index.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,25 +381,34 @@ SeleniumServer.Options;
381381
* @final
382382
*/
383383
class FileDetector extends input.FileDetector {
384-
/** @override */
385-
handleFile(driver, filePath) {
386-
return promise.checkedNodeCall(fs.stat, filePath).then(function(stats) {
384+
/**
385+
* Prepares a `file` for use with the remote browser. If the provided path
386+
* does not reference a normal file (i.e. it does not exist or is a
387+
* directory), then the promise returned by this method will be resolved with
388+
* the original file path. Otherwise, this method will upload the file to the
389+
* remote server, which will return the file's path on the remote system so
390+
* it may be referenced in subsequent commands.
391+
*
392+
* @override
393+
*/
394+
handleFile(driver, file) {
395+
return promise.checkedNodeCall(fs.stat, file).then(function(stats) {
387396
if (stats.isDirectory()) {
388-
throw TypeError('Uploading directories is not supported: ' + filePath);
397+
return file; // Not a valid file, return original input.
389398
}
390399

391400
var zip = new AdmZip();
392-
zip.addLocalFile(filePath);
401+
zip.addLocalFile(file);
393402
// Stored compression, see https://en.wikipedia.org/wiki/Zip_(file_format)
394403
zip.getEntries()[0].header.method = 0;
395404

396405
var command = new cmd.Command(cmd.Name.UPLOAD_FILE)
397406
.setParameter('file', zip.toBuffer().toString('base64'));
398407
return driver.schedule(command,
399-
'remote.FileDetector.handleFile(' + filePath + ')');
408+
'remote.FileDetector.handleFile(' + file + ')');
400409
}, function(err) {
401410
if (err.code === 'ENOENT') {
402-
return filePath; // Not a file; return original input.
411+
return file; // Not a file; return original input.
403412
}
404413
throw err;
405414
});

javascript/node/selenium-webdriver/test/remote_test.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
var assert = require('assert');
18+
'use strict';
1919

20-
var promise = require('../').promise;
21-
var remote = require('../remote');
20+
var assert = require('assert'),
21+
fs = require('fs'),
22+
path = require('path');
23+
24+
var promise = require('../').promise,
25+
io = require('../io'),
26+
cmd = require('../lib/command'),
27+
remote = require('../remote');
2228

2329
describe('DriverService', function() {
2430
describe('start()', function() {
@@ -70,3 +76,44 @@ describe('DriverService', function() {
7076
}
7177
});
7278
});
79+
80+
describe('FileDetector', function() {
81+
class ExplodingDriver {
82+
schedule() {
83+
throw Error('unexpected call');
84+
}
85+
}
86+
87+
it('returns the original path if the file does not exist', function() {
88+
return io.tmpDir(dir => {
89+
let theFile = path.join(dir, 'not-there');
90+
return (new remote.FileDetector)
91+
.handleFile(new ExplodingDriver, theFile)
92+
.then(f => assert.equal(f, theFile));
93+
});
94+
});
95+
96+
it('returns the original path if it is a directory', function() {
97+
return io.tmpDir(dir => {
98+
return (new remote.FileDetector)
99+
.handleFile(new ExplodingDriver, dir)
100+
.then(f => assert.equal(f, dir));
101+
});
102+
});
103+
104+
it('attempts to upload valid files', function() {
105+
return io.tmpFile(theFile => {
106+
return (new remote.FileDetector)
107+
.handleFile(
108+
new (class FakeDriver {
109+
schedule(command) {
110+
assert.equal(command.getName(), cmd.Name.UPLOAD_FILE);
111+
assert.equal(typeof command.getParameters()['file'], 'string');
112+
return Promise.resolve('success!');
113+
}
114+
}),
115+
theFile)
116+
.then(f => assert.equal(f, 'success!'));
117+
});
118+
});
119+
});

0 commit comments

Comments
 (0)