How to upload a file in angularjs e2e protractor test

I want to test file upload using angularjs e2e test. How do you do this in e2e tests? I run my test script through rude karma.

+78
angularjs file file-upload gruntjs protractor
Jan 23 '14 at 10:22
source share
8 answers

Here is how I do it:

var path = require('path'); it('should upload a file', function() { var fileToUpload = '../some/path/foo.txt', absolutePath = path.resolve(__dirname, fileToUpload); element(by.css('input[type="file"]')).sendKeys(absolutePath); element(by.id('uploadButton')).click(); }); 
  • Use the path module to resolve the full path to the file you want to download.
  • Set the path to the input type = "file" element.
  • Click the download button.

This will not work on firefox. The protractor will complain because the item is not visible. To load in firefox you need to make the entry visible. This is what I do:

 browser.executeAsyncScript(function(callback) { // You can use any other selector document.querySelectorAll('#input-file-element')[0] .style.display = 'inline'; callback(); }); // Now you can upload. $('input[type="file"]').sendKeys(absolutePath); $('#uploadButton').click(); 
+121
Jan 23 '14 at 16:46
source share

You cannot directly.

For security reasons, you cannot simulate a user who selects a file in the system in a functional test suite such as ngScenario.

With Protractor, since it is based on WebDriver, it should be possible to use this trick

Q: Does WebDriver support file downloads? A: Yes.

You cannot directly interact with the file browser dialog of your own operating system, but we do some magic so that if you call WebElement # sendKeys ("/ path / to / file") for the file upload element, this does the right thing. Make sure you are not using WebElement # click () for the file upload element, otherwise the browser may freeze.

This works just fine:

 $('input[type="file"]').sendKeys("/file/path") 
+5
Jan 23 '14 at 12:09
source share

Here's a combo of tips by Andres D and davidb583 that would help me when I worked on this ...

I tried to get protractor tests performed against flowjs controls.

  // requires an absolute path var fileToUpload = './testPackages/' + packageName + '/' + fileName; var absolutePath = path.resolve(__dirname, fileToUpload); // Find the file input element var fileElem = element(by.css('input[type="file"]')); // Need to unhide flowjs secret file uploader browser.executeScript( "arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1", fileElem.getWebElement()); // Sending the keystrokes will ultimately submit the request. No need to simulate the click fileElem.sendKeys(absolutePath); // Not sure how to wait for the upload and response to return first // I need this since I have a test that looks at the results after upload // ... there is probably a better way to do this, but I punted browser.sleep(1000); 
+4
Jun 19 '15 at 17:29
source share
 var imagePath = 'http://placehold.it/120x120&text=image1'; element(by.id('fileUpload')).sendKeys(imagePath); 

This works for me.

+2
Oct 17 '16 at 7:33
source share

I realized that the file input in the test web application is only shown in Firefox when it scrolls to the window using JavaScript, so I added scrollIntoView () in Andres D code to make it work for my application:

  browser.executeAsyncScript(function (callback) { document.querySelectorAll('input')[2] .style = ''; document.querySelectorAll('input')[2].scrollIntoView(); callback(); }); 

(I also deleted all styles for the file input element)

0
Jan 28 '16 at 16:01
source share

This is what I do to upload the file in firefox, this script makes the element visible to set the path value:

  browser.executeScript("$('input[type=\"file\"]').parent().css('visibility', 'visible').css('height', 1).css('width', 1).css('overflow', 'visible')"); 
0
Oct 13 '16 at 20:36
source share

// Download file from C: \ Directory

{

 var path = require('path'); var dirname = 'C:/'; var fileToUpload = '../filename.txt'; var absolutePath = path.resolve('C:\filename.txt'); var fileElem = ptor.element.all(protractor.By.css('input[type="file"]')); fileElem.sendKeys(absolutePath); cb(); 

};

0
Feb 13 '18 at 21:57
source share

current documented solutions will only work if users download jQuery. I in different situations, users will get this error: Failed: $ not defined

I suggest documenting the solution using a custom corner code.

eg. I would suggest instead of suggesting:

  $('input[type="file"]') ..... 

to offer:

  angular.element(document.querySelector('input[type="file"]')) ..... 

the latter is more standard, on top of angular and more importantly not require jquery

-2
Jul 05 '15 at 0:03
source share



All Articles