Reputation: 203
I'm looking to upload a file using Protractor testing.
Ideal scenario:
createJobTest.js
it('should click upload file button', function() {
createJobPage.step5UploadFile.click().then(function(){
browser.waitForAngular();
});
});
})
var path = require('path');
it('should upload a file', function() {
var fileToUpload = '../desktop/test.txt',
absolutePath = path.resolve(__dirname, fileToUpload);
$('input[type="file"]').sendKeys(absolutePath);
$('#uploadButton').click();
});
createJobPage.js
this.step5UploadFile = element(by.id('step5--upload-file'));
createJob.html
<span class="btn btn-blue-one btn-upload" id="step5--upload-file" flow-btn translate="uploadfile"></span>
The upload file window opens but I'm getting this error:
Failed: No element found using locator: By(css selector, input[type="file"])
Any help much appreciated!
Upvotes: 1
Views: 2065
Reputation: 74
1 way is to sendkeys.
var fileToUpload =filepath;
var absolutePath = path.resolve(__dirname, fileToUpload); // absolute path
var fileElement = element(by.css('input[type="file"]'));
browser.executeScript("arguments[0].style.visibility = 'visible'; ", fileElement.getWebElement());
fileElement.sendKeys(absolutePath);
Another way is to operate on the desktop window . Which is the main functionality. You can refer the answer from the below link which i already answered.
This is what i am using in my application.
Upvotes: 1
Reputation: 41
var path = require('path');
var fileToUpload = '../desktop/test.txt';
var absolutePath = path.resolve('__dirname', fileToUpload);
$('input[type="file"]').sendKeys(absolutePath);
browser.driver.sleep(100);
Upvotes: 0