phantomjs icon indicating copy to clipboard operation
phantomjs copied to clipboard

Webpage.uploadFile not working in phantomjs 2.0

Open loduis opened this issue 12 years ago • 39 comments

This is in linux (Ubuntu)


var
  page     = require('webpage').create(),
  filename = '/media/loduis/DATOS/projects/ghostdriver/test/javascript/local.jpg';



page.onFilePicker = function(oldFile) {

  console.log('Listen event.');

  return filename;
};


page.open('data:text/html,' +
  '<body><form method="POST" enctype="multipart/form-data">' +
  'File to upload: <input type="file" name="upfile" style="width: 200px;height: 100px; background: gray">' +
  '</form></body>', function(status) {

  console.log(status);
  page.uploadFile('input[name=upfile]', filename);

  page.render('test.png');
});

The result on phantomjs 1.9.7 is test2

while 2.0 is test

another important thing is that it is never fired onFilePicker

loduis avatar Aug 26 '14 20:08 loduis

This is a known regression.

ariya avatar Aug 27 '14 03:08 ariya

What is causing this issue? I'd like to contribute a fix but not sure where to start.

burkestar avatar Nov 04 '14 17:11 burkestar

Looks like I was able to track down the cause of the regression issue and I found a suitable workaround (although I'm not sure its the proper fix long-term).

Basically, a change made in Qt 4.8 and incorporated subsequently in Qt 5.0 prevented clicking on file input elements that were not caused by user gestures.

The following patch to comment out the 2 relevant lines in src/qt/qtwebkit/Source/WebCore/html/FileInputType.cpp circumvents that and allows file uploads to work with Phantom 2. I was able to comment those 2 lines and rebuilding Phantom 2 from source.

diff --git a/src/qt/qtwebkit/Source/WebCore/html/FileInputType.cpp b/src/qt/qtwebkit/Source/WebCore/html/FileInputType.cpp
index 7362f63..1a7b148 100644
--- a/src/qt/qtwebkit/Source/WebCore/html/FileInputType.cpp
+++ b/src/qt/qtwebkit/Source/WebCore/html/FileInputType.cpp
@@ -187,8 +187,9 @@ void FileInputType::handleDOMActivateEvent(Event* event)
     if (element()->isDisabledFormControl())
         return;

-    if (!ScriptController::processingUserGesture())
-        return;
+    // PATCH related to #12506 - commented out to allow programmatic form file uploads
+    //if (!ScriptController::processingUserGesture())
+    //    return;

     if (Chrome* chrome = this->chrome()) {
         FileChooserSettings settings;

Here's the bug that tipped me off to the underlying cause. Originally the filtering of clicks for user gestures was done in Source/WebCore/rendering/RenderFIleUploadControl.cpp in the ::click() method but this revision refactored that functionality out and put in FileInputType.cpp.

Since this is a patch, I'm not sure how such things should be handled long-term (i.e. to ensure the patch is re-applied with newer updates to QtWebKit pulled into this repository).

burkestar avatar Nov 04 '14 19:11 burkestar

Yes burkestar this works.

I also recompile from the source code

Thanks.

loduis avatar Nov 05 '14 14:11 loduis

This has solved the issue for me as well. Thank you @burkestar :+1:

SpenserJ avatar Jan 22 '15 02:01 SpenserJ

It would be great to get a fix for this rolled into upstream Qt. We had originally filed a bug in WebKit here: https://bugs.webkit.org/show_bug.cgi?id=75385. It was closed when all Qt related bugs were bulk closed in the WebKit bug tracker.

In capybara-webkit, we work around this by using native clicks to submit file uploads.

mhoran avatar Feb 04 '15 03:02 mhoran

fwiw, poltergeist spec showing the error if anyone was looking for a cheap way to repro: https://github.com/teampoltergeist/poltergeist/pull/585

xaviershay avatar Feb 17 '15 02:02 xaviershay

I've tried javascript code example from this issue in phantomjs 2 which is published on your website and upload file is not working. Was it fixed at all?

dmitryrechkin avatar Mar 04 '15 05:03 dmitryrechkin

The patch worked for me ! I can now upload via webdriver !

Thank you @burkestar

Nek- avatar Mar 25 '15 10:03 Nek-

Works for me too. Thanks @burkestar!

vassilevsky avatar Mar 27 '15 15:03 vassilevsky

Just updating that this isn't fixed in the latest version installed by brew.

Seems like we need to patch QT upstream? https://bugs.webkit.org/show_bug.cgi?id=75385 doesn't necessarily seem appropriate, since it was file in 2012 and this issue only just surfaced?

xaviershay avatar Apr 11 '15 00:04 xaviershay

Have there been any updates to fix this besides the patch?

stevepm avatar May 08 '15 17:05 stevepm

:+1:

leeight avatar May 27 '15 11:05 leeight

:+1:

leeight avatar May 27 '15 11:05 leeight

I forked and made the update and it works great for me if anyone wants to build the binary off of my fork: https://github.com/stevepm/phantomjs/tree/2.0

stevepm avatar May 28 '15 02:05 stevepm

Is it possible to merge your fix to the main repository? Which commit fixes this issue? Thanks

dmitryrechkin avatar May 28 '15 04:05 dmitryrechkin

Yes, 2.0.1 version will receive this fix

vitallium avatar May 28 '15 06:05 vitallium

Do you know when approximately 2.0.1 will be published?

dmitryrechkin avatar May 28 '15 06:05 dmitryrechkin

@dmitryrechkin I only made one commit. See here: https://github.com/stevepm/phantomjs/commit/5f663b8b39138c0510a7acac153a86492373988f

Also, just to be clear, all credit goes to burkestar for finding the fix.

stevepm avatar May 28 '15 06:05 stevepm

@stevepm thanks for pointing us at your fork. I'm going to try to compile a binary from your fork for OS X Yosemite. Unless someone else has already done this?

jondkinney avatar May 29 '15 13:05 jondkinney