Saturday, 6 December 2014

Sending capital text into a textbox using Robot class

The aim is to shift focus into a textbox and to send capital text using Robot class. Yes, there are other ways of doing this too apart from using Robot class, but I just wanted to give this a try and here's how it's done,

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class RobotDemoKeyPress {

public static void main(String[] args) throws AWTException {

WebDriver driver = new FirefoxDriver();

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

driver.get("http://www.google.com");

//To get the focus into the search box
driver.findElement(By.id("gbqfq")).sendKeys("");

Robot robot = new Robot();

//To send the text in Caps
robot.keyPress(KeyEvent.VK_CAPS_LOCK);
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_I);
robot.keyRelease(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_N);
robot.keyPress(KeyEvent.VK_CAPS_LOCK);
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);

}

}

Pay it forward..

DR

No comments:

Post a Comment