Carl's Blog

Researcher. Mountaineer.

Archive for January, 2009

Ruby and Watir: Sending keyboard events to an element

without comments

Ok, so you need to send certain keyboard events to an element to properly test a webpage, but stuck how to do it?

Ive looked at some tutorials on the web, but there doesn’t seem a simple solution.
However, I have been experimenting, and this method seems to work fine.


<code># First off create your Watir::IE object
ie = Watir::IE.new

# Then navigate to the page you are testing<code>
ie.goto("mypage.myserver.com")

# Grab the descriptor of the element you wish to send a keyboard command to
# (for example, a text field)
element = ie.textField(:id, "myText")

# Now, I found that keyboard events only work if the Watir::IE window is on top and has
# focus, so for example if we wish to send a "Pageup" keyboard command to the element
# "myText" we would do so by
ie.bring_to_front
element.focus
element.container.focus
element.container.send_keys("{PGUP"})
</code></code>

 

This method does require Autoit(http://www.autoitscript.com/autoit3/) to be installed, and a full list of keyboard events can be found here: http://www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm

 

 

 

 

Written by carl

January 5th, 2009 at 10:13 am

Posted in Ruby,Tutorial

Tagged with , , ,