Skip to content

Commit

Permalink
[js] For consistency with java, the file detector should ignore direc…
Browse files Browse the repository at this point in the history
…tory

paths.

Fixes #1814
  • Loading branch information
jleyba committed Mar 15, 2016
1 parent bfbe973 commit cf142b0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
5 changes: 5 additions & 0 deletions javascript/node/selenium-webdriver/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v2.53.1

* FIXED: for consistency with the other language bindings, `remote.FileDetector`
will ignore paths that refer to a directory.

## v2.53.0

### Change Summary
Expand Down
2 changes: 1 addition & 1 deletion javascript/node/selenium-webdriver/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "selenium-webdriver",
"version": "2.53.0",
"version": "2.53.1",
"description": "The official WebDriver JavaScript bindings from the Selenium project",
"license": "Apache-2.0",
"keywords": [
Expand Down
23 changes: 16 additions & 7 deletions javascript/node/selenium-webdriver/remote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,25 +381,34 @@ SeleniumServer.Options;
* @final
*/
class FileDetector extends input.FileDetector {
/** @override */
handleFile(driver, filePath) {
return promise.checkedNodeCall(fs.stat, filePath).then(function(stats) {
/**
* Prepares a `file` for use with the remote browser. If the provided path
* does not reference a normal file (i.e. it does not exist or is a
* directory), then the promise returned by this method will be resolved with
* the original file path. Otherwise, this method will upload the file to the
* remote server, which will return the file's path on the remote system so
* it may be referenced in subsequent commands.
*
* @override
*/
handleFile(driver, file) {
return promise.checkedNodeCall(fs.stat, file).then(function(stats) {
if (stats.isDirectory()) {
throw TypeError('Uploading directories is not supported: ' + filePath);
return file; // Not a valid file, return original input.
}

var zip = new AdmZip();
zip.addLocalFile(filePath);
zip.addLocalFile(file);
// Stored compression, see https://en.wikipedia.org/wiki/Zip_(file_format)
zip.getEntries()[0].header.method = 0;

var command = new cmd.Command(cmd.Name.UPLOAD_FILE)
.setParameter('file', zip.toBuffer().toString('base64'));
return driver.schedule(command,
'remote.FileDetector.handleFile(' + filePath + ')');
'remote.FileDetector.handleFile(' + file + ')');
}, function(err) {
if (err.code === 'ENOENT') {
return filePath; // Not a file; return original input.
return file; // Not a file; return original input.
}
throw err;
});
Expand Down
53 changes: 50 additions & 3 deletions javascript/node/selenium-webdriver/test/remote_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
// specific language governing permissions and limitations
// under the License.

var assert = require('assert');
'use strict';

var promise = require('../').promise;
var remote = require('../remote');
var assert = require('assert'),
fs = require('fs'),
path = require('path');

var promise = require('../').promise,
io = require('../io'),
cmd = require('../lib/command'),
remote = require('../remote');

describe('DriverService', function() {
describe('start()', function() {
Expand Down Expand Up @@ -70,3 +76,44 @@ describe('DriverService', function() {
}
});
});

describe('FileDetector', function() {
class ExplodingDriver {
schedule() {
throw Error('unexpected call');
}
}

it('returns the original path if the file does not exist', function() {
return io.tmpDir(dir => {
let theFile = path.join(dir, 'not-there');
return (new remote.FileDetector)
.handleFile(new ExplodingDriver, theFile)
.then(f => assert.equal(f, theFile));
});
});

it('returns the original path if it is a directory', function() {
return io.tmpDir(dir => {
return (new remote.FileDetector)
.handleFile(new ExplodingDriver, dir)
.then(f => assert.equal(f, dir));
});
});

it('attempts to upload valid files', function() {
return io.tmpFile(theFile => {
return (new remote.FileDetector)
.handleFile(
new (class FakeDriver {
schedule(command) {
assert.equal(command.getName(), cmd.Name.UPLOAD_FILE);
assert.equal(typeof command.getParameters()['file'], 'string');
return Promise.resolve('success!');
}
}),
theFile)
.then(f => assert.equal(f, 'success!'));
});
});
});

0 comments on commit cf142b0

Please sign in to comment.