In some scenarios, we will not able to use locators to find the location. In some times the application needs any data from the local. So, it will prompt any windows application like file explorer to the requested file. As we saw that selenium is only for web applications. To manage these scenarios Robot class will be required.
By using this Robot class all mouse and keyboard events can be used. This is the same as the Actions class and the only difference is this class helps to handle windows applications.
To use this class you need to import the below package and object need to be created for this class to access the methods
import java.awt.Robot; Robot robot = new Robot();
Methods used for Keyboard events
keyPress(key) and keyRelease(key)
The parameter passed in this method should be of keyCode. To mention the key event new package needs to be imported import java.awt.event.KeyEvent; these key events are like communication was made directly to the application and the key needs to be pressed should be passed to the method.
Controll key - robot.keyPress(KeyEvent.VK_CONTROL);
Shift - robot.keyPress(KeyEvent.VK_SHIFT);
Dollar - robot.keyPress(KeyEvent.VK_DOLLAR);
Alphabets - robot.keyPress(KeyEvent.VK_A);
Numbers - robot.keyPress(KeyEvent.VK_0);
keyPress method is for pressing any key and keyRelease is to release the pressed key. Based on the requirement, use the above methods.
Methods used for mouse events
mousePress(int buttons), mouseRelease(int buttons)
These methods are used to press the mouse buttons. These methods are taken a parameter of type int with a mask as Buttons.
Format
InputEvent.BUTTON1_DOWN_MASK : For mouse left -click
InputEvent.BUTTON2_DOWN_MASK : For mouse middle button click
InputEvent.BUTTON3_DOWN_MASK : For mouse right-click
Example
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
mouseMove(int X, int Y)
This method is used to move the mouse pointer in the external window. Coordinates needs to be passed in this method. To perform this below steps need to be followed
1. Navigate to the application and Click the file upload button in the application
2. Get the dimension of the windows application pop up using Dimension class. We can't able to use getLocation method as it is only for web application. To get the coordinates use the below command with type of Dimension
Dimension dimension = driver.manage().window().getSize();
3. Then get the width and height from dimension using methods dimension.getWidth() and dimension.getHeight()
4. Then based on the screen dimension add values to the width and height. Dimension varies from system to system
5. Pass the width and height values to the mouseMove method
Robot robot = new Robot();
Dimension dimension = driver.manage().window().getSize();
int width = dimension.getWidth();
int height = dimension.getHeight();
robot.mouseMove(width,height);
These are some of the most comman events foe keyboard and mouse. Apart from this there are lot of methods available in robot class. If any method is required feel free to comment the method. We will post that method soon.
Comments
Post a Comment