From 363e87c862d999b754eb14f111198a4d708f0a1b Mon Sep 17 00:00:00 2001 From: Peter Williams Date: Fri, 26 Aug 2011 11:36:51 -0600 Subject: [PATCH] Allow #within to take a node in addition to a selector --- lib/capybara/session.rb | 13 +++++++++++-- lib/capybara/spec/session/within_spec.rb | 11 +++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 1189181f..e7833931 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -173,11 +173,20 @@ module Capybara # fill_in('Street', :with => '12 Main Street') # end # - # @param (see Capybara::Node::Finders#all) + # @overload within(*find_args) + # @param (see Capybara::Node::Finders#all) + # + # @overload within(a_node) + # @param [Capybara::Node::Base] a_node The node in whose scope the block should be evaluated + # # @raise [Capybara::ElementNotFound] If the scope can't be found before time expires # def within(*args) - new_scope = find(*args) + new_scope = if args.size == 1 && Capybara::Node::Base === args.first + args.first + else + find(*args) + end begin scopes.push(new_scope) yield diff --git a/lib/capybara/spec/session/within_spec.rb b/lib/capybara/spec/session/within_spec.rb index 997034a6..606352a4 100644 --- a/lib/capybara/spec/session/within_spec.rb +++ b/lib/capybara/spec/session/within_spec.rb @@ -38,6 +38,17 @@ shared_examples_for "within" do end end + context "with Node rather than selector" do + it "should click links in the given scope" do + node_of_interest = @session.find(:css, "ul li[contains('With Simple HTML')]") + + @session.within(node_of_interest) do + @session.click_link('Go') + end + @session.body.should include('Bar') + end + end + context "with the default selector set to CSS" do before { Capybara.default_selector = :css } it "should use CSS" do