Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
Added support for multiple file uploads
Browse files Browse the repository at this point in the history
Added support for multiple file uploads

fixed scope of fileFieldSelector

fixed scope of fileFieldSelector

changed the check for file.path to check if length > 0 instead of 1

When file.path.length was 1 and file.path was an array(when uploading single file to a multiple input) the path would be set as a nested array. The updated behaviour will to do this only if the file.path.length is 0. This should throw a non-existent file error during paths.map

Reverted the matchEngine change

Skip the "file multiple" test on phantomjs 2.0.0

Test fails on phantomjs 2.0.0 due to a fakepath issue

Add tests for command line options

Specifically to catch options incorrectly sent to the engine instead of
the script. See #1384.

Fix assignment of options to engine vs script

Consider entire option name, not just initial chars. Fixes #1384.

fixed scope of fileFieldSelector

Reverted the matchEngine change

Skip the "file multiple" test on phantomjs 2.0.0

Test fails on phantomjs 2.0.0 due to a fakepath issue

Add tests for command line options

Specifically to catch options incorrectly sent to the engine instead of
the script. See #1384.

Fix assignment of options to engine vs script

Consider entire option name, not just initial chars. Fixes #1384.
  • Loading branch information
Sam Albuquerque committed Feb 4, 2016
1 parent b2c2621 commit c9066f3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
11 changes: 7 additions & 4 deletions modules/casper.js
Expand Up @@ -821,16 +821,19 @@ Casper.prototype.fillForm = function fillForm(selector, vals, options) {
if (!file || !file.path) {
return;
}
if (!fs.exists(file.path)) {
throw new CasperError('Cannot upload nonexistent file: ' + file.path);
}
var paths = (utils.isArray(file.path) && file.path.length > 0) ? file.path : [file.path];
paths.map(function(filePath) {
if (!fs.exists(filePath)) {
throw new CasperError('Cannot upload nonexistent file: ' + filePath);
}
},this);
var fileFieldSelector;
if (file.type === "names") {
fileFieldSelector = [selector, 'input[name="' + file.selector + '"]'].join(' ');
} else if (file.type === "css" || file.type === "labels") {
fileFieldSelector = [selector, file.selector].join(' ');
}
this.page.uploadFile(fileFieldSelector, file.path);
this.page.uploadFile(fileFieldSelector, paths);
}.bind(this));
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/site/field-file-multiple.html
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Form field file multiple test</title>
</head>
<body>
<form action="result.html" enctype="multipart/form-data">
<input type="file" name="files[]" multiple="multiple">
</form>
</body>
</html>
19 changes: 19 additions & 0 deletions tests/suites/casper/formfill.js
Expand Up @@ -201,6 +201,25 @@ casper.test.begin('multiple forms', 1, function(test) {
});
});

casper.test.begin('file multiple', 1, function(test) {
var fpaths = [fs.pathJoin(phantom.casperPath, 'README.md'),
fs.pathJoin(phantom.casperPath, 'LICENSE.md')
];

casper.start('tests/site/field-file-multiple.html', function() {
this.fillSelectors('form[action="result.html"]', {
'input[name="files[]"]': fpaths
});
if (!skipPhantom200(test, 1)) {
test.assertEval(function() {
return __utils__.findOne('input[type="file"]').files.length === 2;
});
}
}).run(function() {
test.done();
});
});

casper.test.begin('field array', 1, function(test) {
// issue #267: array syntax field names
casper.start('tests/site/field-array.html', function() {
Expand Down

0 comments on commit c9066f3

Please sign in to comment.