Selenium | Use of Robot Class to handle Dialog box

Author - Webner
8.03.2016
|
0 Comments
||

When we write selenium script for downloading files over internet through Mozilla Firefox following dialog box appears to save or open the file:


Note: This dialog box cannot be handled with simple find element functions like:

find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

******So we have to use “Robot Class” ******

Some of the popular methods under Robot Class are:

.keyPress();
.mousePress();
.mouseMove();
.keyRelease();
.mouseRelease();

Example:

First of all create an object of the Robot Class:

Robot robot=new Robot();

1. .keyPress() :

robot.keyPress(KeyEvent.VK_ESC);

This will press Escape key on keyboard.

2. .keyRelease() :

robot.keyRelease(KeyEvent.VK_CAPS_LOCK);

This will release the CAPS_LOCK key

3. .mousePress() :

robot.mousePress(InputEvent.BUTTON1_MASK);

This will press Left mouse button.

4. .mouseRelease() :

robot.mouseRelease(InputEvent.BUTTON1_MASK);

This will release Left mouse button.

5. .mouseMove() :

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

This will move the mouse pointer to X and Y coordinates.

By the help of these methods into selenium webdriver we can handle the Dialog boxes over the browser.

Below is the piece of code that is used to handle the dialog box to save or open the file :

Robot robot = new Robot();
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
Thread.sleep(4000);
//This will press down key on keyboard i.e Save file (radio button)

robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(4000);
//This will press Enter key on keyboard i.e Allow to save file into the system with Ok button.
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

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