From 230709be11bfd9e9607b494fad11b8c2e2bfbea5 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Sun, 22 Dec 2019 08:43:39 -0800 Subject: [PATCH] Fix issue #2287 --- lib/capybara/selector/definition/table.rb | 7 ++- lib/capybara/spec/session/has_table_spec.rb | 56 +++++++++++++++++++-- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/lib/capybara/selector/definition/table.rb b/lib/capybara/selector/definition/table.rb index 79f48ba8..182b66d1 100644 --- a/lib/capybara/selector/definition/table.rb +++ b/lib/capybara/selector/definition/table.rb @@ -19,7 +19,10 @@ Capybara.add_selector(:table, locator_type: [String, Symbol]) do header = XPath.descendant(:th)[XPath.string.n.is(header)] td = XPath.descendant(:tr)[header].descendant(:td) cell_condition = XPath.string.n.is(cell_str) - cell_condition &= prev_col_position?(XPath.ancestor(:table)[1].join(xp)) if xp + if xp + prev_cell = XPath.ancestor(:table)[1].join(xp) + cell_condition &= (prev_cell & prev_col_position?(prev_cell)) + end td[cell_condition] end else @@ -28,7 +31,7 @@ Capybara.add_selector(:table, locator_type: [String, Symbol]) do if prev_cell prev_cell = XPath.ancestor(:tr)[1].preceding_sibling(:tr).join(prev_cell) - cell_condition &= prev_col_position?(prev_cell) + cell_condition &= (prev_cell & prev_col_position?(prev_cell)) end XPath.descendant(:td)[cell_condition] diff --git a/lib/capybara/spec/session/has_table_spec.rb b/lib/capybara/spec/session/has_table_spec.rb index 1ddeb145..21b6c148 100644 --- a/lib/capybara/spec/session/has_table_spec.rb +++ b/lib/capybara/spec/session/has_table_spec.rb @@ -143,10 +143,56 @@ Capybara::SpecHelper.spec '#has_no_table?' do ]) end - it 'should consider columns' do - expect(@session).to have_no_table('Vertical Headers', with_cols: - [ - { 'First Name' => 'Joe' } - ]) + context 'using :with_cols' do + it 'should consider a single column' do + expect(@session).to have_no_table('Vertical Headers', with_cols: + [ + { 'First Name' => 'Joe' } + ]) + end + + it 'should be true even if the last column does exist' do + expect(@session).to have_no_table('Vertical Headers', with_cols: + [ + { + 'First Name' => 'What?', + 'What?' => 'Walpole', + 'City' => 'Oceanside' # This line makes the example fail + } + ]) + end + + it 'should be true if none of the columns exist' do + expect(@session).to have_no_table('Vertical Headers', with_cols: + [ + { + 'First Name' => 'What?', + 'What?' => 'Walpole', + 'City' => 'What?' + } + ]) + end + + it 'should be true if the first column does match' do + expect(@session).to have_no_table('Vertical Headers', with_cols: + [ + { + 'First Name' => 'Thomas', + 'Last Name' => 'What', + 'City' => 'What' + } + ]) + end + + it 'should be true if none of the columns match' do + expect(@session).to have_no_table('Vertical Headers', with_cols: + [ + { + 'First Name' => 'What', + 'Last Name' => 'What', + 'City' => 'What' + } + ]) + end end end