From ae0203cdb2b73c30c19c2195d03e10a37d23e023 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Sat, 13 Apr 2024 14:55:10 -0400 Subject: [PATCH] Support `:table_row` without locator argument (#2749) 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" ``` --- lib/capybara/selector/definition/table_row.rb | 4 +++- lib/capybara/spec/session/has_table_spec.rb | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/capybara/selector/definition/table_row.rb b/lib/capybara/selector/definition/table_row.rb index 6e9a9a73a..7517595fa 100644 --- a/lib/capybara/selector/definition/table_row.rb +++ b/lib/capybara/selector/definition/table_row.rb @@ -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 diff --git a/lib/capybara/spec/session/has_table_spec.rb b/lib/capybara/spec/session/has_table_spec.rb index 42ac1775e..b947104eb 100644 --- a/lib/capybara/spec/session/has_table_spec.rb +++ b/lib/capybara/spec/session/has_table_spec.rb @@ -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