Do not reload nodes found with #all and #first. closes #713

This commit is contained in:
Jonas Nicklas 2012-06-11 17:29:58 +02:00
parent f57a36d2fe
commit 453cec449d
7 changed files with 43 additions and 11 deletions

View File

@ -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

View File

@ -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
##

View File

@ -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|

View File

@ -40,4 +40,9 @@ $(function() {
$('#reload-me').replaceWith('<div id="reload-me"><em><a>RELOADED</a></em></div>');
}, 250)
});
$('#reload-list').click(function() {
setTimeout(function() {
$('#the-list').html('<li>Foo</li><li>Bar</li>');
}, 250)
});
});

View File

@ -1,5 +1,5 @@
shared_examples_for "find" do
describe '#find', :focus => true do
describe '#find' do
before do
@session.visit('/with_html')
end

View File

@ -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

View File

@ -6,10 +6,10 @@
<script src="/jquery-ui.js" type="text/javascript" charset="utf-8"></script>
<script src="/test.js" type="text/javascript" charset="utf-8"></script>
</head>
<body id="with_js">
<h1>FooBar</h1>
<p id="change">This is text</p>
<div id="drag">
<p>This is a draggable element.</p>
@ -17,16 +17,16 @@
<div id="drop">
<p>It should be dropped here.</p>
</div>
<p><a href="#" id="clickable">Click me</a></p>
<p>
<select id="waiter">
<option>Foo</option>
<option>My Waiting Option</option>
</select>
</p>
<p>
<input type="text" name="with_focus_event" value="" id="with_focus_event"/>
</p>
@ -44,6 +44,14 @@
<div id="reload-me"><em>waiting to be reloaded</em></div>
</p>
<p>
<a id="reload-list" href="#">Fetch new list!</a>
<ul id="the-list">
<li>Item 1</li>
<li>Item 2</li>
</ul>
</p>
<script type="text/javascript">
// a javascript comment
var aVar = 123;