1

On my password change page I have the new password field named password, and the current password field named current_password so that password managers will pick up on the change and prompt to save your new password. (Also it's default devise behavior)

However if a user doesn't want to change their password, I have them leave the new password field blank (again devise default behavior), but password managers will often autofill this field with the current password.

That's not a problem, I just use js to clear the field if the value was entered automatically. But I'd like to test this, and can't figure out how to get capybara (webkit driver) to fill in the field in a way that doesn't get interpreted as manual by js. (ie doesn't focus the field)

Currently I wipe the value (once) if the field wasn't focused before the value was entered. That way there's no race conditions and the field becomes intelligible to use if the user wants to use it (focuses the field).

Camden Narzt
  • 2,271
  • 1
  • 23
  • 42
  • Have you tried adding `autocomplete="false"` to your new password input? – dhouty Jul 30 '15 at 18:14
  • Yes, browsers ignore that on password fields these days. – Camden Narzt Jul 30 '15 at 19:15
  • I thought that they ignore `autocomplete="off"` but `autocomplete="false"` works, I guess that has must have changed recently. If it comes down to it, I have come across a few hacky alternatives that might do the trick. The first is making the input readonly and then using javascript to remove the readonly attribute on focus (see [here](http://stackoverflow.com/a/24247840/2720041)). You could also try adding decoy inputs as seen [here](http://stackoverflow.com/a/15917221/2720041). Those methods might not work either, but you could give them a try. – dhouty Jul 30 '15 at 19:34
  • Did you read the question? I have a working solution for clearing the password, I just want to cover the functionality in my tests. – Camden Narzt Jul 30 '15 at 21:45

1 Answers1

0

Capybara is designed to emulate user interaction. Since a user would focus the field, Capybara does also.
One way around that might be to use javascript from within capybara to fill the field. Perhaps try something like:

page.execute_script( "$('input[name=password]').val('user-password')" )

execute_script injects JS into the page Capybara is testing. Note that when using this method you do have to handle waiting on your own.

tgf
  • 2,977
  • 2
  • 18
  • 29
  • I'm not hung up on capybara, is there another tool that would be better suited to testing this? – Camden Narzt Jul 31 '15 at 21:28
  • Did you try using `execute_script`? If it works for you that would be quicker than setting up another tool. You could also just write unit tests on the javascript function. There are a number of tools for that including Jasmine, mocha and qunit. I do see a potential problem here though, your feature is bound to the implementation of specific password managers which could change with no notice to you. – tgf Aug 01 '15 at 00:06
  • It works, I was just making sure I wasn't missing an opportunity to learn about a potentially better approach to testing such things. – Camden Narzt Aug 01 '15 at 00:37
  • Awesome. I think this is the route I would go unless I was planning to do enough JS to warrant adding a unit-test suite just for it. If you are, then definitely checkout Jasmine (popular in the Rails world) and Mocha (popular with many). There are tons of others too. – tgf Aug 01 '15 at 00:56