After some recent Certified ScrumMaster training with AxisAgile, I’ve been really challenged to beef up my integration testing Kungfu. I’ve always found Selenium a little cumbersome from an API viewpoint, but I’ve recently discovered Selenide and have become a huge fan of (IMHO) the cleaner and more concise Selenide API.

But one recent snag I’ve hit was around downloading files in Internet Explorer 9. There is no popup dialog any more, only an in-window “Download Manager” (scan for the term here). Turns out you can get access to this window via Alt+N, so if you’re driving via a java.awt.Robot, that’s the magic you’ll need.

A good discussion of several approaches to the problem can be found on StackExchange,  and I’ve tweaked StatusQuo’s solution there to make things play nice with the new IE9 Download Manager magic:

public void clickAndSaveFileIE(WebElement element) throws AWTException, InterruptedException {

    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);

    // 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);

    // Press S key to save            
    robot.keyPress(KeyEvent.VK_S);
    robot.keyRelease(KeyEvent.VK_S);
    Thread.sleep(2000);

    // 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);

    // Tab to X exit key
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);

    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);

    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);

}

 

A bit of magic around the Alt+N, a few strategic tabs, and you’re off and running. And if you need to get access to the actually downloaded file, then a little strategic pre-fetch deleting and asserting over your downloads directory…

File downloadsDir = new File(System.getenv("USERPROFILE") + File.separator + "Downloads");

and you can have a look at how the download file turned up!

Not as elegant as the very cool Apache Client/Cross-browser/Cookie-switching magic found on this blog, but for a quick IE9 smoke test, Robot works a treat!

Oh, and if you haven’t already, you should definitely check out Selenide