Skip to content

Commit

Permalink
Support :table_row without locator argument (#2749)
Browse files Browse the repository at this point in the history
The `:table_row` selector supports both `Hash` and `Array` arguments.
Many other selectors support omitting the `locator` argument, so this
commit adds support for omitting the `locator` from `:table_row`.

This can be useful when grabbing all rows and making assertions about
sort order:

```ruby
table = find :table, "My Table"

header, first, second, third = table.all :table_row

header.assert_text "Header"
first.assert_text "First row"
second.assert_text "Second row"
third.assert_text "Third row"
```
  • Loading branch information
seanpdoyle authored Apr 13, 2024
1 parent 0ce6981 commit ae0203c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/capybara/selector/definition/table_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
]
xp.where(cell_xp)
end
else
elsif locator.is_a? Array
initial_td = XPath.descendant(:td)[XPath.string.n.is(locator.shift)]
tds = locator.reverse.map { |cell| XPath.following_sibling(:td)[XPath.string.n.is(cell)] }
.reduce { |xp, cell| cell.where(xp) }
xpath[initial_td[tds]]
else
xpath
end
end
end
7 changes: 7 additions & 0 deletions lib/capybara/spec/session/has_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@
expect(@session.find(:table, 'Horizontal Headers')).not_to have_selector(:table_row, %w[Walpole Thomas])
expect(@session.find(:table, 'Horizontal Headers')).not_to have_selector(:table_row, %w[Other])
end

it 'should find row by all rows without locator values' do
table = @session.find(:table, 'Horizontal Headers')

expect(table).to have_selector(:table_row)
expect(table).to have_selector(:table_row, count: 6)
end
end

Capybara::SpecHelper.spec '#has_no_table?' do
Expand Down

0 comments on commit ae0203c

Please sign in to comment.