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
Zoltorn
  • 171
  • 1
  • 2
  • 10
  • Are you talking about Selenium RC, or WebDriver? The former has the [`attachFile()`](http://selenium.googlecode.com/svn/trunk/docs/api/java/com/thoughtworks/selenium/Selenium.html#attachFile%28java.lang.String,%20java.lang.String%29) method, the latter uses [`sendKeys()`](http://stackoverflow.com/a/10717319/1273080) (the link also has some tips about the `Robot` class usage which, if used properly, also always works). – Petr Janeček May 17 '13 at 06: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. – Zoltorn May 17 '13 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 `` element. Did you try that? – Petr Janeček May 17 '13 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 =/. – Petr Janeček May 17 '13 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. – Zoltorn May 17 '13 at 16:22
  • If I understand your problem correctly, you click the `addResourcesButton` button, you are redirected to a new page and there immediatelly the Open file dialog is opened. Is that correct? In that case, all we need is to click the `addResourcesButton` button with a nonblocking click - for which I'll gladly post an answer, because there are several possibilities. – Petr Janeček May 18 '13 at 22:44
  • That is exactly my problem. A non-blocking click should definitely resolve it - I just need to be able to get rid of the dialog box but as you stated before the click isn't returning until after I close the dialog myself. – Zoltorn May 20 '13 at 15:53

2 Answers2

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);
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
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.

Community
  • 1
  • 1
praneel
  • 1,842
  • 4
  • 19
  • 24
  • 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. – Zoltorn May 17 '13 at 15:06