Permalink
Browse files

FIX: Check file exist when attempting upload.

Both "page.uploadFile()" and "onFilePicker = function() {}" check file exists before accepting them.
If file don't exists, they are "ignored".

Fixes issue #941: https://code.google.com/p/phantomjs/issues/detail?id=941
  • Loading branch information...
1 parent a7a3928 commit 4dec091ac2ce63952867ca219268ab8bd2378de1 @detro detro committed Jan 17, 2013
Showing with 24 additions and 13 deletions.
  1. +17 −6 src/webpage.cpp
  2. +7 −7 test/webpage-spec.js
View
@@ -114,12 +114,16 @@ public slots:
QString chooseFile(QWebFrame *originatingFrame, const QString &oldFile) {
Q_UNUSED(originatingFrame);
- QString filePath = m_webPage->filePicker(oldFile);
- QString choosenFile = !filePath.isNull() ? filePath : m_uploadFiles.first();
+ // Check if User set a file via File Picker
+ QString chosenFile = m_webPage->filePicker(oldFile);
+ if (chosenFile == QString::null && m_uploadFiles.count() > 0) {
+ // Check if instead User set a file via uploadFile API
+ chosenFile = m_uploadFiles.first();
+ }
// Return the value coming from the "filePicker" callback, IFF not null.
- qDebug() << "CustomPage - file choosen for upload:" << choosenFile;
- return choosenFile;
+ qDebug() << "CustomPage - file chosen for upload:" << chosenFile;
+ return chosenFile;
}
void javaScriptAlert(QWebFrame *originatingFrame, const QString &msg) {
@@ -670,7 +674,7 @@ QString WebPage::filePicker(const QString &oldFile)
}
}
}
- return QString();
+ return QString::null;
}
bool WebPage::javaScriptConfirm(const QString &msg)
@@ -1125,7 +1129,14 @@ void WebPage::_uploadFile(const QString &selector, const QStringList &fileNames)
if (el.isNull())
return;
- m_customWebPage->m_uploadFiles = fileNames;
+ // Filter out "fileNames" that don't actually exist
+ m_customWebPage->m_uploadFiles.clear();
+ for (int i = 0, ilen = fileNames.length(); i < ilen; ++i) {
+ if (QFile::exists(fileNames[i])) {
+ m_customWebPage->m_uploadFiles.append(fileNames[i]);
+ }
+ }
+
el.evaluateJavaScript(JS_ELEMENT_CLICK);
}
View
@@ -513,9 +513,9 @@ describe("WebPage object", function() {
page.content = '<input type="file" id="file">\n' +
'<input type="file" id="file2" multiple>\n' +
'<input type="file" id="file3" multiple>';
- page.uploadFile("#file", 'README.md');
- page.uploadFile("#file2", 'README.md');
- page.uploadFile("#file3", ['README.md', 'LICENSE.BSD']);
+ page.uploadFile("#file", "run-tests.js");
+ page.uploadFile("#file2", "run-tests.js");
+ page.uploadFile("#file3", ["run-tests.js", "webpage-spec.js"]);
});
waits(50);
@@ -526,12 +526,12 @@ describe("WebPage object", function() {
fileName = page.evaluate(function() {
return document.getElementById('file').files[0].fileName;
});
- expect(fileName).toEqual('README.md');
+ expect(fileName).toEqual("run-tests.js");
fileName = page.evaluate(function() {
return document.getElementById('file2').files[0].fileName;
});
- expect(fileName).toEqual('README.md');
+ expect(fileName).toEqual("run-tests.js");
var files = page.evaluate(function() {
var files = document.getElementById('file3').files;
@@ -541,8 +541,8 @@ describe("WebPage object", function() {
}
});
expect(files.length).toEqual(2)
- expect(files.fileNames[0]).toEqual('README.md');
- expect(files.fileNames[1]).toEqual('LICENSE.BSD');
+ expect(files.fileNames[0]).toEqual("run-tests.js");
+ expect(files.fileNames[1]).toEqual("webpage-spec.js");
});
});

0 comments on commit 4dec091

Please sign in to comment.