Added unselect to session and rack-test driver
This commit is contained in:
parent
4c632f3a17
commit
42a5eeb4f4
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -29,6 +29,10 @@ module Capybara
|
|||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def unselect(option)
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def click
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue