org.unitils.selenium.downloader.impl.RobotDownloaderIE9 Maven / Gradle / Ivy
The newest version!
package org.unitils.selenium.downloader.impl;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.WebElement;
import org.unitils.core.UnitilsException;
import org.unitils.selenium.downloader.RobotDownloader;
/**
* This class downloads a file (IE 9).
* The files are downloaded to your downloads folder in your user folder.
*
* @author Glen Smith, Willemijn Wouters
*
* @since 1.0.8
*
* @see downloading files IE
*
*/
public class RobotDownloaderIE9 extends AbstractRobotDownloader implements RobotDownloader {
@Override
public void clickAndSaveFileIE(WebElement element) {
try {
Robot robot = new Robot();
// Get the focus on the element..don't use click since it stalls the driver
element.sendKeys("");
//simulate pressing enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(4000);
//robot.wait(robot.isAutoWaitForIdle());
// Wait for the download manager to open
//Thread.sleep(2000);
// Switch to download manager tray via Alt+N
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_ALT);
robot.delay(4000);
// Press S key to save
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.delay(4000);
// Switch back to download manager tray via Alt+N
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_ALT);
robot.delay(4000);
// Tab to X exit key
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
// Press Enter to close the Download Manager
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(4000);
} catch(AWTException e) {
throw new UnitilsException(e.getMessage(), e);
}
}
}