Implementation:Teamcapybara Capybara Selector Table
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Testing, Selector_System |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete selector definition for matching HTML
elements provided by with support for complex row/column content filtering and header-to-cell position tracking.Description
The selector is registered via . The block builds an expression that matches by attribute, layouts.Usage
Used automatically when calling find(:table, ...) or have_table(...) matchers. The expression filters allow asserting specific table content structure without manual XPath.
Code Reference
Source Location
- Repository: capybara
- File: lib/capybara/selector/definition/table.rb (109 lines)
Signature
Capybara.add_selector(:table, locator_type: [String, Symbol]) do
xpath do |locator, caption: nil, **|
# Builds XPath matching by id, caption text, or test_id
# @param locator [String, Symbol, nil] Table id or caption text
# @param caption [String, nil] Explicit caption text filter
# @return [XPath::Expression]
end
expression_filter(:with_cols, valid_values: [Array]) do |xpath, cols|
# @param cols [Array<Hash>, Array<Array>] Column content specifications
# @return [XPath::Expression] XPath with column existence conditions
end
expression_filter(:cols, valid_values: [Array]) do |xpath, cols|
# @param cols [Array<Array>] Exact column content (must be Array of Arrays)
# @return [XPath::Expression] XPath with strict column/row count conditions
end
expression_filter(:with_rows, valid_values: [Array]) do |xpath, rows|
# @param rows [Array<Hash>, Array<Array>] Row content specifications
# @return [XPath::Expression] XPath with row existence conditions
end
expression_filter(:rows, valid_values: [Array]) do |xpath, rows|
# @param rows [Array<Hash>, Array<Array>] Exact row content
# @return [XPath::Expression] XPath with strict row count conditions
end
end
Import
require 'capybara'
# The :table selector is auto-loaded from lib/capybara/selector/definition/table.rb
I/O Contract
Inputs
Capybara.add_selector(:table)
:table
Capybara.add_selector(:table, locator_type: [String, Symbol])
xpath
XPath.descendant(:table)
id
| Name | Type | Required | Description |
|---|---|---|---|
| locator | String/Symbol | No | Table id attribute or |
| caption | String | No | Explicit caption text to match |
| with_cols | Array | No | Array of Hashes (header => cell) or Array of Arrays for column content assertion |
| cols | Array<Array> | No | Strict column match; must be Array of Arrays; enforces exact row count |
| with_rows | Array | No | Array of Hashes (header => cell) or Array of Arrays for row content assertion |
| rows | Array | No | Strict row match; enforces exact row count |
Outputs
| Name | Type | Description |
|---|---|---|
| XPath expression | XPath::Expression | XPath selecting elements matching all specified criteria
Usage ExamplesFind Table by Captionfind(:table, 'Monthly Revenue')
# Matches <table> with <caption>Monthly Revenue</caption> or id="Monthly Revenue"
Assert Row Content with Header Mappingexpect(page).to have_table('Users', with_rows: [
{ 'Name' => 'Alice', 'Role' => 'Admin' },
{ 'Name' => 'Bob', 'Role' => 'Editor' }
])
Strict Column Matchexpect(page).to have_table('Scores', cols: [
['Alice', 'Bob', 'Carol'], # First column values top-to-bottom
['95', '87', '92'] # Second column values top-to-bottom
])
# Enforces exactly 3 rows and exactly 2 columns
Assert Partial Column Content with Headersexpect(page).to have_table('Inventory', with_cols: [
{ 'Product' => 'Widget', 'Stock' => '150' }
])
# Verifies that a column headed "Product" contains "Widget" in the same row
# where the column headed "Stock" contains "150"
Related PagesImplements Principle |
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment