5

I am using https://github.com/danialfarid/ng-file-upload for file upload. I have to test it so I wrote protractor test case but it not working.

Code

<div class="col-lg-12 up-buttons">
     <div ng-file-select="" ng-model="files" ng-model-rejected="rejFiles" class="btn btn-default" ng-multiple="false" ng-accept="'text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'"  ng-model-rejected="rejFiles" tabindex="0">Choose file</div>
</div>

Test case

it('upload file', function(){   
  var fileToUpload = 'C:/Users/Anusha/Desktop/demo.csv';
  var absolutePath = path.resolve(fileToUpload);
  $('input[type="file"]').sendKeys(absolutePath);
  browser.sleep(1500);
})

testcase result

I can upload file but it is received in rejFiles model instead of files model eventhough file format is correct one. Could anyone please suggest me how to do this?

Anusha Nilapu
  • 1,243
  • 8
  • 24
  • 36

4 Answers4

4

Html

<div class="col-lg-12 up-buttons">
    <div ng-file-select="" ng-model="files" ng-model-rejected="rejFiles" class="btn btn-default" ng-multiple="false" ng-accept="'*.csv'"  ng-model-rejected="rejFiles" tabindex="0">Choose file</div>
</div>

Test case

var path = require('path');
var fileToUpload = file_path;
var absolutePath = path.resolve(fileToUpload);
element.all(by.css('input[type="file"]')).then(function(items) {
  items[0].sendKeys(absolutePath);
});
browser.sleep(500);
Anusha Nilapu
  • 1,243
  • 8
  • 24
  • 36
3

There is an issue with runtime DOM element change. So we can get element by element's runtime changed property and for file uploading case, DOM changes a property with 'input[type="file"]'.No matter if there is input tag for uploading element.

element(by.css('input[type="file"]')).sendKeys(absolutePathOfFile);
2

I struggled with this for some time but found that I had to trigger a click on the ngf-select element before trying to sendKeys.

it('upload file', function(){
  var fileToUpload = 'C:/Users/Anusha/Desktop/demo.csv';
  var absolutePath = path.resolve(fileToUpload);
  var button = element(by.css('[ng-file-select]');
  button.click();
  var input = element(by.css('input[type="file"]'));
  input.sendKeys(absolutePath);
});

The input is added by ng-file-upload at the bottom of the page after the ngf-select directive button is clicked.

Also, I was able to use relative paths (relative to the location of the spec file) for the file like this:

it('upload file', function(){
  var path = require('path');
  var fileToUpload = '../demo.csv';
  var absolutePath = path.resolve(__dirname, fileToUpload);
  var button = element(by.css('[ng-file-select]');
  button.click();
  var input = element(by.css('input[type="file"]'));
  input.sendKeys(absolutePath);
});

This assumes you have run npm install path --save-dev.

This is using Protractor 2.0.0 and ng-file-upload 4.0.4

thetallweeks
  • 6,875
  • 6
  • 25
  • 24
0

Absolute path has helped me:

var fileToUpload = 'D:/file.csv';
var absolutePath = path.resolve(fileToUpload);
$('input[type="file"]').sendKeys(absolutePath);
element(by.css('#doUpload')).click();

(Also discuessed here)

Community
  • 1
  • 1
Marko Jurisic
  • 844
  • 9
  • 10