This repository has been archived by the owner. It is now read-only.

Webpage.uploadFile not working in phantomjs 2.0 #12506

Closed
loduis opened this Issue Aug 26, 2014 · 39 comments

Comments

Projects
None yet
@loduis

loduis commented Aug 26, 2014

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

@ariya ariya added the Regression label Aug 27, 2014

@ariya

This comment has been minimized.

Show comment
Hide comment
@ariya

ariya Aug 27, 2014

Owner

This is a known regression.

Owner

ariya commented Aug 27, 2014

This is a known regression.

@burkestar

This comment has been minimized.

Show comment
Hide comment
@burkestar

burkestar Nov 4, 2014

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

burkestar commented Nov 4, 2014

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

@burkestar

This comment has been minimized.

Show comment
Hide comment
@burkestar

burkestar Nov 4, 2014

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 commented Nov 4, 2014

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).

@loduis

This comment has been minimized.

Show comment
Hide comment
@loduis

loduis Nov 5, 2014

Yes burkestar this works.

I also recompile from the source code

Thanks.

loduis commented Nov 5, 2014

Yes burkestar this works.

I also recompile from the source code

Thanks.

Drahakar added a commit to Drahakar/phantomjs that referenced this issue Jan 21, 2015

Deactivated the UserGestureIndicator
fixes issue #12506 ariya#12506

Since phantomJS is a headless browser, evaluating if the gesture comes
from a user is pointless. The UserGestureIndicator is now a dummy class
that always returns true.
@SpenserJ

This comment has been minimized.

Show comment
Hide comment
@SpenserJ

SpenserJ Jan 22, 2015

This has solved the issue for me as well. Thank you @burkestar 👍

SpenserJ commented Jan 22, 2015

This has solved the issue for me as well. Thank you @burkestar 👍

@mhoran

This comment has been minimized.

Show comment
Hide comment
@mhoran

mhoran Feb 4, 2015

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 commented Feb 4, 2015

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.

@xaviershay

This comment has been minimized.

Show comment
Hide comment
@xaviershay

xaviershay Feb 17, 2015

fwiw, poltergeist spec showing the error if anyone was looking for a cheap way to repro: teampoltergeist/poltergeist#585

xaviershay commented Feb 17, 2015

fwiw, poltergeist spec showing the error if anyone was looking for a cheap way to repro: teampoltergeist/poltergeist#585

@dmitryrechkin

This comment has been minimized.

Show comment
Hide comment
@dmitryrechkin

dmitryrechkin Mar 4, 2015

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 commented Mar 4, 2015

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?

asolntsev added a commit to codeborne/selenide that referenced this issue Mar 4, 2015

asolntsev added a commit to codeborne/selenide that referenced this issue Mar 4, 2015

@Nek-

This comment has been minimized.

Show comment
Hide comment
@Nek-

Nek- Mar 25, 2015

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

Thank you @burkestar

Nek- commented Mar 25, 2015

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

Thank you @burkestar

@vassilevsky

This comment has been minimized.

Show comment
Hide comment
@vassilevsky

vassilevsky Mar 27, 2015

Contributor

Works for me too. Thanks @burkestar!

Contributor

vassilevsky commented Mar 27, 2015

Works for me too. Thanks @burkestar!

@xaviershay

This comment has been minimized.

Show comment
Hide comment
@xaviershay

xaviershay Apr 11, 2015

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 commented Apr 11, 2015

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?

@stevepm

This comment has been minimized.

Show comment
Hide comment
@stevepm

stevepm May 8, 2015

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

stevepm commented May 8, 2015

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

@leeight

This comment has been minimized.

Show comment
Hide comment
@leeight

leeight May 27, 2015

Contributor

👍

Contributor

leeight commented May 27, 2015

👍

@leeight

This comment has been minimized.

Show comment
Hide comment
@leeight

leeight May 27, 2015

Contributor

👍

Contributor

leeight commented May 27, 2015

👍

@stevepm

This comment has been minimized.

Show comment
Hide comment
@stevepm

stevepm May 28, 2015

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 commented May 28, 2015

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

@dmitryrechkin

This comment has been minimized.

Show comment
Hide comment
@dmitryrechkin

dmitryrechkin May 28, 2015

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

dmitryrechkin commented May 28, 2015

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

@Vitallium

This comment has been minimized.

Show comment
Hide comment
@Vitallium

Vitallium May 28, 2015

Collaborator

Yes, 2.0.1 version will receive this fix

Collaborator

Vitallium commented May 28, 2015

Yes, 2.0.1 version will receive this fix

@dmitryrechkin

This comment has been minimized.

Show comment
Hide comment
@dmitryrechkin

dmitryrechkin May 28, 2015

Do you know when approximately 2.0.1 will be published?

dmitryrechkin commented May 28, 2015

Do you know when approximately 2.0.1 will be published?

@stevepm

This comment has been minimized.

Show comment
Hide comment
@stevepm

stevepm May 28, 2015

@dmitryrechkin I only made one commit. See here: stevepm@5f663b8

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

stevepm commented May 28, 2015

@dmitryrechkin I only made one commit. See here: stevepm@5f663b8

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

@jondkinney

This comment has been minimized.

Show comment
Hide comment
@jondkinney

jondkinney May 29, 2015

@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 commented May 29, 2015

@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?

@RamyaBNayak

This comment has been minimized.

Show comment
Hide comment
@RamyaBNayak

RamyaBNayak Jun 4, 2015

Could anyone please share the compiled exe for windows for this fix? I am having difficulty compiling.

RamyaBNayak commented Jun 4, 2015

Could anyone please share the compiled exe for windows for this fix? I am having difficulty compiling.

@chipmunkgeek

This comment has been minimized.

Show comment
Hide comment
@chipmunkgeek

chipmunkgeek Jun 5, 2015

👍 on the suggested patch. Thank you!

chipmunkgeek commented Jun 5, 2015

👍 on the suggested patch. Thank you!

@Vitallium Vitallium referenced this issue Jun 9, 2015

Closed

Release of PhantomJS 2.0.1 #12902

0 of 7 tasks complete

@Vitallium Vitallium added this to the Release 2.0.1 milestone Jun 9, 2015

@leopucci-zz

This comment has been minimized.

Show comment
Hide comment
@leopucci-zz

leopucci-zz Jul 7, 2015

Hi @stevepm , do you have a windows binary of your fork? (https://github.com/stevepm/phantomjs/tree/2.0) Could you share it?
Thanks

leopucci-zz commented Jul 7, 2015

Hi @stevepm , do you have a windows binary of your fork? (https://github.com/stevepm/phantomjs/tree/2.0) Could you share it?
Thanks

@skakri

This comment has been minimized.

Show comment
Hide comment
@skakri

skakri Jul 7, 2015

I have Ubuntu/Arch/OSX binaries @ https://github.com/skakri/phantomjs/releases/tag/2.0.1-regression-12506 , if someone needs those.

skakri commented Jul 7, 2015

I have Ubuntu/Arch/OSX binaries @ https://github.com/skakri/phantomjs/releases/tag/2.0.1-regression-12506 , if someone needs those.

@leopucci-zz

This comment has been minimized.

Show comment
Hide comment
@leopucci-zz

leopucci-zz Jul 7, 2015

Thanks @skakri, your build have the patch for upload file gesture 👍 i´ll try to build on the weekend.

leopucci-zz commented Jul 7, 2015

Thanks @skakri, your build have the patch for upload file gesture 👍 i´ll try to build on the weekend.

@skakri

This comment has been minimized.

Show comment
Hide comment
@skakri

skakri Jul 7, 2015

@leopucci if you manage to build it on Windows x68_64, feel free to send me a link to binary @ kristaps.karlsons@gmail.com. I'll add it to downloads.

skakri commented Jul 7, 2015

@leopucci if you manage to build it on Windows x68_64, feel free to send me a link to binary @ kristaps.karlsons@gmail.com. I'll add it to downloads.

@leopucci-zz

This comment has been minimized.

Show comment
Hide comment
@leopucci-zz

leopucci-zz Jul 7, 2015

Ok! :) 🎱

leopucci-zz commented Jul 7, 2015

Ok! :) 🎱

@astrosuf

This comment has been minimized.

Show comment
Hide comment
@astrosuf

astrosuf Jul 16, 2015

Hey,

has a windows patch for the upload file bug been created? Could someone direct me to the patch this will be extremely helpful!

astrosuf commented Jul 16, 2015

Hey,

has a windows patch for the upload file bug been created? Could someone direct me to the patch this will be extremely helpful!

@cantonic

This comment has been minimized.

Show comment
Hide comment
@cantonic

cantonic Jul 17, 2015

I compiled a binary for ubuntu 12.04 64bit using the source from @skakri on https://github.com/skakri/phantomjs/releases/tag/2.0.1-regression-12506
The ubuntu binary in the repo threw an error when using it on TravisCI: phantomjs: error while loading shared libraries: libicui18n.so.52: cannot open shared object file: No such file or directory.

I have uploaded the binary on https://github.com/cantonic/phantomjs/releases/tag/2.0.1-regression-12506

cantonic commented Jul 17, 2015

I compiled a binary for ubuntu 12.04 64bit using the source from @skakri on https://github.com/skakri/phantomjs/releases/tag/2.0.1-regression-12506
The ubuntu binary in the repo threw an error when using it on TravisCI: phantomjs: error while loading shared libraries: libicui18n.so.52: cannot open shared object file: No such file or directory.

I have uploaded the binary on https://github.com/cantonic/phantomjs/releases/tag/2.0.1-regression-12506

@leopucci-zz

This comment has been minimized.

Show comment
Hide comment
@leopucci-zz

leopucci-zz Jul 17, 2015

Sorry i did not had time to compile for windows yet as i told.

leopucci-zz commented Jul 17, 2015

Sorry i did not had time to compile for windows yet as i told.

@samir-mahendra

This comment has been minimized.

Show comment
Hide comment
@samir-mahendra

samir-mahendra Jul 17, 2015

@skakri Thanks for sharing the source. My colleague compiled it for windows, binary is on Dropbox here: https://www.dropbox.com/s/u71vipap8bnm72u/windows-x86_64-phantomjs.exe?dl=0

samir-mahendra commented Jul 17, 2015

@skakri Thanks for sharing the source. My colleague compiled it for windows, binary is on Dropbox here: https://www.dropbox.com/s/u71vipap8bnm72u/windows-x86_64-phantomjs.exe?dl=0

@skakri

This comment has been minimized.

Show comment
Hide comment
@skakri

skakri Jul 18, 2015

@samir-mahendra – thanks, added. @cantonic sorry, I've compiled it for Ubuntu 14.04.

skakri commented Jul 18, 2015

@samir-mahendra – thanks, added. @cantonic sorry, I've compiled it for Ubuntu 14.04.

@astrosuf

This comment has been minimized.

Show comment
Hide comment

astrosuf commented Jul 19, 2015

@samir-mahendra Thanks!!

@geminiyellow

This comment has been minimized.

Show comment
Hide comment
@geminiyellow

geminiyellow Jul 31, 2015

still not working.

geminiyellow commented Jul 31, 2015

still not working.

@samir-mahendra

This comment has been minimized.

Show comment
Hide comment
@samir-mahendra

samir-mahendra Jul 31, 2015

@geminiyellow Can you give more details on which OS you are using, and the specific test that fails for you? The test script I used to test if it's working is below. You have to run it as follows:

phantomjs --ssl-protocol=any testscript.js

You can then check if the file got uploaded by going to: https://encodable.com/uploaddemo/?action=listfiles

// testscript.js
var webpage = require('webpage'),
system = require('system'),
fname;

if (system.args.length !== 2) {
    console.log('Usage: uploadtest.js filename');
    phantom.exit(1);
} else {
    fname = system.args[1];
    var page = webpage.create();

    page.onConsoleMessage = function(msg) {
        console.log(msg);
    };

    page.open("https://encodable.com/uploaddemo/", function (status) {
        console.log("Page opened, ready to upload file.  Status is", status);
        page.uploadFile('input[name=uploadname1]', fname);
        page.evaluate(function () {
            document.forms[0].submit();
        });
        window.setTimeout(function () {
            phantom.exit();
        }, 3000);
    });
}

samir-mahendra commented Jul 31, 2015

@geminiyellow Can you give more details on which OS you are using, and the specific test that fails for you? The test script I used to test if it's working is below. You have to run it as follows:

phantomjs --ssl-protocol=any testscript.js

You can then check if the file got uploaded by going to: https://encodable.com/uploaddemo/?action=listfiles

// testscript.js
var webpage = require('webpage'),
system = require('system'),
fname;

if (system.args.length !== 2) {
    console.log('Usage: uploadtest.js filename');
    phantom.exit(1);
} else {
    fname = system.args[1];
    var page = webpage.create();

    page.onConsoleMessage = function(msg) {
        console.log(msg);
    };

    page.open("https://encodable.com/uploaddemo/", function (status) {
        console.log("Page opened, ready to upload file.  Status is", status);
        page.uploadFile('input[name=uploadname1]', fname);
        page.evaluate(function () {
            document.forms[0].submit();
        });
        window.setTimeout(function () {
            phantom.exit();
        }, 3000);
    });
}
@nottoseethesun

This comment has been minimized.

Show comment
Hide comment

nottoseethesun commented Aug 1, 2015

+1

@skakri

This comment has been minimized.

Show comment
Hide comment
@skakri

skakri Aug 4, 2015

Please stop spamming "+1", it adds clutter.

skakri commented Aug 4, 2015

Please stop spamming "+1", it adds clutter.

@Vitallium

This comment has been minimized.

Show comment
Hide comment
@Vitallium

Vitallium Aug 4, 2015

Collaborator

We have a proposal fix for this issue. And it will be landed in 2.0.1. Thanks for your patience.

Collaborator

Vitallium commented Aug 4, 2015

We have a proposal fix for this issue. And it will be landed in 2.0.1. Thanks for your patience.

Repository owner locked and limited conversation to collaborators Aug 4, 2015

@Vitallium

This comment has been minimized.

Show comment
Hide comment
@Vitallium

Vitallium Dec 12, 2015

Collaborator

Fixed in master.

Collaborator

Vitallium commented Dec 12, 2015

Fixed in master.

@Vitallium Vitallium closed this Dec 12, 2015

@zackw zackw modified the milestones: Release 2.0.1, Release 2.1 Dec 23, 2015

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.