send_keys, fire_event and keyboard events

560 views
Skip to first unread message

Walter Rumsby

unread,
May 6, 2008, 10:44:12 PM5/6/08
to Watir General
Hi There,

I've had a look at both IE.send_keys and Element.fire_event because I
want to test code that relies on particular keyboard events and
neither seems to be able to do what I want.

Specifically I want to write a test for a page that has a textarea
that listens for a particular keyboard combination (SHIFT + SPACE) and
pops up a menu in response to that, so what I want to do is send a
keypress event *with that particular key combination* to the textarea
and then verify the contents of the menu.

My Watir test starts off like so:


require 'watir'

include Watir

browser = IE.new

browser.maximize
browser.goto(...)

textarea = browser.text_field(:id, 'contents')
textarea.focus
textarea.set('The quick brown d')


If I try


browser.send_keys("+{ASC 032}")


it appears that SHIFT + SPACE is sent to the address bar of the
browser. Not quite what I wanted.

So if I try


textarea.fire_event('onKeyPress')


well that "works", but it's not sending the keyboard combination I
want.

The problem seems to be that keyboard events are a little more complex
than other kinds of events like click, mouseover, etc. There's
additional information that's important (i.e. the key data) and I'm
not sure that Watir currently provides a way to send that information
to the browser.

Any advice on how to do this?

Bill Agee

unread,
May 7, 2008, 4:12:58 AM5/7/08
to watir-...@googlegroups.com
If send_keys isn't working for this case, you might want to try using AutoIt directly to try different things - for more info see http://www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm
 
Or for a quick one-off hack, you could try this out (note that you might not need to require win32ole since I think Watir already does that for you):
 
require 'win32ole'
wsh = WIN32OLE.new('Wscript.Shell')
wsh.SendKeys("+ ") # SHIFT+SPACE
 
...although if send_keys is not working, I don't know if this will either.
 
Here's some more info on the WSH SendKeys method, in case it actually works here and you want to know more:
 

Paul Rogers

unread,
May 7, 2008, 1:39:23 PM5/7/08
to watir-...@googlegroups.com
the events code I posted to the wike a few weeks back will go along
way to doing what you want. Either search the wiki or the mailing list
to find it

Paul

Željko Filipin

unread,
May 8, 2008, 6:38:50 AM5/8/08
to watir-...@googlegroups.com
On Wed, May 7, 2008 at 7:39 PM, Paul Rogers <paul....@shaw.ca> wrote:
> the events code I posted to the wike a few weeks back will go along
> way to doing what you want.

http://wiki.openqa.org/display/WTR/Possibilities+For+New+Events+in+Watir

Željko
--
ZeljkoFilipin.com

Walter Rumsby

unread,
May 11, 2008, 6:34:42 PM5/11/08
to Watir General
Yes, this does the trick.

Thanks a lot.

On May 8, 10:38 pm, "Željko Filipin" <zeljko.fili...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages