- Robot Class in Java
- Advantages of Robot Class :
- Limitations of Robot Class :
- What is robot class in java
- Constructor Summary
- Method Summary
- Methods inherited from class java.lang.Object
- Constructor Detail
- Robot
- Robot
- Method Detail
- mouseMove
- mousePress
- mouseRelease
- mouseWheel
- keyPress
- keyRelease
- getPixelColor
- createScreenCapture
- isAutoWaitForIdle
- setAutoWaitForIdle
- getAutoDelay
- setAutoDelay
- delay
- waitForIdle
- toString
- What is robot class in java
- Constructor Summary
- Method Summary
- Methods declared in class java.lang.Object
- Constructor Detail
- Robot
- Robot
- Method Detail
- mouseMove
- mousePress
- mouseRelease
- mouseWheel
- keyPress
- keyRelease
- getPixelColor
- createScreenCapture
- createMultiResolutionScreenCapture
- isAutoWaitForIdle
- setAutoWaitForIdle
- getAutoDelay
- setAutoDelay
- delay
- waitForIdle
- toString
Robot Class in Java
Today we are learning about Robot class in Java. Robot class is a package of Java AWT. Java AWT(Abstract Window Toolkit) is a java functional class that provides a GUI(Graphical user interface) for a java program.
Robot class is used to build an environment so we can take system input events to test automation or to build other application which requires control over the mouse and keyboard. In JDK 1.3 Robot class is introduced as a feature to take control over the mouse and keyboard.
The library required to import the Robot class in our program is given below:
The robot class object is required to invoke its methods. So, let’s instantiate the Robot class.
Now discuss some methods of the robot class.
robot.mousePress(int buttons)// To press mouse buttons robot.mouseRelease(int buttons)// to release mouse key/button which was pressed earlier if we don't call //release method then the key will remain pressed until we terminate the program or we call the release method. robot.mouseMove(int x, int y)// to move the mouse cursor on the screen according to the x and y coordinates. robot.mouseWheel(int wheelAmt)// To scroll the mouse wheel according to the given amount. robot.keyPress(int keycode) // To press a specific key of keyboard robot.keyRelease(int keycode)// To release a specific key of the keyboard robot.createScreenCapture(Rectangle screenRect) // Captures a screenshot in a rectangle shape robot.getPixelColor(int x, int y)// It returns the color of a pixel at a specific robot.setAutoDelay(int ms)// it sets the delay for the given amount of milliseconds.
Advantages of Robot Class :
- Provides control over the Mouse and keyboard events.
- Using Robot class we can interact with Operating system pop-ups which are not possible with Selenium web driver API.
- The Robot class code or programs are easy to consume in the java Selenium scripts as the robot class is part of the Java package.
Limitations of Robot Class :
- The methods mentioned above to control Keyboard and Mouse have some limitations.
- Most of the methods like mouse move are dependent on the screen resolution.
- Using Robot class methods or function switching between screens or windows is difficult.
Thank you for reading this blog.
What is robot class in java
This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations. Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform’s native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events. Note that some platforms require special privileges or extensions to access low-level input control. If the current platform configuration does not allow input control, an AWTException will be thrown when trying to construct Robot objects. For example, X-Window systems will throw the exception if the XTEST 2.2 standard extension is not supported (or not enabled) by the X server. Applications that use Robot for purposes other than self-testing should handle these error conditions gracefully.
Constructor Summary
Method Summary
Methods inherited from class java.lang.Object
Constructor Detail
Robot
Robot
public Robot(GraphicsDevice screen) throws AWTException
- share the same coordinate system to form a combined virtual screen
- use different coordinate systems to act as independent screens
If screen devices are reconfigured such that the coordinate system is affected, the behavior of existing Robot objects is undefined.
Method Detail
mouseMove
public void mouseMove(int x, int y)
mousePress
public void mousePress(int buttons)
Presses one or more mouse buttons. The mouse buttons should be released using the mouseRelease(int) method.
- If support for extended mouse buttons is disabled by Java then it is allowed to use only the following standard button masks: InputEvent.BUTTON1_DOWN_MASK , InputEvent.BUTTON2_DOWN_MASK , InputEvent.BUTTON3_DOWN_MASK .
- If support for extended mouse buttons is enabled by Java then it is allowed to use the standard button masks and masks for existing extended mouse buttons, if the mouse has more then three buttons. In that way, it is allowed to use the button masks corresponding to the buttons in the range from 1 to MouseInfo.getNumberOfButtons() .
It is recommended to use the InputEvent.getMaskForButton(button) method to obtain the mask for any mouse button by its number.
- InputEvent.BUTTON1_MASK
- InputEvent.BUTTON2_MASK
- InputEvent.BUTTON3_MASK
mouseRelease
public void mouseRelease(int buttons)
- If the support for extended mouse buttons is disabled by Java then it is allowed to use only the following standard button masks: InputEvent.BUTTON1_DOWN_MASK , InputEvent.BUTTON2_DOWN_MASK , InputEvent.BUTTON3_DOWN_MASK .
- If the support for extended mouse buttons is enabled by Java then it is allowed to use the standard button masks and masks for existing extended mouse buttons, if the mouse has more then three buttons. In that way, it is allowed to use the button masks corresponding to the buttons in the range from 1 to MouseInfo.getNumberOfButtons() .
It is recommended to use the InputEvent.getMaskForButton(button) method to obtain the mask for any mouse button by its number.
- InputEvent.BUTTON1_MASK
- InputEvent.BUTTON2_MASK
- InputEvent.BUTTON3_MASK
mouseWheel
public void mouseWheel(int wheelAmt)
keyPress
public void keyPress(int keycode)
Presses a given key. The key should be released using the keyRelease method. Key codes that have more than one physical key associated with them (e.g. KeyEvent.VK_SHIFT could mean either the left or right shift key) will map to the left key.
keyRelease
public void keyRelease(int keycode)
Releases a given key. Key codes that have more than one physical key associated with them (e.g. KeyEvent.VK_SHIFT could mean either the left or right shift key) will map to the left key.
getPixelColor
createScreenCapture
public BufferedImage createScreenCapture(Rectangle screenRect)
Creates an image containing pixels read from the screen. This image does not include the mouse cursor.
isAutoWaitForIdle
public boolean isAutoWaitForIdle()
setAutoWaitForIdle
public void setAutoWaitForIdle(boolean isOn)
getAutoDelay
setAutoDelay
public void setAutoDelay(int ms)
delay
Sleeps for the specified time. To catch any InterruptedException s that occur, Thread.sleep() may be used instead.
waitForIdle
toString
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
What is robot class in java
This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations. Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform’s native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events. Note that some platforms require special privileges or extensions to access low-level input control. If the current platform configuration does not allow input control, an AWTException will be thrown when trying to construct Robot objects. For example, X-Window systems will throw the exception if the XTEST 2.2 standard extension is not supported (or not enabled) by the X server. Applications that use Robot for purposes other than self-testing should handle these error conditions gracefully.
Constructor Summary
Method Summary
Methods declared in class java.lang.Object
Constructor Detail
Robot
Robot
public Robot(GraphicsDevice screen) throws AWTException
- share the same coordinate system to form a combined virtual screen
- use different coordinate systems to act as independent screens
If screen devices are reconfigured such that the coordinate system is affected, the behavior of existing Robot objects is undefined.
Method Detail
mouseMove
public void mouseMove(int x, int y)
mousePress
public void mousePress(int buttons)
Presses one or more mouse buttons. The mouse buttons should be released using the mouseRelease(int) method.
- If support for extended mouse buttons is disabled by Java then it is allowed to use only the following standard button masks: InputEvent.BUTTON1_DOWN_MASK , InputEvent.BUTTON2_DOWN_MASK , InputEvent.BUTTON3_DOWN_MASK .
- If support for extended mouse buttons is enabled by Java then it is allowed to use the standard button masks and masks for existing extended mouse buttons, if the mouse has more then three buttons. In that way, it is allowed to use the button masks corresponding to the buttons in the range from 1 to MouseInfo.getNumberOfButtons() .
It is recommended to use the InputEvent.getMaskForButton(button) method to obtain the mask for any mouse button by its number.
mouseRelease
public void mouseRelease(int buttons)
- If the support for extended mouse buttons is disabled by Java then it is allowed to use only the following standard button masks: InputEvent.BUTTON1_DOWN_MASK , InputEvent.BUTTON2_DOWN_MASK , InputEvent.BUTTON3_DOWN_MASK .
- If the support for extended mouse buttons is enabled by Java then it is allowed to use the standard button masks and masks for existing extended mouse buttons, if the mouse has more then three buttons. In that way, it is allowed to use the button masks corresponding to the buttons in the range from 1 to MouseInfo.getNumberOfButtons() .
It is recommended to use the InputEvent.getMaskForButton(button) method to obtain the mask for any mouse button by its number.
mouseWheel
public void mouseWheel(int wheelAmt)
keyPress
public void keyPress(int keycode)
Presses a given key. The key should be released using the keyRelease method. Key codes that have more than one physical key associated with them (e.g. KeyEvent.VK_SHIFT could mean either the left or right shift key) will map to the left key.
keyRelease
public void keyRelease(int keycode)
Releases a given key. Key codes that have more than one physical key associated with them (e.g. KeyEvent.VK_SHIFT could mean either the left or right shift key) will map to the left key.
getPixelColor
createScreenCapture
public BufferedImage createScreenCapture(Rectangle screenRect)
Creates an image containing pixels read from the screen. This image does not include the mouse cursor.
createMultiResolutionScreenCapture
public MultiResolutionImage createMultiResolutionScreenCapture(Rectangle screenRect)
- Base Image with user specified size. This is scaled from the screen.
- Native device resolution image with device size pixels.
Image nativeResImage; MultiResolutionImage mrImage = robot.createMultiResolutionScreenCapture(frame.getBounds()); List resolutionVariants = mrImage.getResolutionVariants(); if (resolutionVariants.size() > 1) < nativeResImage = resolutionVariants.get(1); >else
isAutoWaitForIdle
public boolean isAutoWaitForIdle()
setAutoWaitForIdle
public void setAutoWaitForIdle(boolean isOn)
getAutoDelay
setAutoDelay
public void setAutoDelay(int ms)
delay
Sleeps for the specified time. To catch any InterruptedException s that occur, Thread.sleep() may be used instead.
waitForIdle
toString
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.