Search code examples
ruby-on-railsdeviseintegration-testingminitestcapybara-webkit

How can I simulate password autofilling in my integration test?


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).


Solution

  • 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.