0

Repeating the title, my question is how to test for form submission using "Enter Key" using poltergeist through capybara. In other words, i expect when i fill in the username, password and then press "Enter" while on either input i'll submit the form. From what I can tell poltergeist achieves this through "send_keys". However my current test:

When(/^I log in with enter key$/) do
  visit_login
  fill_in "app-email", :with => admin_user.email
  fill_in "app-password", :with => admin_user.password
  login = find("#app-password")
  login.native.send_key :Enter
end

works with the following (original) code which uses ng-click. I expected the above test to fail on the code below as with the code below you can't hit enter and submit the form in the browser. Here is the ng-click code:

%form{name: "form"}
  %input#app-email{name: "email", "ng-model" => "email", type: 'email'}
  %input#app-password{name: "password", "ng-model" => "password", type: "password"}
  %a.btn.btn-primary.login-btn#app-login{"ng-click" => "login(email, password)"} LOG IN

and here is the code with ng-submit, this both works manually and with the test:

%form{name: "form", "ng-submit" => "login(email, password)"}
  %input#app-email{name: "email", "ng-model" => "email", type: 'email'}
  %input#app-password{name: "password", "ng-model" => "password", type: "password"}
  %input.btn.btn-primary.login-btn#app-login{type: "submit", value: "LOG IN"}

there is a rather lengthy discussion on getting this to work here: Is there a way to send key presses to Webkit using Capybara?. However, I couldn't modify any of those solutions to work for me.

Drew Verlee
  • 1,880
  • 5
  • 21
  • 30
  • Hmmm. The question you cite was about `send_keys` not working with capybara-webkit. It works for me with poltergeist: http://stackoverflow.com/a/23378110/634576 I notice that you spelled it `send_key`, not `send_keys`, however. – Dave Schweisguth Jul 14 '14 at 22:08
  • send_key and send_keys should aliases of each other according to the docs. Also I have two questions. 1. Why does that test cause pass without the seemingly necessary code. 2. What is the correct test code. I'm going to update my post tomorrow as I discovered that by splitting my steps up into pieces the test and code behaves as expected. Possible the problem is that I have a race condition. Unfortantly I have very little understanding of this subject, a problem i need to correct as it possible keeps coming up. – Drew Verlee Jul 15 '14 at 00:53
  • OK on the alias. Race conditions are common with Cucumber scenarios that run Javascript; the general fix is to use Capybara to find an element that doesn't exist until the Javascript has run (Capybara will wait), or, less often, to wait for an object to appear in the database or something like that. Might take an Angular expert to diagnose your specific case, though. – Dave Schweisguth Jul 15 '14 at 02:11
  • Good point, I should tag it with angularJS. – Drew Verlee Jul 15 '14 at 12:38

0 Answers0