The application I'm writing depends heavily on key events. When you press enter in a specific text field, some other field opens, when you press tab, the text fields moves, etc. Of course I need to cover this in my cucumber integration tests. The Celerity API is a bit thin for this purpose: You can fire a, let's say, keydown event - but it doesn't pass through the specific key code.
Actually it's easy when you know how. You can execute any javascript directly on the Culerity RemoteBrowserProxy object. In order to fire a specific key event, you have to create a KeyboardEvent, initialize it with whatever you want, and dispatch it on the element.
Read up the details, especially the meaning of initKeyEvent's parameters in this Javascript API.
I ended up with this cucumber step:
When /^I hit "([^\"]*)" in a text_field with id "([^\"]*)"$/ do |key, id|
key_code = case key
when 'enter'
13
when 'up'
38
when 'down'
40
else
0
end
$browser.execute_script('event = document.createEvent("KeyboardEvent");')
$browser.execute_script('event.initKeyEvent("keydown", true, false, document.window, false, false, false, false, ' + key_code.to_s + ', 0)')
$browser.execute_script("document.getElementById('#{id}').dispatchEvent(event)")
When 'I wait for the AJAX call to finish'
endNote that this is meant to work for Firefox only. For IE you can do a similar thing with createEventObject, see here how.
Trackbacks
Use the following link to trackback from your own site:
http://lenaherrmann.net/trackbacks?article_id=7