https://i.sstatic.net/noq46.png
Here's a snippet of HTML code you might find useful:
<input type="file" class="fileUploadInput" name="fileUpload" id="fileUploadInput" accept="application/msword,application/pdf,text/plain,application/rtf,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.oasis.opendocument.formula" multiple="multiple" title="Choose File">
In the following code snippet, I use a method to upload a file through Protractor. However, even though there are no errors thrown, the file isn't being uploaded and the pop-up window isn't closing as expected:
browser.wait(function(){
return element(by.id('.filepicker_dialog_container')).isPresent();
}).then(function() {
browser.driver.switchTo().frame('.filepicker_dialog');
}).then(function(){
var fileToUpload = '/home/raghavendra/Desktop/f0657c76d96b9ddab5562b8391297dbbb01488fec4e79a4c13195aea.doc';
var absolutePath = protractor.basePath.resolve(__dirname, fileToUpload);
$("#fileUploadInput").sendKeys(absolutePath);
});
Below is the complete code showcasing how I successfully handle file uploads within a Protractor test:
var path = require('path');
var ptor = browser,
driver = browser.driver;
describe('Test Suite for Uploading Files', function() {
it('Click on the filepicker icon and upload a document', function () {
$('.icon-people').click();
browser.sleep(5000);
browser.driver.findElement(By.xpath('/html/body/div[4]/div/ng-view/div/div/div[2]/' +
'section/div/div/div[1]/form/div[2]/input')).sendKeys(group_name);
browser.sleep(5000);
element.all(by.css('.btn.btn-main')).click();
browser.sleep(5000);
browser.wait(function(){
return element(by.id('filepicker_dialog')).isPresent();
})
.then(function(){
ptor.switchTo().frame('filepicker_dialog');
})
.then(function(){
var fileToUpload = '/home/raghavendra/Desktop/50_resumes/f0657c76d96b9ddab5562b8391297dbbb01488fec4e79a4c13195aea.doc';
var absolutePath = path.resolve(__dirname, fileToUpload);
driver.findElement(By.id('fileUploadInput')).sendKeys(absolutePath);
})
.then(function(){
ptor.switchTo().defaultContent();
})
.then(function(){
browser.wait(function(){
var deferred = protractor.promise.defer();
element(by.id('filepicker_dialog')).isPresent()
.then(function(present){
deferred.fulfill(!present);
});
return deferred.promise;
});
});
});
})
If you encounter any issues or have any questions, please let me know.