How to close file upload dialogue after click on upload button in protractor

Viewed 5597

I want to upload an image file in protractor. the problem is i can not get "input[type = "file"]" element without clicking on upload button.

when I click upload button, file upload dialogue popup opens.

i have tried

browser.actions().sendKeys(protractor.Key.ESCAPE).perform(); but it does not work.

This is what I am doing:

var image = '../images/image1.png';
var absPathImg = path.resolve(__dirname, image);
element(by.id('uploadImage')).click();
browser.actions().sendKeys(protractor.Key.ESCAPE).perform();
element(by.css('input[type=file]')).sendKeys(absPathImg);
element(by.id('upload')).click();

How can i close that file upload dialogue to upload an image?

3 Answers

This is an old thread but I'll answer in case anyone else runs to this problem.

I had to solve this same problem today. I was able to work around opening the upload dialog, because I am quite sure it cannot be closed without some external libraries and that would not work in headless mode.

You need to inspect the javascript linked to the upload button's click event and execute some script before sending keys to the file input.

This is individual to each case, but in my case i had to do this (Python):

script = (
        "var url = '/Contacting/UploadCsv'; "
        "$('#buttonFileUpload').data('url', url);"
        )
driver.execute_script(script)
driver.find_element_by_id('buttonFileUpload').send_keys(csv_path)

I picked that script from the JS function linked to the button.

Related