Robot class & Auto IT

  1. Robot Class can simulate Keyboard and Mouse Event

  2. Robot Class can help with upload/download of files when using a selenium web driver

  3. Robot Class can easily be integrated with the current automation framework (keyword, data- driven or hybrid)

  • While automating a web based application we will come across many specific scenarios where any selenium command is launched but actual event is not fired.

  • This causes termination of execution of test and it reports some errors. Here Java Robot Class comes into the picture.

  • Using Robot Class we can simulate keyboard and mouse events in Selenium. This class is very easy to use with automation process. It can be easily integrated with the current automation framework.

Let’s See an example of GMAIL where we have to automate below test cases:

    Step 1) Open URL Gmail

    Step 2) Enter Valid Login Credentials

    Step 3) Verify the Gmail has Logged in Successfully

    Step 4) Click on Compose

    Step 5) Enter to Address in Compose mail

    Step 6) Enter Subject in Compose Mail

    Step 7) Click on Attachment link

    Step 8) Attach any file (JPG or doc) from your local machine.

    Step 9) click on Send

    Step 10) Verify for the success message

  • Now in the above test cases we can automate the entire ‘Test’ step with selenium web driver except ‘Attaching’ a file. Since selenium can only recognize web based application we have to use a separate tool like AutoIT to choose a file from local machine to upload.

  • Here ‘Java Robot’ Class also comes in picture. Using ‘Robot Class’ we can simulate keyboard and mouse events in Selenium.

  • Robot class is used to (generate native system input events) take the control of mouse and keyboard. Once we get the control, we can do any type of operation related to mouse and keyboard through Java code.

  • ‘Robot Class’ is available under ‘java.awt.package’.

  • The packages that we need to import while working in Robot class are as follows:

  • The very first step should be creating an Object of robot class.

  • Methods in Robot Class can be effectively used to make the interaction with popups in Web Applications.

  • Selenium does not provide support to handle browser pop-ups or the native operating system pop-ups. To handle these kind of pop-up, take the help of Robot Class.

  • This is also used while we need to handle file upload and download activity using selenium web Driver.

There are different Methods used in robot class.
Some of the popular methods are:

  1. .keyPress();

  2. .mousePress();

  3. .mouseMove();

  4. .keyRelease();

  5. .mouseRelease();

  1. .keyPress()

    1. robot.keyPress(KeyEvent.VK_ESC);

    2. This will press Escape key on the keyboard.

  2. .mousePress()

    1. robot.mousePress(InputEvent.BUTTON1_MASK);

    2. This will press Left mouse button.

  3. .mouseMove()

    1. robot.mouseMove(coordinates.getX(), coordinates.getY());

    2. This will move the mouse pointer to X and Y co-ordinates.

  4. .keyRelease()

    1. robot.keyRelease(KeyEvent.VK_CAPS_LOCK);

    2. This will release the CAPS_LOCK key.

  5. .mouseRelease()

    1. robot.mouseRelease(InputEvent.BUTTON1_MASK);

    2. This will release Left mouse button.

Note: keyPress will send an event that a key has been pressed down. For Releasing the press effect on the key if we have pressed a key using robot.keyPress(KeyEvent.VK_CONTROL), then we should Release it too. Otherwise once our Java application runs, keyboard will continue with virtually pressed CTRL key.

Selenium cannot handle file downloading because browsers use native dialogs for downloading files. Sometime we need to download file from AUT (Application under Test). There are several ways to automate download file in Selenium but here we see download file using AutoIT in Selenium WebDriver.

Follow the below steps:

  1. Download AutoIT tool

  2. autoit tool

  3. Now install both whatever we have downloaded.

  4. autoit tool

Open Programs → AutoIT tool → SciTE Script Editor and add the below mentioned AutoIT script in AutoIT editor and save it as ‘DownloadFile.au3’ in your system

autoit window info

Explanation for above

AutoIT Script Explanation:

  1. Line 1 : WinWait(“[CLASS:#MozillaDialogClass]”,””,8)

    1. Wait for 8 seconds to appear download and save dialog. Used class property of download dialog.

    2. MozillaDialogClass – we brought this from finder tool.

    3. How to open finder tool.

    4. autoit window info

      autoit window info

    5. Drag highlighted box to download window. And OK button you will see this MozillaDialogClass class.

  2. Line 2 : Send(“!s”)

    1. Perform keyboard ALT key down + s + ALT key up action to select Save File Radio button using keyboard shortcut.

  3. Line 3 : Sleep(9000)

    1. Wait for 9 seconds

  4. Line 4: Send(“{ENTER}”)

    1. After that it downloads the document

  5. Once the file is saved, we need to convert the ‘DownloadFile.au3’ to ‘DownloadFile.exe’. To do this we need to compile the ‘DownloadFile.au3’, right click and select option as below.

    autoit window info

  6. In Eclipse, add the below mentioned Selenium Script and run. Write below program to download syllabus of selenium from

  7. Project structure looks like this.

    autoit window info