Jordan .Muscat Jordan .Muscat - 25 days ago 14
AngularJS Question

Uploading a file using Angular js - protractor e2e testing

i want to upload a file as an automated test, with the following code, it correctly selects the file & clicks the upload button, however nothing happens after the upload button is clicked, I believe its not triggering the javascript on the page:

var fileUpload = './testFile.txt',
absolutePath = path.resolve('./testFile.txt', fileUpload);
$('input[type="file"]').sendKeys(absolutePath);

element(by.model('documentFile'));

element(by.css('button[ng-click="uploadSelectedFiles()"]')).click();


Is there another method for this?

EDIT: I have managed to do this, thank you to who helped me, here is my code:

var fileUpload = './testFile.jpg',
absolutePath = path.resolve('/home/xxx/workspace/xxx/xxx/xxx/xxxxx/test/x/x/x/x/filesToUpload', fileUpload);

var uploadInput = $('input[type="file"]');
uploadInput.sendKeys(absolutePath);
uploadInput.submit();
element(by.css('button[ng-click="uploadSelectedFiles()"]')).click();

Answer

Instead of clicking "upload" button, send keys to the input to set the path and submit the form by clicking Submit button:

var uploadInput = $('input[type="file"]');

uploadInput.sendKeys(absolutePath);

var submitButton = element(by.xpath("//input[.='Submit']"));
submitButton.click();
Comments