Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle windows file upload window when using selenium

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
like image 613
Zoltorn Avatar asked May 16 '13 21:05

Zoltorn


1 Answers

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);
like image 80
Petr Janeček Avatar answered Sep 23 '22 18:09

Petr Janeček