August 6th 2011

Check Active Element with Capybara

The other day, I wanted to test that a certain element had the focus with capybara. This is what I came up with:

assert(@s.evaluate_script('document.activeElement')[:id] == 'IdOfElement', 'The element did not have the focus')

Note: @s is my session. I am managing session myself as I am testing a non rack app.