Fixed the xpath when selecting an option so that it should match labels with preceding or trailing whitespace.

This commit is contained in:
Jonathon Padfield 2010-06-01 13:16:58 +10:00
parent b8f4230271
commit c6918f307c
3 changed files with 9 additions and 2 deletions

View File

@ -36,7 +36,7 @@ class Capybara::Driver::Selenium < Capybara::Driver::Base
end
def select(option)
option_node = node.find_element(:xpath, ".//option[text()=#{Capybara::XPath.escape(option)}]") || node.find_element(:xpath, ".//option[contains(.,#{Capybara::XPath.escape(option)})]")
option_node = node.find_element(:xpath, ".//option[normalize-space(text())=#{Capybara::XPath.escape(option)}]") || node.find_element(:xpath, ".//option[contains(.,#{Capybara::XPath.escape(option)})]")
option_node.select
rescue
options = node.find_elements(:xpath, "//option").map { |o| "'#{o.text}'" }.join(', ')
@ -49,7 +49,7 @@ class Capybara::Driver::Selenium < Capybara::Driver::Base
end
begin
option_node = node.find_element(:xpath, ".//option[text()=#{Capybara::XPath.escape(option)}]") || node.find_element(:xpath, ".//option[contains(.,#{Capybara::XPath.escape(option)})]")
option_node = node.find_element(:xpath, ".//option[normalize-space(text())=#{Capybara::XPath.escape(option)}]") || node.find_element(:xpath, ".//option[contains(.,#{Capybara::XPath.escape(option)})]")
option_node.clear
rescue
options = node.find_elements(:xpath, "//option").map { |o| "'#{o.text}'" }.join(', ')

View File

@ -37,6 +37,12 @@ shared_examples_for "select" do
extract_results(@session)['locale'].should == 'jo'
end
it "match labels with preceding or trailing whitespace" do
@session.select("Lojban", :from => 'Locale')
@session.click_button('awesome')
extract_results(@session)['locale'].should == 'jbo'
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)

View File

@ -77,6 +77,7 @@
<option value="fi">Finish</option>
<option value="no">Norwegian</option>
<option value="jo">John's made-up language</option>
<option value="jbo"> Lojban </option>
</select>
</p>