Implementation:Teamcapybara Capybara Driver Node Interface
| Knowledge Sources | |
|---|---|
| Domains | Testing, Web_Automation |
| Last Updated | 2026-02-12 06:00 GMT |
Overview
The Capybara::Driver::Node class defines the abstract interface that all Capybara driver-specific node implementations must satisfy, with every method raising NotImplementedError by default.
Description
Capybara::Driver::Node serves as the base class and contract definition for all driver node implementations in Capybara. It stores references to the driver instance, the native element object (whose type varies by driver), and an optional initial_cache hash. The class declares the full set of methods that a node must support -- including text extraction (all_text, visible_text), attribute access ([], value, style), mutation (set, select_option, unselect_option), user interaction (click, right_click, double_click, send_keys, hover, drag_to, drop, scroll_by, scroll_to), state queries (visible?, obscured?, checked?, selected?, disabled?, readonly?, multiple?), identity (tag_name, path, rect), and advanced features (trigger, shadow_root). The only methods with default implementations are readonly? (checks the readonly attribute), multiple? (checks the multiple attribute), inspect (returns a human-readable string), and == (compares native elements). All other methods raise NotImplementedError or NotSupportedByDriverError, forcing concrete subclasses to provide their own implementations.
Usage
Driver authors subclass Capybara::Driver::Node and override the abstract methods to implement element interactions for their specific driver (e.g., Capybara::RackTest::Node, Capybara::Selenium::Node). End users do not interact with this class directly; it is used internally by Capybara's driver system.
Code Reference
Source Location
- Repository: Teamcapybara_Capybara
- File: lib/capybara/driver/node.rb
- Lines: 1-143
Signature
module Capybara
module Driver
class Node
attr_reader :driver, :native, :initial_cache
def initialize(driver, native, initial_cache = {})
def all_text # raises NotImplementedError
def visible_text # raises NotImplementedError
def [](name) # raises NotImplementedError
def value # raises NotImplementedError
def style(styles) # raises NotImplementedError
def set(value, **options) # raises NotImplementedError
def select_option # raises NotImplementedError
def unselect_option # raises NotImplementedError
def click(keys = [], **options) # raises NotImplementedError
def right_click(keys = [], **options) # raises NotImplementedError
def double_click(keys = [], **options)# raises NotImplementedError
def send_keys(*args) # raises NotImplementedError
def hover # raises NotImplementedError
def drag_to(element, **options) # raises NotImplementedError
def drop(*args) # raises NotImplementedError
def scroll_by(x, y) # raises NotImplementedError
def scroll_to(element, alignment, position = nil) # raises NotImplementedError
def tag_name # raises NotImplementedError
def visible? # raises NotImplementedError
def obscured? # raises NotImplementedError
def checked? # raises NotImplementedError
def selected? # raises NotImplementedError
def disabled? # raises NotImplementedError
def readonly? # => !!self[:readonly]
def multiple? # => !!self[:multiple]
def rect # raises NotSupportedByDriverError
def path # raises NotSupportedByDriverError
def trigger(event) # raises NotSupportedByDriverError
def shadow_root # raises NotSupportedByDriverError
def inspect
def ==(other)
end
end
end
Import
require 'capybara/driver/node'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| driver | Capybara::Driver::Base | Yes | The driver instance that owns this node. |
| native | Object | Yes | The underlying native element object (type varies by driver implementation). |
| initial_cache | Hash | No | An optional hash of pre-computed values to cache on initialization. Defaults to {}.
|
value (for set) |
String, Array | Yes | The value to set on the element. Arrays are only allowed if the node has the multiple attribute. |
options (for set) |
Hash | No | Driver-specific options controlling how the value is set. |
keys (for click) |
Array | No | Modifier keys to hold during the click. Defaults to [].
|
Outputs
| Name | Type | Description |
|---|---|---|
| NotImplementedError | Exception | Raised by all unimplemented abstract methods, signaling that the concrete subclass must provide an implementation. |
| NotSupportedByDriverError | Exception | Raised by methods that a particular driver may legitimately not support (rect, path, trigger, shadow_root). |
| readonly? | Boolean | Returns true if the element has a readonly attribute. |
| multiple? | Boolean | Returns true if the element has a multiple attribute. |
| inspect | String | A human-readable representation of the node including tag name and path. |
| == | Boolean | Returns true if both nodes wrap the same native element. |
Usage Examples
Basic Usage
# Implementing a custom driver node:
class MyDriver::Node < Capybara::Driver::Node
def all_text
native.text_content
end
def visible_text
native.inner_text
end
def [](name)
native.get_attribute(name.to_s)
end
def tag_name
native.tag_name.downcase
end
def visible?
native.displayed?
end
def click(keys = [], **options)
native.click
end
def set(value, **options)
native.clear
native.send_keys(value.to_s)
end
def find_xpath(locator)
native.find_elements(:xpath, locator).map { |el| self.class.new(driver, el) }
end
def find_css(locator)
native.find_elements(:css, locator).map { |el| self.class.new(driver, el) }
end
end