Thanks Mark for the great share. I would also like to add that there are 2 tools used for the purpose.
1 Autoit
2 Robot class of Java (imported from java.awt package)
I prefer using Robot which is less reliable but platform independent.
There could be the following scenarios that could be handled using Robot
1. Cancel: As we know that when the file download dialogue box appears
it means that there is the file which can be downloaded thus we can
directly cancel the pop-up.
2. Open: On getting the file download prompt, we can directly open the file.
3. Save: We can save the file for this we have 2 conditions:
A. Auto Path: In Browser we can make the setting which will save the
file at the pre-defined path (Usually in downloads folder).
B. Manual Path: User will have to give the path for saving the file.
The code is as follows:
try
{
Robot robot = new Robot();
robot.delay(1000);
robot.mouseMove(450, 400);
robot.delay(1000);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_ALT);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.delay(5000);
//Bring control back to the main window.
String winHandleBefore = fD.getWindowHandle();
fD.switchTo().window(winHandleBefore);
File screenshot = ((TakesScreenshot)fD).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("C:\\Screenshot\\"+System.currentTimeMillis()+".png"));
}
catch (Exception e)
{
e.printStackTrace();
}
You can make your changes in the code and play around.
--Vikas Garg