diff --git a/lib/capybara/node/element.rb b/lib/capybara/node/element.rb index ed87350e..ec20a44f 100644 --- a/lib/capybara/node/element.rb +++ b/lib/capybara/node/element.rb @@ -22,10 +22,14 @@ module Capybara # class Element < Base - def initialize(session, base, parent, selector) + def initialize(session, base, parent, query) super(session, base) @parent = parent - @selector = selector + @query = query + end + + def allow_reload! + @allow_reload = true end ## @@ -186,8 +190,10 @@ module Capybara end def reload - reloaded = parent.reload.first(@selector.name, @selector.locator, @selector.options) - @base = reloaded.base if reloaded + if @allow_reload + reloaded = parent.reload.first(@query.name, @query.locator, @query.options) + @base = reloaded.base if reloaded + end self end diff --git a/lib/capybara/node/finders.rb b/lib/capybara/node/finders.rb index 289ef168..76f1e29d 100644 --- a/lib/capybara/node/finders.rb +++ b/lib/capybara/node/finders.rb @@ -24,7 +24,7 @@ module Capybara # @raise [Capybara::ElementNotFound] If the element can't be found before time expires # def find(*args) - synchronize { all(*args).find! } + synchronize { all(*args).find! }.tap(&:allow_reload!) end ## diff --git a/lib/capybara/node/simple.rb b/lib/capybara/node/simple.rb index f4c94912..96c3509e 100644 --- a/lib/capybara/node/simple.rb +++ b/lib/capybara/node/simple.rb @@ -122,6 +122,10 @@ module Capybara yield # simple nodes don't need to wait end + def allow_reload! + # no op + end + def all(*args) query = Capybara::Query.new(*args) elements = native.xpath(query.xpath).map do |node| diff --git a/lib/capybara/spec/public/test.js b/lib/capybara/spec/public/test.js index 07a126f5..4ef1e6cd 100644 --- a/lib/capybara/spec/public/test.js +++ b/lib/capybara/spec/public/test.js @@ -40,4 +40,9 @@ $(function() { $('#reload-me').replaceWith('
RELOADED
'); }, 250) }); + $('#reload-list').click(function() { + setTimeout(function() { + $('#the-list').html('
  • Foo
  • Bar
  • '); + }, 250) + }); }); diff --git a/lib/capybara/spec/session/find_spec.rb b/lib/capybara/spec/session/find_spec.rb index 7664829c..72cfa673 100644 --- a/lib/capybara/spec/session/find_spec.rb +++ b/lib/capybara/spec/session/find_spec.rb @@ -1,5 +1,5 @@ shared_examples_for "find" do - describe '#find', :focus => true do + describe '#find' do before do @session.visit('/with_html') end diff --git a/lib/capybara/spec/session/javascript.rb b/lib/capybara/spec/session/javascript.rb index 57395b03..93bbda43 100644 --- a/lib/capybara/spec/session/javascript.rb +++ b/lib/capybara/spec/session/javascript.rb @@ -73,6 +73,15 @@ shared_examples_for "session with javascript support" do sleep(0.3) node.find(:css, 'a').text.should == 'RELOADED' end + + it "should not reload nodes which haven't been found" do + @session.visit('/with_js') + node = @session.all(:css, '#the-list li')[1] + @session.click_link('Fetch new list!') + sleep(0.3) + running { node.text.should == 'Foo' }.should raise_error + running { node.text.should == 'Bar' }.should raise_error + end end end diff --git a/lib/capybara/spec/views/with_js.erb b/lib/capybara/spec/views/with_js.erb index 2e4f869a..21e0c113 100644 --- a/lib/capybara/spec/views/with_js.erb +++ b/lib/capybara/spec/views/with_js.erb @@ -6,10 +6,10 @@ - +

    FooBar

    - +

    This is text

    This is a draggable element.

    @@ -17,16 +17,16 @@

    It should be dropped here.

    - +

    Click me

    - +

    - +

    @@ -44,6 +44,14 @@
    waiting to be reloaded

    +

    + Fetch new list! +

    +

    +