Skip to content

Commit

Permalink
Fix base_selector propagation through the page hierarchy
Browse files Browse the repository at this point in the history
What?
=====

This fixes a bug where base_selector values re-propagate across nested
selectors.

Resolves #51
  • Loading branch information
joshuaclayton committed Nov 9, 2023
1 parent 62e786f commit 0f44b7b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/page_ez/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def self.constructor_from_block(superclass = nil, &block)
Class.new(superclass || self).tap do |klass|
visitor.begin_block_evaluation
klass.macro_registrar = {}
klass.container_base_selector = nil
klass.class_eval(&block)
visitor.end_block_evaluation
self.nested_macro = false
Expand Down
60 changes: 60 additions & 0 deletions spec/features/contains_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,66 @@ def awesome?
expect(test_page).to be_awesome
end

it "correctly stops base selector propagation within page hierarchy" do
page = build_page(<<-HTML)
<table data-role=test-me>
<tbody>
<tr>
<td>1</td>
<td class="one"><span>2</span></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
</tbody>
</table>
HTML

table = Class.new(PageEz::Page) do
base_selector "table[data-role=test-me]"

has_many_ordered :rows, "tbody tr" do
has_many :cells, "td"
end

has_many_ordered :other_rows, "tbody tr" do
base_selector ".one"

has_many :spans, "span"
end

has_many_ordered :broken_rows, "tbody tr" do
base_selector "bogus"

has_many :spans, "span"
end
end.new(page)

page.visit "/"

expect(table.rows.count).to eq(1)
expect(table.row_at(0).cells.count).to eq(2)

expect(table.other_rows.count).to eq(1)
expect(table.other_row_at(0).spans.count).to eq(1)

expect(table.broken_rows.count).to eq(1)
expect do
table.broken_row_at(0).spans.count
end.to raise_error(Capybara::ElementNotFound)
end

it "allows multiple page objects to use contains with the same page object" do
expect do
heading = Class.new(PageEz::Page) do
Expand Down

0 comments on commit 0f44b7b

Please sign in to comment.