Over a million developers have joined DZone.

Write Your Own Custom Automation Using java.awt.Robot

· DevOps Zone

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

The following tip shows how to use java.awt.Robot to create your own handy custom made automation. Running this example will open up Firefox in your system and type in twitter.com, loading the page for you. Isn't that cool? Try it yourself.

import java.awt.AWTException;import java.awt.Robot;import java.awt.event.KeyEvent;import java.io.IOException;public class RobotSample {public static void main(String[] args) throws IOException {try {Robot robot = new Robot();Runtime runtime = Runtime.getRuntime();runtime.exec("D:\\Program Files\\Mozilla Firefox\\firefox.exe");robot.delay(1000);robot.keyPress(KeyEvent.VK_F6);robot.keyPress(KeyEvent.VK_DELETE);robot.keyPress(KeyEvent.VK_T);robot.keyPress(KeyEvent.VK_W);robot.keyPress(KeyEvent.VK_I);robot.keyPress(KeyEvent.VK_T);robot.delay(70);robot.keyPress(KeyEvent.VK_T);robot.keyPress(KeyEvent.VK_E);robot.keyPress(KeyEvent.VK_R);robot.keyPress(KeyEvent.VK_DECIMAL);robot.keyPress(KeyEvent.VK_C);robot.keyPress(KeyEvent.VK_O);robot.keyPress(KeyEvent.VK_M);robot.keyPress(KeyEvent.VK_ENTER);robot.delay(200);} catch (AWTException e) {e.printStackTrace();}}}

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

Topics:
java,automation,java.awt.robot

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}