Skip to content

Capybara UI Gotchas

Adam edited this page Jan 21, 2016 · 1 revision

A brief tour of some of the more common mistakes one might make in Capybara-UI.


Don't store Capybara-UI elements in a variable and then expect that variable to work later on. Especially on projects where there's a lot of ajax flying around.

# in your role
def update_and_complete(current_name, new_name)
  item = widget(:todo_item, current_name)
  click :clear_completed_items

# in your test
old_item = role.update_and_complete('Buy Milk', 'Buy Whole Milk')

expect( eq('Buy Milk')
# => StaleElementReferenceError

Instead, always call the widget. This also encourages you to make more, and skinnier, widgets.

# in your role
def update_and_complete(current_name, new_name)
  submit :edit_todo_item, current_name, name: new_name
  click :complete_item, new_name
  click :clear_completed_items

# in your test
role.update_and_complete('Buy Milk', 'Buy Whole Milk')

expect(role).to see :todo_item, 'Buy Whole Milk'
expect(role).not_to see :todo_item, 'Buy Milk'

'eventually' does not work with all errors

eventually(5) do
  puts "called"
  raise Exception

# => called
# => Exception

eventually only catches errors that subclass StandardError. Any error that subclasses Exception will not be caught, unless you configure Capybara-UI to specifically catch that error.

You can’t perform that action at this time.