Reject hidden nodes in searches

This commit is contained in:
Jonas Nicklas 2010-01-30 19:48:25 +01:00
parent cbe2f07331
commit 4202c4aafa
5 changed files with 42 additions and 40 deletions

View File

@ -36,8 +36,8 @@ module Capybara
results = results.select { |n| n.text.match(options[:text]) }
end
if options[:visible] == true
results.reject! { |n| !n.visible? }
if options[:visible] or Capybara.ignore_hidden_elements
results = results.select { |n| n.visible? }
end
results

View File

@ -10,6 +10,17 @@ shared_examples_for "all" do
@session.all("//input[@id='test_field']").first[:value].should == 'monkey'
end
it "should return an empty array when nothing was found" do
@session.all('//div[@id="nosuchthing"]').should be_empty
end
it "should accept an XPath instance" do
@session.visit('/form')
@xpath = Capybara::XPath.text_field('Name')
@result = @session.all(@xpath).map { |r| r.value }
@result.should include('Smith', 'John', 'John Smith')
end
context "with css selectors" do
it "should find the first element using the given locator" do
@session.all(:css, 'h1').first.text.should == 'This is a test'
@ -33,15 +44,14 @@ shared_examples_for "all" do
after { Capybara.default_selector = :xpath }
end
it "should return an empty array when nothing was found" do
@session.all('//div[@id="nosuchthing"]').should be_empty
end
it "should accept an XPath instance" do
@session.visit('/form')
@xpath = Capybara::XPath.text_field('Name')
@result = @session.all(@xpath).map { |r| r.value }
@result.should include('Smith', 'John', 'John Smith')
context "with visible filter" do
after { Capybara.ignore_hidden_elements = false }
it "should only find visible nodes" do
@session.all("//a[@title='awesome title']").should have(2).elements
@session.all("//a[@title='awesome title']", :visible => true).should have(1).elements
Capybara.ignore_hidden_elements = true
@session.all("//a[@title='awesome title']").should have(1).elements
end
end
context "within a scope" do

View File

@ -10,16 +10,10 @@ shared_examples_for "fill_in" do
extract_results(@session)['first_name'].should == 'Harry'
end
it "should fill in a text field by label" do
@session.fill_in('First Name', :with => 'Harry')
@session.click_button('awesome')
extract_results(@session)['first_name'].should == 'Harry'
end
it "should fill in a text field by name" do
@session.fill_in('form[first_name]', :with => 'Harry')
@session.fill_in('form[last_name]', :with => 'Green')
@session.click_button('awesome')
extract_results(@session)['first_name'].should == 'Harry'
extract_results(@session)['last_name'].should == 'Green'
end
it "should fill in a text field by label without for" do
@ -76,6 +70,16 @@ shared_examples_for "fill_in" do
extract_results(@session)['name'].should == 'Ford Prefect'
end
context "with ignore_hidden_fields" do
before { Capybara.ignore_hidden_elements = true }
after { Capybara.ignore_hidden_elements = false }
it "should not find a hidden field" do
running do
@session.fill_in('Super Secret', :with => '777')
end.should raise_error(Capybara::ElementNotFound)
end
end
context "with a locator that doesn't exist" do
it "should raise an error" do
running do

View File

@ -58,4 +58,4 @@ module Capybara
end #all
end
end
end

View File

@ -2,25 +2,6 @@
<form action="/form" method="post">
<div style="display:none;">
<label for="form_first_name_hidden">
First Name
<input type="text" name="form[first_name]" value="John" id="form_first_name_hidden"/>
</label>
<button type="submit" id="hidden_button" value="click_me">Hidden button</button>
<input type="checkbox" id="hidden_unchecked_checkbox"/>
<input type="checkbox" id="hidden_checked_checkbox" checked="checked"/>
<label for="hidden_form_locale">Locale</label>
<select name="form[locale]" id="hidden_form_locale">
<option value="sv">Swedish</option>
<option selected="selected" value="en">English</option>
<option value="fi">Finish</option>
<option value="no">Norwegian</option>
</select>
<input type="radio" value="male" id="hidden_gender_male"/>
<input type="file" id="hidden_form_upload"/>
</div>
<p>
<label for="form_first_name">
First Name
@ -130,6 +111,13 @@
<label for="form_pets_hamster">Hamster</label>
</p>
<div style="display:none;">
<label for="form_first_name_hidden">
Super Secret
<input type="text" name="form[super_secret]" value="test123" id="form_super_secret"/>
</label>
</div>
<p>
<input type="submit" name="form[awesome]" id="awe123" value="awesome"/>
<input type="submit" name="form[crappy]" id="crap321" value="crappy"/>
@ -201,4 +189,4 @@
<input type="submit" name="form[button]" value="Just a button that came first"/>
<input type="submit" name="form[button]" value="Just a button"/>
</p>
</form>
</form>