Protractor file uploads - Support remote uploads with webdriver setFileDetector & LocalFileDetector
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.
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
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!!
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
There's an open pull request here but it looks like it's in limbo :( https://github.com/SeleniumHQ/selenium/pull/219
@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.
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
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).
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.
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.
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.
We will update to a new version of selenium-webdriver as soon as it's available on npm!
@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
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!
Blocked on https://github.com/angular/protractor/issues/1872
Hi, I'm wondering if setFileDetector related method available in the latest version of Protractor?
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!!
button id="upload-button" class="button" ng-file-select ng-file-change="uploadCtrl.upload($files)" ng-accept="'*.jpg, *.gif'"
Is there an example of a protractor spec for file upload running against saucelabs?
Follow the issue https://github.com/angular/protractor/issues/1959 I try it but it seems that it is not working.
@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!