Read more

Capybara - The missing API

Henning Koch
July 28, 2011Software engineer at makandra GmbH

Note: The information in this card might be outdated.


Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

The Capybara API Show archive.org snapshot is somewhat hard for parse for a list of methods you can call on a Capybara node. Below you can find such a list. It's all copied from the Capybara docs, so all credit goes to the Capybara committers.

When you talk to Capybara from a Cucumber step definition, you always have page as the document root node, or whatever you scoped to by saying within(selector) { ... }. You can select child notes by calling page.find(selector) or page.all(selector). You can call the same list of methods on child nodes, select further child nodes, etc.

Inspect a Capybara node

(String) [](name)
Retrieve the given attribute.

(Boolean) checked?
Whether or not the element is checked.

(String) path
An XPath expression describing where on the page the element can be found.

(Boolean) selected?
Whether or not the element is selected.

(String) tag_name
The tag name of the element.

(String) text
The text of the element.

(String) value
The value of the form element.

(Boolean) visible?
Whether or not the element is visible.

Select child nodes

(Capybara::Element) all(*args)
Find all elements on the page matching the given selector and options.

(Capybara::Element) find(*args)
Find an Element based on the given arguments.

(Capybara::Element) find_button(locator)
Find a button on the page.

(Capybara::Element) find_by_id(id)
Find a element on the page, given its id.

(Capybara::Element) find_field(locator) (also: #field_labeled)
Find a form field on the page.

(Capybara::Element) find_link(locator)
Find a link on the page.

(Object) first(*args)
Find the first element on the page matching the given selector and options, or nil if no element matches.

Manipulate forms in child elements

(Object) attach_file(locator, path)
Find a file field on the page and attach a file given its path.

(Object) check(locator)
Find a check box and mark it as checked.

(Object) choose(locator)
Find a radio button and mark it as checked.

(Object) click_button(locator)
Finds a button by id, text or value and clicks it.

(Object) click_link(locator)
Finds a link by id or text and clicks it.

(Object) click_link_or_button(locator) (also: #click_on)
Finds a button or link by id, text or value and clicks it.

(Object) fill_in(locator, options = {})
Locate a text field or text area and fill it in with the given text The field can be found via its name, id or label text.

(Object) select(value, options = {})
Find a select box on the page and select a particular option from it.

(Object) uncheck(locator)
Find a check box and mark uncheck it.

(Object) unselect(value, options = {})
Find a select box on the page and unselect a particular option from it.

Check for the presence of child nodes

(Boolean) has_button?(locator)
Checks if the page or current node has a button with the given text, value or id.`

(Boolean) has_checked_field?(locator)
Checks if the page or current node has a radio button or checkbox with the given label, value or id, that is currently checked.`

(Boolean) has_content?(content)
Checks if the page or current node has the given text content, ignoring any HTML tags and normalizing whitespace.`

(Boolean) has_css?(path, options = {})
Checks if a given CSS selector is on the page or current node.`

(Boolean) has_field?(locator, options = {})
Checks if the page or current node has a form field with the given label, name or id.`

(Boolean) has_link?(locator, options = {})
Checks if the page or current node has a link with the given text or id.`

(Boolean) has_no_button?(locator)
Checks if the page or current node has no button with the given text, value or id.`

(Boolean) has_no_checked_field?(locator)
Checks if the page or current node has no radio button or checkbox with the given label, value or id, that is currently checked.`

(Boolean) has_no_content?(content)
Checks if the page or current node does not have the given text content, ignoring any HTML tags and normalizing whitespace.

(Boolean) has_no_css?(path, options = {})
Checks if a given CSS selector is not on the page or current node.

(Boolean) has_no_field?(locator, options = {})
Checks if the page or current node has no form field with the given label, name or id.

(Boolean) has_no_link?(locator, options = {})
Checks if the page or current node has no link with the given text or id.

(Boolean) has_no_select?(locator, options = {})
Checks if the page or current node has no select field with the given label, name or id.

(Boolean) has_no_selector?(*args)
Checks if a given selector is not on the page or current node.

(Boolean) has_no_table?(locator, options = {})
Checks if the page or current node has no table with the given id or caption.

(Boolean) has_no_unchecked_field?(locator)
Checks if the page or current node has no radio button or checkbox with the given label, value or id, that is currently unchecked.

(Boolean) has_no_xpath?(path, options = {})
Checks if a given XPath expression is not on the page or current node.

(Boolean) has_select?(locator, options = {})
Checks if the page or current node has a select field with the given label, name or id.

(Boolean) has_selector?(*args)
Checks if a given selector is on the page or current node.

(Boolean) has_table?(locator, options = {})
Checks if the page or current node has a table with the given id or caption.

(Boolean) has_unchecked_field?(locator)
Checks if the page or current node has a radio button or checkbox with the given label, value or id, that is currently unchecked.

(Boolean) has_xpath?(path, options = {})
Checks if a given XPath expression is on the page or current node.

Henning Koch
July 28, 2011Software engineer at makandra GmbH
Posted by Henning Koch to makandra dev (2011-07-28 19:03)