Skip to content

Commit

Permalink
table_row and with_rows ensure that the header matches locator for fi…
Browse files Browse the repository at this point in the history
…rst column Fixes #2685 (#2688)
  • Loading branch information
jparr authored Jul 23, 2023
1 parent 5521da1 commit ad549b9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/capybara/selector/definition/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def row_match_cells_to_headers(row)
row.map do |header, cell|
header_xp = XPath.ancestor(:table)[1].descendant(:tr)[1].descendant(:th)[XPath.string.n.is(header)]
XPath.descendant(:td)[
XPath.string.n.is(cell) & XPath.position.equals(header_xp.preceding_sibling.count.plus(1))
XPath.string.n.is(cell) & header_xp.boolean & XPath.position.equals(header_xp.preceding_sibling.count.plus(1))
]
end.reduce(:&)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/selector/definition/table_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
locator.reduce(xpath) do |xp, (header, cell)|
header_xp = XPath.ancestor(:table)[1].descendant(:tr)[1].descendant(:th)[XPath.string.n.is(header)]
cell_xp = XPath.descendant(:td)[
XPath.string.n.is(cell) & XPath.position.equals(header_xp.preceding_sibling.count.plus(1))
XPath.string.n.is(cell) & header_xp.boolean & XPath.position.equals(header_xp.preceding_sibling.count.plus(1))
]
xp.where(cell_xp)
end
Expand Down
13 changes: 12 additions & 1 deletion lib/capybara/spec/session/has_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
])
end

it 'should accept rows with partial column header hashses' do
it 'should accept rows with partial column header hashes' do
expect(@session).to have_table('Horizontal Headers', with_rows:
[
{ 'First Name' => 'Thomas' },
{ 'Last Name' => 'Sawayn', 'City' => 'West Trinidad' }
])

expect(@session).not_to have_table('Horizontal Headers', with_rows:
[
{ 'Unmatched Header' => 'Thomas' }
])
end

it 'should accept rows with array of cell values' do
Expand Down Expand Up @@ -103,6 +108,11 @@
{ 'First Name' => 'Danilo', 'Last Name' => 'Walpole', 'City' => 'Johnsonville' },
{ 'Last Name' => 'Sawayn', 'City' => 'West Trinidad' }
])

expect(@session).not_to have_table('Vertical Headers', with_cols:
[
{ 'Unmatched Header' => 'Thomas' }
])
end

it 'should be false if the table is not on the page' do
Expand All @@ -113,6 +123,7 @@
expect(@session.find(:table, 'Horizontal Headers')).to have_selector(:table_row, 'First Name' => 'Thomas', 'Last Name' => 'Walpole')
expect(@session.find(:table, 'Horizontal Headers')).to have_selector(:table_row, 'Last Name' => 'Walpole')
expect(@session.find(:table, 'Horizontal Headers')).not_to have_selector(:table_row, 'First Name' => 'Walpole')
expect(@session.find(:table, 'Horizontal Headers')).not_to have_selector(:table_row, 'Unmatched Header' => 'Thomas')
end

it 'should find row by cell values' do
Expand Down

0 comments on commit ad549b9

Please sign in to comment.