add #within to Section
MODIFY
features/section.feature
features/section.feature
@@ -13,6 +13,11 @@ Feature: Page Sections
Then I can access elements within the section using a block
But I cannot access elements that are not in the section using a block
+ Scenario: access elements in the section by passing a block to within
+ When I navigate to the home page
+ Then I can execute elements in the context of a section by passing a block to within
+ But I cannot access elements that are not in the section using within
+
Scenario: section in a section
When I navigate to the nested section page
Then I can see a section in a section

MODIFY
features/step_definitions/section_steps.rb
features/step_definitions/section_steps.rb
@@ -27,12 +27,34 @@ Then('I can access elements within the section using a block') do
expect(block_inner_executions).to eq 1
end
+Then('I can execute elements in the context of a section by passing a block to within') do
+ block_inner_executions = 0
+
+ expect(@test_site.home).to have_people
+
+ @test_site.home.people.within do |section|
+ block_inner_executions += 1
+
+ expect(section.headline.text).to eq('People')
+
+ expect(section).to have_individuals(count: 4)
+ end
+
+ expect(block_inner_executions).to eq 1
+end
+
Then('I cannot access elements that are not in the section using a block') do
expect do
@test_site.home.people { |section| expect(section).to have_not_here }
end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
+Then('I cannot access elements that are not in the section using within') do
+ expect do
+ @test_site.home.people.within { |section| expect(section).to have_not_here }
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
+end
+
Then('access to elements is constrained to those within the section') do
expect(@test_site.home).to have_css('.welcome')

MODIFY
lib/site_prism/section.rb
lib/site_prism/section.rb
@@ -31,6 +31,10 @@ module SitePrism
Capybara.within(@root_element) { yield(self) } if block_given?
end
+ def within(&block)
+ Capybara.within(@root_element) { yield(self) }
+ end
+
# Capybara::DSL module "delegates" Capybara methods to the "page" method
# as such we need to overload this method so that the correct scoping
# occurs and calls within a section (For example section.find(element))

MODIFY
spec/site_prism/section_spec.rb
spec/site_prism/section_spec.rb
@@ -77,6 +77,7 @@ describe SitePrism::Section do
it { is_expected.to respond_to(:wait_until_section_visible) }
it { is_expected.to respond_to(:wait_until_section_invisible) }
it { is_expected.to respond_to(:all_there?) }
+ it { is_expected.to respond_to(:within) }
end
context 'when second argument is not a Class but a block is given' do
@@ -96,6 +97,7 @@ describe SitePrism::Section do
it { is_expected.to respond_to(:wait_until_anonymous_section_visible) }
it { is_expected.to respond_to(:wait_until_anonymous_section_invisible) }
it { is_expected.to respond_to(:all_there?) }
+ it { is_expected.to respond_to(:within) }
end
context 'when second argument is a Class and a block is given' do
@@ -115,6 +117,7 @@ describe SitePrism::Section do
it { is_expected.to respond_to(:wait_until_anonymous_section_visible) }
it { is_expected.to respond_to(:wait_until_anonymous_section_invisible) }
it { is_expected.to respond_to(:all_there?) }
+ it { is_expected.to respond_to(:within) }
end
context 'when second argument is not a Class and no block is given' do
@@ -278,6 +281,14 @@ set_default_search_arguments within section class"
end
end
+ describe '#within' do
+ it "passes the block to Capybara#within" do
+ expect(Capybara).to receive(:within).with(locator)
+
+ section_without_block.within { :noop }
+ end
+ end
+
describe '#visible?' do
it 'delegates through root_element' do
expect(locator).to receive(:visible?)

add #within to Section