Use Java Robot To Operate Download Popup Dialog In Selenium WebDriver

java.awt.Robot class is very useful when you need to control the mouse or keyboard to operate OS-based windows dialog such as Download popup, Print popup, etc. It can also be used to operate native OS GUI applications like Calculator, Notepad.

Because selenium webdriver can not test OS-based window applications or popups directly, so java.awt.Robot can be used in such cases. This article will show you examples of how to use the java.awt.Robot class to handle download popup dialog for Firefox and Internet Explorer.

1. java.awt.Robot Class Methods Introduction.

Robot class provides a lot of methods to simulate keyboard or mouse actions. Below is a list of major methods that are commonly used.

  1. keyPress(): Simulate press down a keyboard key. Example: robotObj.keyPress(KeyEvent.VK_TAB) : Press down the keyboard tab key.
  2. keyRelease(): Simulate release up keyboard key. Example: robotObj.keyRelease(KeyEvent.VK_DOWN) : Release up keyboard down arrow key.
  3. mousePress(): Simulate press down mouse key. Example : robotObj.mousePress(InputEvent.BUTTON1_DOWN_MASK) : Press down the left key of your mouse.
  4. mouseRelease(): Simulate release up mouse key. Example: robotObj.mouseRelease(InputEvent.BUTTON3_DOWN_MASK) : Release up the right key of your mouse.
  5. mouseMove(): Simulate move mouse pointer. Example: robotObj.mouseMove(point.getX(), point.getY()) : Move the mouse pointer to a point located by X and Y coordinates.

2. Handle Firefox Download Popup Dialog.

  1. Add the below java method to your java application and invoke it in your java application main method, it can handle the Firefox download popup dialog.
    /* Use java.awt.Robot class to operate Firefox download popup dialog. */
    public void passFirfoxDownloadDialogUseRobot()
    {
    	WebDriver ffDriver = null;
    	try
    	{			
    		String downloadUrl = "http://www.dev2qa.com/download/326/";
    			
    		// Initiate Firefox WebDriver object.
    		ffDriver = new FirefoxDriver();			
    		
    		// Navigate to the download url.
    		ffDriver.navigate().to(downloadUrl);
    		
    		Robot robotObj = new Robot();	
            
    		// Press arrow down key to select save radio button.
    		Thread.sleep(2000);	
    		robotObj.keyPress(KeyEvent.VK_DOWN);
    		robotObj.keyRelease(KeyEvent.VK_DOWN);
    		
    		// Press tab key three time to navigate to Save button. 
    		for(int i=0;i<3;i++)
    		{
    			Thread.sleep(2000);	
    			robotObj.keyPress(KeyEvent.VK_TAB);
    		}
    		
    		// Press down Save button.
                    Thread.sleep(2000);	
                    robotObj.keyPress(KeyEvent.VK_ENTER);
            
                    // Release up Save button, download process start.
                    Thread.sleep(2000);
    	        robotObj.keyRelease(KeyEvent.VK_ENTER);
    	        
    	}catch(Exception ex)
    	{
    		ex.printStackTrace();
    	}finally
    	{
    		if(ffDriver!=null)
    		{
    			ffDriver.close();
    			ffDriver = null;
    		}
    	}
    }
    

3. Handle Internet Explorer Download Popup Dialog.

  1. Add the below java method in your java application and call it in your java application main method to handle the Internet Explorer download popup dialog.
    /* Use java.awt.Robot class to operate Internet Explorer download popup dialog. */
    public void passIEDownloadDialogUseRobot()
    {
    	InternetExplorerDriver ieDriver = null;
    	try
    	{			
    		String downloadUrl = "http://www.dev2qa.com/download/326/";
    					
    		String ieDriverFilePath = "C:\\Workspace\\dev2qa.com\\Lib\\IEDriverServer_x64_3.4.0\\IEDriverServer.exe";
    		//Specify the executable file path to sysem property.
    		System.setProperty("webdriver.ie.driver", ieDriverFilePath);
    			
    		//Initiate internet explorer web browser
    		ieDriver = new InternetExplorerDriver();
    		ieDriver.navigate().to(downloadUrl);
    			
    		Robot robotObj = new Robot(); 	
    	        
    	        // Press tab key twice, goto save button.
    		for(int i=0;i<2;i++)
    		{
    			Thread.sleep(2000);	
    			robotObj.keyPress(KeyEvent.VK_TAB);
    		}
    			
    		// Press down save button.
    	        Thread.sleep(2000);	
    	        robotObj.keyPress(KeyEvent.VK_ENTER);
    	        // Release up save button to start download process.
    	        Thread.sleep(2000);
    	        robotObj.keyRelease(KeyEvent.VK_ENTER);
    	        
    	        //Press tab key twice, goto open download folder button.
    		for(int i=0;i<2;i++)
    		{
    			Thread.sleep(2000);	
    			robotObj.keyPress(KeyEvent.VK_TAB);
    		}
    			
    		// Press down open download folder button.
    	        Thread.sleep(2000);	
    	        robotObj.keyPress(KeyEvent.VK_ENTER);
    	        // Release up open download folder button, then download folder opened.
    	        Thread.sleep(2000);
    	        robotObj.keyRelease(KeyEvent.VK_ENTER);
    	        
    	        // Close the opened download folder.
    	        Thread.sleep(2000);
    	        robotObj.keyPress(KeyEvent.VK_CONTROL);
    	        robotObj.keyPress(KeyEvent.VK_W);
    	        robotObj.keyRelease(KeyEvent.VK_W);
    	        robotObj.keyRelease(KeyEvent.VK_CONTROL);
    	        
    	}catch(Exception ex)
    	{
    		ex.printStackTrace();
    	}finally
    	{
    		if(ieDriver!=null)
    		{
    			ieDriver.close();
    			ieDriver=null;
    		}
    	}
    }

References

  1. java.awt.Robot
  2. java.awt.event.KeyEvent
  3. java.awt.event.InputEvent
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x