protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Protractor file uploads - Support remote uploads with webdriver setFileDetector & LocalFileDetector

Open elgalu opened this issue 12 years ago • 26 comments

Right now it is perfectly possible to upload files for an input file element like this (Andres example)

var util = require('util');

it('should upload file', function() {
  var fileToUpload = '../some/path/foo.txt',
  var absolutePath = path.resolve(__dirname, fileToUpload);
  $('input[type="file"]').sendKeys(absolutePath);
  $('#uploadButton').click();
});

I've tested this on an ajax multi-file uploader and works! is awesome.

The issue is how to use this on a remote selenium grid like BrowserStack or Sauce Labs.

Since selenium 2.8.0 they have

Added setFileDetector to RemoteWebDriver to allow files to be uploaded to the remote server

But only java bindings seems to be available so i wonder if this is going to be possible on protractor.

The workaround i tried to use was uploading the target file to a publicly accessible server and then on the sendKeys() put the complete URI, e.g. http://my-server.com/fileToUpload.doc but that only works on Windows machines, linux complains with UnknownError: unknown error: path is not absolute: http://.... and OSX failed too.

elgalu avatar Mar 14 '14 02:03 elgalu

You're probably getting the path is not absolute errors because you're passing the URI to path.resolve - if using a URI, you should just be able to input that directly to sendKeys.

As for setFileDetector there's a current request for this in the nodejs bindings: https://code.google.com/p/selenium/issues/detail?id=6048&q=setFileDetector&colspec=ID%20Stars%20Type%20Status%20Priority%20Milestone%20Owner%20Summary

juliemr avatar Mar 28 '14 00:03 juliemr

Well, actually, I pasted the path.resolve code for the sake of an example. I'm not really using it. I do instead, something like this:

var absolutePath = 'http://secret-storage.s3.amazonaws.com/testing/templates/' + templName;
element(inputFile).sendKeys(absolutePath);

Thanks for pointing out the setFileDetector nodejs binding request!!

elgalu avatar Mar 28 '14 05:03 elgalu

I found a workaround, using MS Windows shares, i.e. Samba SMB share:

Setting samba file sharing on your ubuntu server

sudo apt-get install samba
sudo vim /etc/samba/smb.conf

Then I can set the file path like this:

\\your-server-domain.com\uploads-here

elgalu avatar May 30 '14 18:05 elgalu

There's an open pull request here but it looks like it's in limbo :( https://github.com/SeleniumHQ/selenium/pull/219

pmoriarty avatar Jun 24 '14 10:06 pmoriarty

@juliemr, when you say URIs should work, are you suggesting that something like the following should work through a remote selenium server?

    var fileInput = section.element(by.css('input[type="file"]'));
    var uri = '//some-random-image-domain/found-on-the-web.png';
    fileInput.sendKeys(uri);

I am having no luck with this.

mattgi avatar Jun 27 '14 06:06 mattgi

I think Julie meant a url starting with http like this 'http://secret-storage.s3.amazonaws.com/testing/templates/' in which case you should not use path.resolve

elgalu avatar Jun 27 '14 18:06 elgalu

When I do this (the code above, replaced with a http://blah uri), I receive the path is not absolute exception:

   Message:
     UnknownError: unknown error: path is not absolute: http://site.com/6d95af2a009c444e9877db6a9da58551/intro.gif
  (Session info: chrome=35.0.1916.153)
  (Driver info: chromedriver=2.9.248307,platform=Mac OS X 10.9.3 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 19 milliseconds
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:17:32'
System info: host: 'g.local', ip: '192.168.0.12', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9.3', java.version: '1.8.0_05'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, chrome={userDataDir=/var/folders/k1/tmslnft94tl4l3bb7w1k39l00000gn/T/.org.chromium.Chromium.Mxnsly}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=35.0.1916.153, platform=MAC, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 3bbb229cdb6d6bbab118802ab5c18d07
...
==== async task ====
WebElement.sendKeys([object Object])

I should add that I am running from Linux (a Bamboo build machine) and hitting a remote selenium server that is running on a Windows box (both hosted in AWS).

mattgi avatar Jun 28 '14 22:06 mattgi

Did anyone figure out a workaround to get this working? I am unable to get file upload working with my tests running on Sauce Labs. Everything works fine locally.

Sauce Labs recommends setFileDetector but from the pull request https://github.com/SeleniumHQ/selenium/pull/219 it looks like this is not going anywhere.

m3kessler1 avatar Jul 15 '14 20:07 m3kessler1

Whats going on with this issue? I'm also having the same problem as @mattgi this is my error log: UnknownError: unknown error: path is not absolute: http://lorempixel.com/g/600/600/ (Session info: chrome=40.0.2214.94) (Driver info: chromedriver=2.12.301326 (093c7e07b4a916b690e784b0374c7f618f1ea4be),platform=Mac OS X 10.9.4 x86_64)

I'm try to use sauce labs and nothing suggested here works.

snayagar avatar Feb 02 '15 23:02 snayagar

Hey @juliemr, the changes for the webdriver.js file detector code got merged this morning: https://github.com/SeleniumHQ/selenium/commit/db632150ebee29cb832fd480d4f4d85e4fa8b0d8 It would be awesome if we could get remote file uploads supported in the next version of protractor.

snayagar avatar Feb 11 '15 18:02 snayagar

We will update to a new version of selenium-webdriver as soon as it's available on npm!

juliemr avatar Feb 11 '15 18:02 juliemr

@juliemr and @snayagar I can't tell if the setFileDetector makes it to protractor 1.7.0 yet. It it is there, is the following code correct to set FileDectector?

browser.driver.setFileDetector(new browser.driver.remote.FileDetector); 

Based on the commit of: https://github.com/SeleniumHQ/selenium/commit/db632150ebee29cb832fd480d4f4d85e4fa8b0d8

jlin412 avatar Feb 24 '15 20:02 jlin412

Hi @juliemr , the method setFileDetector is supported in [email protected], but the dependency of protractor is still [email protected]. Could you update the version of selenium-webdriver? Thanks!

sasanala avatar Mar 03 '15 03:03 sasanala

Blocked on https://github.com/angular/protractor/issues/1872

juliemr avatar Mar 03 '15 04:03 juliemr

Hi, I'm wondering if setFileDetector related method available in the latest version of Protractor?

lmr3796 avatar Mar 11 '15 10:03 lmr3796

Can someone post an example of how to use setFileDetector? (unless not needed in this example) I am using Protractor, JS, selenium standalone server, and have a button to select a file, i.e. <button id="upload-button" class="button" ng-file-select ng-file-change="uploadCtrl.upload($files) ng-accept="'.jpg,.gif'">Select File

Can someone explain how to upload the file in protractor? Thanks!!

rfodge avatar Mar 25 '15 19:03 rfodge

button id="upload-button" class="button" ng-file-select ng-file-change="uploadCtrl.upload($files)" ng-accept="'*.jpg, *.gif'"

rfodge avatar Mar 25 '15 20:03 rfodge

Is there an example of a protractor spec for file upload running against saucelabs?

anuragsimgeker avatar Mar 25 '15 23:03 anuragsimgeker

Follow the issue https://github.com/angular/protractor/issues/1959 I try it but it seems that it is not working.

jlin412 avatar Mar 26 '15 01:03 jlin412

@juliemr ANy idea if this has happened? I am stuck with this now when I run in sauce too. Also is there an example of how to use? Thx!

srajagopalan1712 avatar Feb 05 '16 02:02 srajagopalan1712