5

I am trying to write selenium tests for a website using java. However, I have come across a problem when testing file uploading..

When I click the file upload button, it automatically opens the windows file upload. I have code working to put the text in the upload box successfully, it's just there is nothing I can do to stop the windows box from coming up automatically, and having the website not automatically open the windows file upload isn't really an option. From researching this subject I understand there is no way for selenium webdriver to handle this. So my question is this: what is a way I can simply close the upload window in an automated way?

I have tried the java robot class and it did not work. It waited until the upload window was closed before doing any of the commands I gave it (ALT-F4, clicking in an x-y position, etc)

Thanks in advance

EDIT:

wait.until(ExpectedConditions.elementToBeClickable(By.id(("addResourcesButton"))));
driver.findElement(By.id("addResourcesButton")).click();

//popup window comes up automatically at this point


try {
    Robot robot = new Robot();
    robot.mouseMove(875, 625);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (AWTException e) {
    e.printStackTrace();
}

//my attempt to move the mouse and click, doesn't move or click until after I close the windows upload box

String fileToUpload = "C:\\file.png";


WebElement uploadElement = driver.findElement(By.id("fileInput"));
uploadElement.sendKeys(fileToUpload);

//Takes the code and successfully submits it to the text area, where I can now upload it
7
  • Are you talking about Selenium RC, or WebDriver? The former has the attachFile() method, the latter uses sendKeys() (the link also has some tips about the Robot class usage which, if used properly, also always works). Commented May 17, 2013 at 6:26
  • My apologies, I am talking about WebDriver in particular here. The problem with the robot class is it just waits until the windows dialog closes before it runs anything I tell it to. I had a similar issue for an OS alert box and I was able to get the robot to work there, it just seems to be for the upload window that it won't run until after the upload window is closed. Commented May 17, 2013 at 15:03
  • Absolutely not. Could you show us your code? I can reliably use the Robot class to upload a file. Anyway, a better way is to use the sendKeys() method on the <input type='file'> element. Did you try that? Commented May 17, 2013 at 15:18
  • Ohhh, right. WebDriver doesn't return from the click() method until you closed the popup - that means you have to click on it with something else in order to use Robot. Interesting. Anyway, am I reading it right that the sendKeys() works? I am confused as to where the problem appears and what actually happens =/. Commented May 17, 2013 at 15:58
  • Everything works except I can't make the pop-up window go away automatically :P once the window closes the test performs correctly. Commented May 17, 2013 at 16:22

2 Answers 2

5

You can do a nonblocking click by using either one of these:

The Advanced User Interactions API (JavaDocs)

WebElement element = driver.findElement(By.whatever("anything"));
new Actions(driver).click(element).perform();

or JavaScript:

JavascriptExecutor js = (JavascriptExecutor)driver;

WebElement element = driver.findElement(By.whatever("anything"));
js.executeScript("arguments[0].click()", element);
Sign up to request clarification or add additional context in comments.

2 Comments

The first result worked for me, but I had to do an Actions.perform() afterwards. Thanks!
@user2246596 Yes, sorry about that, I forgot that one. Anyway, I'm glad I could help!
0

I have answered this for a similar question. There are other solutions provided for Upload - Like using AutoIT. But I personally would defer to interact with any OS specific dialogues. Interacting with OS specific dialogues would limit you to run the tests from a given environment.

Selenium webdriver java - upload file with phantomjs driver

Always identify & interact with elements of type "file" when uploads are concerned. This would solve your issue of pop ups.

Ex: In my application, upload related elements have the below DOM -

<a id="uploadFileButtonLink" class="uploadFileButtonLink" href="javascript:void(0)" data-uidsfdc="3" style="display: none;">Upload a file</a>
<input id="multiFileInput" class="multifile-upload-input-button" type="file" name="chatterFile_upload" multiple="multiple"/>
<input id="multiUploadBtn" class="btnImportant" type="button" value="Upload Files"/>

In this case, you can use sendKeys method to "multiFileInput" which is of type "file". This way it would work for all FF, Chrome & also headless browsers.

1 Comment

The thing is there is no way for me to not get the alert box. When I hit upload, it then takes me to a new page where I can then see the text field where I can put text into, but in addition to this it launches the windows upload box. I have it able to upload and send the file correctly, I just can't figure out what to do to get rid of that pop up window. Until now I have had to manually hit cancel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.