Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload file in angularjs e2e protractor testing

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

like image 734
Pawan Singh Avatar asked Jan 23 '14 10:01

Pawan Singh


People also ask

How do you upload multiple files on protractor?

But, according to webdriver:upload multiple files, you should be able to solve it in Chrome by joining file paths with a new-line character: uploadInput. sendKeys(absolutePath1 + "\n" + absolutePath2);

Does angular still use protractor?

Angular Team Announces End of Protractor Development By continuing to use Protractor, users may end up with disruptions in their automation scripts. However, the Protractor team has defined a timeline that gives users enough time to look into alternatives and migrate their tests accordingly.


1 Answers

This 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(); }); 
  1. Use the path module to resolve the full path of the file that you want to upload.
  2. Set the path to the input type="file" element.
  3. Click on the upload button.

This will not work on firefox. Protractor will complain because the element is not visible. To upload in firefox you need to make the input 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(); 
like image 196
Andres D Avatar answered Oct 01 '22 21:10

Andres D