In my Rails app, I am using this jQuery to prevent a form from being submitted when the user presses 'enter' in an input field:
$('input').on('keydown', function(e) {
if(e.which == '13') {
return false;
}
})
I wrote a test in Capybara to make sure the form was not being submitted (more on that here), and had a hell of a time getting the test to work properly. Eventually I discovered that the jQuery function had to target a keydown
event, not keypress
. It works wonderfully now.
This makes me a bit uncomfortable, because I know that keypress
should work just as well, and I know that it actually does work from trying it manually in the browser. But if I want to have a working test for this behavior, I have to do it with keydown
.
This is the command that I'm sending to Webkit in my integration test:
page.driver.browser.execute_script "var e = jQuery.Event('keydown'); e.which = 13; $('#shift_employee').trigger( e );"
It goes without saying that when I change the jQuery function to handle keypress
instead of keydown
, I also change the Ruby line above to trigger a keypress
event instead of a keydown
event. But the test fails unless I use keydown
.
So what's the deal? Is this a Webkit issue? Should I not worry about it, as long as the test is working and the form is not being submitted?