Don't raise error when field not found
This commit is contained in:
parent
2d4f8b0bee
commit
43f65f0738
|
@ -49,27 +49,39 @@ module Capybara
|
|||
end
|
||||
|
||||
def fill_in(locator, options={})
|
||||
find_field(locator, :text_field, :text_area, :password_field).set(options[:with])
|
||||
field = find_field(locator, :text_field, :text_area, :password_field)
|
||||
raise Capybara::ElementNotFound, "cannot fill in, no text field, text area or password field with id or label '#{locator}' found" unless field
|
||||
field.set(options[:with])
|
||||
end
|
||||
|
||||
def choose(locator)
|
||||
find_field(locator, :radio).set(true)
|
||||
field = find_field(locator, :radio)
|
||||
raise Capybara::ElementNotFound, "cannot choose field, no radio button with id or label '#{locator}' found" unless field
|
||||
field.set(true)
|
||||
end
|
||||
|
||||
def check(locator)
|
||||
find_field(locator, :checkbox).set(true)
|
||||
field = find_field(locator, :checkbox)
|
||||
raise Capybara::ElementNotFound, "cannot check field, no checkbox with id or label '#{locator}' found" unless field
|
||||
field.set(true)
|
||||
end
|
||||
|
||||
def uncheck(locator)
|
||||
find_field(locator, :checkbox).set(false)
|
||||
field = find_field(locator, :checkbox)
|
||||
raise Capybara::ElementNotFound, "cannot uncheck field, no checkbox with id or label '#{locator}' found" unless field
|
||||
field.set(false)
|
||||
end
|
||||
|
||||
def select(value, options={})
|
||||
find_field(options[:from], :select).select(value)
|
||||
field = find_field(options[:from], :select)
|
||||
raise Capybara::ElementNotFound, "cannot select option, no select box with id or label '#{options[:from]}' found" unless field
|
||||
field.select(value)
|
||||
end
|
||||
|
||||
def attach_file(locator, path)
|
||||
find_field(locator, :file_field).set(path)
|
||||
field = find_field(locator, :file_field)
|
||||
raise Capybara::ElementNotFound, "cannot attach file, no file field with id or label '#{locator}' found" unless field
|
||||
field.set(path)
|
||||
end
|
||||
|
||||
def body
|
||||
|
@ -129,9 +141,7 @@ module Capybara
|
|||
|
||||
def find_field(locator, *kinds)
|
||||
kinds = FIELDS_PATHS.keys if kinds.empty?
|
||||
field = find_field_by_id(locator, *kinds) || find_field_by_label(locator, *kinds)
|
||||
raise Capybara::ElementNotFound, "no field of kind #{kinds.inspect} with id or label '#{locator}' found" unless field
|
||||
field
|
||||
find_field_by_id(locator, *kinds) || find_field_by_label(locator, *kinds)
|
||||
end
|
||||
alias_method :field_labeled, :find_field
|
||||
|
||||
|
|
|
@ -272,6 +272,12 @@ shared_examples_for "session" do
|
|||
@session.click_button('awesome')
|
||||
extract_results(@session)['gender'].should == 'both'
|
||||
end
|
||||
|
||||
context "with a locator that doesn't exist" do
|
||||
it "should raise an error" do
|
||||
running { @session.choose('does not exist') }.should raise_error(Capybara::ElementNotFound)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#check" do
|
||||
|
@ -290,6 +296,12 @@ shared_examples_for "session" do
|
|||
@session.click_button('awesome')
|
||||
extract_results(@session)['pets'].should include('dog', 'cat', 'hamster')
|
||||
end
|
||||
|
||||
context "with a locator that doesn't exist" do
|
||||
it "should raise an error" do
|
||||
running { @session.check('does not exist') }.should raise_error(Capybara::ElementNotFound)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#uncheck" do
|
||||
|
@ -310,6 +322,12 @@ shared_examples_for "session" do
|
|||
extract_results(@session)['pets'].should include('dog')
|
||||
extract_results(@session)['pets'].should_not include('hamster')
|
||||
end
|
||||
|
||||
context "with a locator that doesn't exist" do
|
||||
it "should raise an error" do
|
||||
running { @session.uncheck('does not exist') }.should raise_error(Capybara::ElementNotFound)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#select" do
|
||||
|
@ -328,6 +346,12 @@ shared_examples_for "session" do
|
|||
@session.click_button('awesome')
|
||||
extract_results(@session)['locale'].should == 'fi'
|
||||
end
|
||||
|
||||
context "with a locator that doesn't exist" do
|
||||
it "should raise an error" do
|
||||
running { @session.select('foo', :from => 'does not exist') }.should raise_error(Capybara::ElementNotFound)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#has_content?' do
|
||||
|
@ -509,7 +533,12 @@ shared_examples_for "session" do
|
|||
@session.click_button('Upload')
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
context "with a locator that doesn't exist" do
|
||||
it "should raise an error" do
|
||||
running { @session.attach_file('does not exist', 'foo.txt') }.should raise_error(Capybara::ElementNotFound)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#find_field' do
|
||||
|
@ -524,23 +553,17 @@ shared_examples_for "session" do
|
|||
end
|
||||
|
||||
it "should raise an error if the field doesn't exist" do
|
||||
running {
|
||||
@session.find_field('Does not exist')
|
||||
}.should raise_error(Capybara::ElementNotFound)
|
||||
@session.find_field('Does not exist').should be_nil
|
||||
end
|
||||
|
||||
it "should find only given kind of field" do
|
||||
@session.find_field('form_description', :text_field, :text_area).text.should == 'Descriptive text goes here'
|
||||
running {
|
||||
@session.find_field('form_description', :password_field)
|
||||
}.should raise_error(Capybara::ElementNotFound)
|
||||
@session.find_field('form_description', :password_field).should be_nil
|
||||
end
|
||||
|
||||
it "should be aliased as 'field_labeled' for webrat compatibility" do
|
||||
@session.field_labeled('Dog').value.should == 'dog'
|
||||
running {
|
||||
@session.field_labeled('Does not exist')
|
||||
}.should raise_error(Capybara::ElementNotFound)
|
||||
@session.field_labeled('Does not exist').should be_nil
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue