Implementation:Teamcapybara Capybara RackTest Node
| Knowledge Sources | |
|---|---|
| Domains | Testing, Web_Automation |
| Last Updated | 2026-02-12 06:00 GMT |
Overview
The Capybara::RackTest::Node class implements DOM node interactions for Capybara's built-in Rack Test driver, providing methods to read, manipulate, and query HTML elements without a real browser.
Description
Capybara::RackTest::Node extends Capybara::Driver::Node to provide a concrete implementation of node operations using Nokogiri-parsed HTML. It handles text extraction (with whitespace normalization), form input manipulation (setting values on text fields, checkboxes, radio buttons, range inputs, textareas, and multi-valued inputs), click simulation (following links, submitting forms, toggling checkboxes/radios, clicking labels, and toggling <details> elements), and state queries (visible?, disabled?, checked?, selected?, readonly?). The class also implements stale element detection by comparing the node's document against the driver's current DOM, and delegates visibility and state checks to an internal Capybara::Node::Simple wrapper via the string_node helper.
Usage
This class is used internally by the Rack Test driver whenever Capybara needs to interact with a found element. It is not typically instantiated directly by end users but is created automatically when calling methods like find, click_link, fill_in, or choose during tests that run with the default :rack_test driver.
Code Reference
Source Location
- Repository: Teamcapybara_Capybara
- File: lib/capybara/rack_test/node.rb
- Lines: 1-324
Signature
class Capybara::RackTest::Node < Capybara::Driver::Node
include Capybara::Node::WhitespaceNormalizer
BLOCK_ELEMENTS = %w[p h1 h2 h3 h4 h5 h6 ol ul pre address blockquote dl div fieldset form hr noscript table].freeze
def all_text
def visible_text
def [](name)
def value
def set(value, **options)
def select_option
def unselect_option
def click(keys = [], **options)
def tag_name
def visible?
def checked?
def selected?
def disabled?
def readonly?
def path
def find_xpath(locator, **_hints)
def find_css(locator, **_hints)
end
Import
require 'capybara/rack_test/node'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| driver | Capybara::RackTest::Driver | Yes | The Rack Test driver instance that owns this node. |
| native | Nokogiri::XML::Node | Yes | The underlying Nokogiri node representing the HTML element. |
value (for set) |
String, Array | Yes | The value to set on the element. Arrays are only allowed for inputs with the multiple attribute.
|
options (for set) |
Hash | No | Driver-specific options (ignored by RackTest with a warning). |
keys (for click) |
Array | No | Modifier keys (must be empty; RackTest does not support click modifiers). |
locator (for find_xpath, find_css) |
String | Yes | An XPath or CSS selector expression to search within this node. |
Outputs
| Name | Type | Description |
|---|---|---|
| all_text | String | The full text content of the node with normalized spacing. |
| visible_text | String | Only the visible text, excluding content hidden by CSS. |
| find_xpath / find_css | Array<Capybara::RackTest::Node> | An array of child nodes matching the given selector. |
| visible? / checked? / selected? / disabled? | Boolean | State query results for the element. |
| click | nil | Performs the appropriate click action (follow link, submit form, toggle checkbox, etc.). |
Usage Examples
Basic Usage
# Typically used internally by Capybara. Direct usage looks like:
node = Capybara::RackTest::Node.new(driver, nokogiri_element)
# Read text content
node.all_text # => "Hello World"
node.visible_text # => "Hello World"
# Set a value on a form input
node.set("new value")
# Click the element (follows links, submits forms, toggles checkboxes)
node.click
# Query element state
node.visible? # => true
node.disabled? # => false
node.checked? # => false
# Find child elements
children = node.find_css('input[type="text"]')
xpath_results = node.find_xpath('.//div[@class="content"]')