blog: http://alvinalexander.com/java/java-robot-class-example-mouse-keystroke
http://ruby.about.com/od/Automation-and-FFI/fl/Automating-with-Robot.htm
key shortcuts: http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html
eg:
http://ruby.about.com/od/Automation-and-FFI/fl/Automating-with-Robot.htm
Use JRuby if you want to implement Robot class:
require 'rubygems'
require 'Selenium-webdriver'
require 'java'
java_import 'java.awt.Robot'
java_import 'java.awt.event.InputEvent'
java_import 'java.awt.event.KeyEvent'
class WatirTest
# browser = Watir::Browser.new :firefox
browser = Selenium::WebDriver.for :firefox
browser.get 'http://gmail.com'
sleep 3
browser.find_element(:id, 'Email').send_keys 'vicky.venu81'
# puts browser.execute_script("return document.body").inspect
robot = Robot.new
robot.key_press(KeyEvent::VK_ENTER)
puts robot.methods
# robot.mouseMove(963,10);
# browser.quit
end
Working eg: 2
require 'java'
java_import 'java.awt.Robot'
java_import 'java.awt.event.InputEvent'
java_import 'java.awt.event.KeyEvent'
class RobotTest
def testing
x = 'a'
puts x.ord
puts "ASCII value of 'a': #{'a'.ord}"
puts "ASCII value of 'z': #{'z'.ord}"
puts "ASCII value of 'A': #{'A'.ord}"
puts "ASCII value of 'Z': #{'Z'.ord}"
word = "abcdefg"
word.each_byte {|b| print b.chr }
puts '+++++++++++++++++'
sleep 5
word.each_byte do |chr|
puts chr
robot = Robot.new
code = chr-32
robot.key_press(code)
end
end
end
RobotTest.new.testing()
Output:
97
ASCII value of 'a': 97
ASCII value of 'z': 122
ASCII value of 'A': 65
ASCII value of 'Z': 90
abcdefg+++++++++++++++++
97
98
99
100
101
102
103
++++++++++++++++++++++++++++++++
if wanted to implement additional methods using Robot class, ref the below the example in java:
public
class
JavaRobotExample
{
Robot robot =
new
Robot();
public
static
void
main(String[] args)
throws
AWTException
{
new
JavaRobotExample();
}
public
JavaRobotExample()
throws
AWTException
{
robot.setAutoDelay(
40
);
robot.setAutoWaitForIdle(
true
);
robot.delay(
4000
);
robot.mouseMove(
40
,
130
);
robot.delay(
500
);
leftClick();
robot.delay(
500
);
leftClick();
robot.delay(
500
);
type(
"Hello, world"
);
robot.mouseMove(
40
,
160
);
robot.delay(
500
);
leftClick();
robot.delay(
500
);
leftClick();
robot.delay(
500
);
type(
"This is a test of the Java Robot class"
);
robot.delay(
50
);
type(KeyEvent.VK_DOWN);
robot.delay(
250
);
type(
"Four score and seven years ago, our fathers ..."
);
robot.delay(
1000
);
System.exit(
0
);
}
private
void
leftClick()
{
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(
200
);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(
200
);
}
private
void
type(
int
i)
{
robot.delay(
40
);
robot.keyPress(i);
robot.keyRelease(i);
}
private
void
type(String s)
{
byte
[] bytes = s.getBytes();
for
(
byte
b : bytes)
{
int
code = b;
// keycode only handles [A-Z] (which is ASCII decimal [65-90])
if
(code >
96
&& code <
123
) code = code -
32
;
robot.delay(
40
);
robot.keyPress(code);
robot.keyRelease(code);
}
}
}
I gathered lots of information from your blog and it helped me a lot. Keep posting more.
ReplyDeleteData Science Training in Chennai
Data Science Training Institute in Chennai
Data Science classes in Chennai
R Training in Chennai
R Programming Training in Chennai
Machine Learning Training in Chennai
Machine Learning course in Chennai
Data Science course in Anna Nagar