Use Java Robot To Operate Download Popup Dialog In Selenium WebDriver

java.awt.Robot class is very useful when you need to control 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 application like Calculator, Notepad. Because selenium webdriver can not test OS based window application or popups directly, so java.awt.Robot can be used in such cases. This article will show you examples about how to use java.awt.Robot to handle download popup dialog for Firefox and Internet Explorer.

Robot Class Methods Introduction

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

  1. keyPress(): Simulate press down keyboard key. Example: robotObj.keyPress(KeyEvent.VK_TAB) : Press down 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 left key of your mouse.
  4. mouseRelease() : Simulate release up mouse key. Example: robotObj.mouseRelease(InputEvent.BUTTON3_DOWN_MASK) : Release up right key of your mouse.
  5. mouseMove() : Simulate move mouse pointer. Example: robotObj.mouseMove(point.getX(), point.getY()) : Move mouse pointer to a point located by X and Y coordinates.

Handle Firefox Download Popup Dialog

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

Handle Internet Explorer Download Popup Dialog

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

Source Code

  1. Download “TestPassDownloadPopupDialogUseRobot.zip” TestPassDownloadPopupDialogUseRobot.zip – Downloaded 114 times – 1 KB

READ :   How to run test cases using Internet Explorer in selenium automation script

Reference

  1. java.awt.Robot
  2. java.awt.event.KeyEvent
  3. java.awt.event.InputEvent
(Visited 2,900 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

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