How to Handle Window based applications

Robot class enables selenium to use actual mouse, Actions class in selenium only simulates mouse, which means Actions class does not move the mouse cursor.

Robot class is very easy to use with automation process. It can be easily integrated with Java automation frameworks

Selenium does not provide support to handle window based pop-ups (like download popups, upload popups). Robot Class used while we need to handle file upload and download activity using selenium webDriver.

Useful Robot Class Methods for selenium webdriver

  • keyPress();
  • keyRelease();
  • mousePress();
  • mouseRelease();
  • mouseMove();
  • mouseWheel(wheelAmt);
  • delay(ms);
  • createScreenCapture();
  • keyPress() : Presses a given key, The key should be released using the keyRelease method.
    If there is more than one key present in keyboard for Key Code value( Like SHIFT, ALT, CTRL ) will map to the left key.
  • keyRelease() : Releases a given keyboard key, if there is more than one key robot class considers the left key.
  • mousePress() : Presses mouse button based on the input value
  • mousePress(1) : Presses Primary key
  • mousePress(2) : Presses Secondary key
  • mouseRelease() : Releases the mouse button
  • mouseMove() : Moves mouse pointer to given screen coordinates, used long with Mouse Press most of the time.
  • mouseWheel() : Scrolls specific amount of notches (check the wheel of the mouse), if the given value is Positive scrolled down, in case of positive value wheel scrolled up
  • delay(ms) : Makes the robot action to sleep for the specified time. To catch any InterruptedExceptions that occur, Thread.sleep() may be used instead.

Handle Download popUp using Robot class in selenium webdriver

We can handle the download pop up using Robot class, Just we have to press couple of TABS and ENTER keys.

Avoid using the mouse with robot class when you cannot find the exact location of the buttons. Location of button or the elements may change due to size of the screen we have for testing.

Example : Consider the Below download popup.
selenium-server-robot-download

To Handle above popUp, follow below steps:

  1. Once the popup is active, check which element is in active state.
  2. In above popup Save File radio button is active
  3. Calculate how may tabs it takes to reach Save File or OK button
  4. It takes two TABS to reach Save File button.
  5. Once we reach required button, press EnterKey.
  6. Download link Download Minion
    1. // PLEASE DO WRITE IMPORT STATEMENTS
      public class DownloadUsingRobot {
      	public static void main(String[] args) throws Exception {
      	// set the geckodriver.exe property
      			System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
      			// open firefox
      			WebDriver driver = new FirefoxDriver();
      			driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
      			// open web page
      			driver.get("http://chercher.tech/files/minion.zip");
      			Thread.sleep(3000);
      			// create object to robot class
      			Robot robot = new Robot();
      			// press tab first time
      			robot.keyPress(KeyEvent.VK_TAB);
      			// press tab second time
      			robot.keyPress(KeyEvent.VK_TAB);
      			// press enter key
      			robot.keyPress(KeyEvent.VK_ENTER);
      	}
      }
                              	

Capture Screenshot with Robot Class in Selenium

createScreenCapture Takes the screen shot of the given rectangle shape. This method accepts Rectangle type parameter.

Robot class takes screenshot irrespective of application, if application is minimize robot class takes screenshot of desktop.

// PLEASE DO WRITE IMPORT STATEMENTS
public class RobotClass {
	public static void main(String[] args) throws Exception {
		// set the geckodriver.exe property
		System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
		// open firefox
		WebDriver driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
		// open webpage
		driver.get("http://chercher.tech/practice/practice-pop-ups-selenium-webdriver");
		Robot robot = new Robot();
		int x = 10;
		int y = 10;
		int width = 500;
		int height = 200;
		Rectangle area = new Rectangle(x, y, width, height);
		BufferedImage bufferedImage = robot.createScreenCapture(area);
		File imageFile = new File("D:\\Robot.png");
		ImageIO.write(bufferedImage, "png", imageFile);
	}
}
                        				

Output :
robot-portion-selenium-webdriver

Full screenshot with Robot class in Selenium We can also take full screenshot of the Desktop (any application which is active currently).

// PLEASE DO WRITE IMPORT STATEMENTS
public class FullScreenCaptureExample {
  public static void main(String[] args) throws Exception {
  	
		// set the geckodriver.exe property
		System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
		// open firefox
		WebDriver driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
		// open webpage
		driver.get("http://chercher.tech/java/practice-pop-ups-selenium-webdriver");
		// create object to robt class
		Robot robot = new Robot();
		// create rectangle for full screenshot
		Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
		// capture screen of the desktop
		BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
		// save the screenshot to local system
		ImageIO.write(screenFullImage, "png", new File("D:\\FullScreenshotRobot.png"));
		 
		System.out.println("Full Desktop screenshot saved!");
  }
}
			                        	

Output :
robot-fullscreenshot-selenium

Take Full Screenshot in selenium

Difference between Actions class and Robot class

  • Actions class simulates Mouse and keyboard, Robot class enables the actual mouse and keyboard, because of this reason you can see the movement of mouse cursor.
  • Actions class does not affect parallel running but Robot class affects the parallel running as there is only one mouse connected to system
  • Actions class uses the native browser commands, Mouse class does not use browser based commands
  • Actions class limited to browser application, Robot class is can be used along with all the applications
  • Actions class is from org.openqa.selenium.interactions.Actions, Robot class is from java.awt.Robot
  • Dropdown Option selected or Not in selenium

Difference between Autoit and Robot class

  • AutoIT follows .au3 extension to execute it we need to convert the autoit script to .exe file, In case of Robot class we donot have to package it, Robot class can be used along the java code itself.
  • AutoIT is platform dependent and Robot class is not platform dependent (but java dependent)
  • AutoIT works based on the window's locator but Robot class does not use locators, Robot class just enables the mouse and keyword actions
  • AutoIT also used along with QTP/UFT, Robot class used along with Java tools
  • AutoIT used for lengthy process automation as well, but Robot class used for few steps only

Exception with Robot Class

Robot class throws few exceptions, we have to explicitly handle the exception with java.

AWTException - if the platform configuration does not allow low-level input control. This exception is always thrown when GraphicsEnvironment.isHeadless() returns true.

IllegalArgumentException - if screen is not a screen GraphicsDevice.

SecurityException - if createRobot permission is not granted

There are more than 40 exceptions present in selenium webdriver apart from awt exceptions

Read QR by taking as Screenshot in Selenium

Drawbacks of Robot Class in Automation

Though Robot class helps to handle window based popup, below drawbacks of robot class

  • MouseMove method is specific to dimension of the device screen size.
  • Screenshot method is not controlled by specific operation
  • Parallel running must be avoided as Robot class Executes actual mouse commands, so a computer cannot have two mice.
  • It may fail if permissions are not granted
  • It doesn't work well with selenium Grid/remote execution
  • Performs all actions on the active application on the desktop
  • When you use keyPress event of this class then you should use keyRelease event also to perform the short keys on to the browser.For example, you want to click Ctrl+C if you forget to add keyRelease event then it will remain pressed and consume memory in the background

Method Overloading in Selenium and java

HtmlUnitDriver for headless execution in Selenium webdriver

What jar to import to use robot class

by Kiruthiga

Please help us with Jar detail as well to use these classes

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook robto class vs autoit
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech