Search code examples
ruby-on-railsrspeccapybarakeyboard-events

Simulate pressing Tab key in Rspec Capybara?


I'm trying to write tests for managing focus in a dialog modal. How to simulate pressing the keyboard Tab key in Rspec Capybara?


Solution

  • Use the send_keys method, eg:

    body = find('body')
    body.send_keys(:tab)
    body.send_keys([:shift, :tab])
    

    Out of all the capybara docs pages for send_keys, the Capybara::Node::Element class has the best write up.