Added unselect to session and rack-test driver

This commit is contained in:
Carl Porth 2010-02-19 12:37:46 -08:00
parent 4c632f3a17
commit 42a5eeb4f4
7 changed files with 83 additions and 0 deletions

View File

@ -8,6 +8,7 @@ module Capybara
class DriverNotFoundError < CapybaraError; end
class ElementNotFound < CapybaraError; end
class OptionNotFound < ElementNotFound; end
class UnselectNotAllowed < CapybaraError; end
class NotSupportedByDriverError < CapybaraError; end
class TimeoutError < CapybaraError; end
class LocateHiddenElementError < CapybaraError; end

View File

@ -54,6 +54,20 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
end
end
def unselect(option)
if node['multiple'] != 'multiple'
raise Capybara::UnselectNotAllowed, "Cannot unselect option '#{option}' from single select box."
end
if option_node = node.xpath(".//option[text()='#{option}']").first ||
node.xpath(".//option[contains(.,'#{option}')]").first
option_node.remove_attribute('selected')
else
options = node.xpath(".//option").map { |o| "'#{o.text}'" }.join(', ')
raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}"
end
end
def click
if tag_name == 'a'
driver.visit(self[:href].to_s)

View File

@ -29,6 +29,10 @@ module Capybara
raise NotImplementedError
end
def unselect(option)
raise NotImplementedError
end
def click
raise NotImplementedError
end

View File

@ -96,6 +96,11 @@ module Capybara
locate(:xpath, XPath.select(options[:from]), msg).select(value)
end
def unselect(value, options={})
msg = "cannot unselect option, no select box with id, name, or label '#{options[:from]}' found"
locate(:xpath, XPath.select(options[:from]), msg).unselect(value)
end
def attach_file(locator, path)
msg = "cannot attach file, no file field with id, name, or label '#{locator}' found"
locate(:xpath, XPath.file_field(locator), msg).set(path)

48
spec/dsl/unselect_spec.rb Normal file
View File

@ -0,0 +1,48 @@
shared_examples_for "unselect" do
describe "#unselect" do
before do
@session.visit('/form')
end
context "with multiple select" do
it "should unselect an option from a select box by id" do
@session.unselect('Commando', :from => 'form_underwear')
@session.click_button('awesome')
extract_results(@session)['underwear'].should include('Briefs', 'Boxer Briefs')
extract_results(@session)['underwear'].should_not include('Commando')
end
it "should unselect an option from a select box by label" do
@session.unselect('Commando', :from => 'Underwear')
@session.click_button('awesome')
extract_results(@session)['underwear'].should include('Briefs', 'Boxer Briefs')
extract_results(@session)['underwear'].should_not include('Commando')
end
it "should favour exact matches to option labels" do
@session.unselect("Briefs", :from => 'Underwear')
@session.click_button('awesome')
extract_results(@session)['underwear'].should include('Commando', 'Boxer Briefs')
extract_results(@session)['underwear'].should_not include('Briefs')
end
end
context "with single select" do
it "should raise an error" do
running { @session.unselect("English", :from => 'form_locale') }.should raise_error(Capybara::UnselectNotAllowed)
end
end
context "with a locator that doesn't exist" do
it "should raise an error" do
running { @session.unselect('foo', :from => 'does not exist') }.should raise_error(Capybara::ElementNotFound)
end
end
context "with an option that doesn't exist" do
it "should raise an error" do
running { @session.unselect('Does not Exist', :from => 'form_underwear') }.should raise_error(Capybara::OptionNotFound)
end
end
end
end

View File

@ -57,6 +57,7 @@ shared_examples_for "session" do
it_should_behave_like "has_field"
it_should_behave_like "select"
it_should_behave_like "uncheck"
it_should_behave_like "unselect"
it_should_behave_like "locate"
it_should_behave_like "within"
it_should_behave_like "current_url"

View File

@ -130,6 +130,16 @@
</select>
</p>
<p>
<label for="form_underwear">Underwear</label>
<select name="form[underwear][]" id="form_underwear" multiple="multiple">
<option selected="selected">Boxer Briefs</option>
<option>Boxers</option>
<option selected="selected">Briefs</option>
<option selected="selected">Commando</option>
</select>
</p>
<div style="display:none;">
<label for="form_first_name_hidden">
Super Secret