Read more

Using Capybara finder methods with arbitrary matching conditions

Henning Koch
May 11, 2023Software engineer at makandra GmbH

Capybara has a variety of finder methods Show archive.org snapshot like find_button to help you look up DOM elements. There are also matchers Show archive.org snapshot like have_field to make expectations during tests.

Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot

These methods also have a number of options to influence the lookup. E.g. the :disabled option lets you control whether Capybara will match disabled fields.

If you have a matching condition that cannot be expressed by the existing Capybara option you may pass a block to define additional conditions in Ruby.

Example

The following will expect a field labeled Username, but also requires that this field has a blank [class] attribute:

expect(page).to have_field('Username') { |field| field[:class].blank? }

Limitations

Using execute_script, evaluate_script or evaluate_async_script within a filter block will crash with a timeout:

*** Selenium::WebDriver::Error::ScriptTimeoutError Exception: script timeout

This is due to Capybara using a zero timeout Show archive.org snapshot while calling the filter block.

A workaround is to temporarily set a different timeout while running your script:

expect(page).to have_field('Username') do |field|
  field.session.using_wait_time(0.5 * Capybara.default_max_wait_time) do
    execute_script(...)
  end
end

Note

Don't reset the wait time to the full Capybara.default_max_wait_time or you will lose Capybara's retry mechanism Show archive.org snapshot .

Posted by Henning Koch to makandra dev (2023-05-11 14:46)