Merge remote branch 'badcarl/add_multiple_select'
This commit is contained in:
commit
6cf594cf9b
|
@ -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
|
||||
|
|
|
@ -13,8 +13,12 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
|
|||
attr_name = name.to_s
|
||||
case
|
||||
when 'select' == tag_name && 'value' == attr_name
|
||||
option = node.xpath(".//option[@selected='selected']").first || node.xpath(".//option").first
|
||||
option.content if option
|
||||
if node['multiple'] == 'multiple'
|
||||
node.xpath(".//option[@selected='selected']").map { |option| option.content }
|
||||
else
|
||||
option = node.xpath(".//option[@selected='selected']").first || node.xpath(".//option").first
|
||||
option.content if option
|
||||
end
|
||||
when 'input' == tag_name && 'checkbox' == type && 'checked' == attr_name
|
||||
node[attr_name] == 'checked' ? true : false
|
||||
else
|
||||
|
@ -54,6 +58,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)
|
||||
|
|
|
@ -35,6 +35,20 @@ class Capybara::Driver::Selenium < Capybara::Driver::Base
|
|||
raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}"
|
||||
end
|
||||
|
||||
def unselect(option)
|
||||
if node['multiple'] != 'multiple'
|
||||
raise Capybara::UnselectNotAllowed, "Cannot unselect option '#{option}' from single select box."
|
||||
end
|
||||
|
||||
begin
|
||||
option_node = node.find_element(:xpath, ".//option[text()='#{option}']") || node.find_element(:xpath, ".//option[contains(.,'#{option}')]")
|
||||
option_node.clear
|
||||
rescue
|
||||
options = node.find_elements(: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
|
||||
node.click
|
||||
end
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -44,6 +44,16 @@ shared_examples_for "select" do
|
|||
end
|
||||
|
||||
context "with multiple select" do
|
||||
it "should return an empty value" do
|
||||
@session.find_field('Language').value.should == []
|
||||
end
|
||||
|
||||
it "should return value of the selected options" do
|
||||
@session.select("Ruby", :from => 'Language')
|
||||
@session.select("Javascript", :from => 'Language')
|
||||
@session.find_field('Language').value.should include('Ruby', 'Javascript')
|
||||
end
|
||||
|
||||
it "should select one option" do
|
||||
@session.select("Ruby", :from => 'Language')
|
||||
@session.click_button('awesome')
|
||||
|
|
|
@ -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