开发者

How does one use Selenium::WebDriver::Element#click to select an option from a SELECT element?

Using capybara 1.0.0 and selenium-webdriver 0.2.0 and in a test I have something like the following to select from a dropdown.

select 'Food & Dining', :from => 'category_id'

The test passes but I get the following complaint:

Seleni开发者_如何学编程um::WebDriver::Element#select is deprecated. Please use Selenium::WebDriver::Element#click ...

I've searched the web, the documents are sparse, anyone know how to use click to select an option of a select element?


From looking at the capybara-1.0.0 source:

# File 'lib/capybara/node/actions.rb', line 110
def select(value, options={})
  if options.has_key?(:from)
    no_select_msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found"
    no_option_msg = "cannot select option, no option with text '#{value}' in select box '#{options[:from]}'"
    select = find(:xpath, XPath::HTML.select(options[:from]), :message => no_select_msg)
    select.find(:xpath, XPath::HTML.option(value), :message => no_option_msg).select_option
  else
    no_option_msg = "cannot select option, no option with text '#{value}'"
    find(:xpath, XPath::HTML.option(value), :message => no_option_msg).select_option
  end
end

From looking at the selenium-webdriver 0.2.2 code, where the warning is being generated:

  # File 'rb/lib/selenium/webdriver/common/element.rb', line 175
  # Select this element
  #

  def select
    warn "#{self.class}#select is deprecated. Please use #{self.class}#click and determine the current state with #{self.class}#selected?"

    unless displayed?
      raise Error::ElementNotDisplayedError, "you may not select an element that is not displayed"
    end

    unless enabled?
      raise Error::InvalidElementStateError, "cannot select a disabled element"
    end

    unless selectable?
      raise Error::InvalidElementStateError, "you may only select options, radios or checkboxes"
    end

    click unless selected?
  end

so as a temporary solution to the annoying message and until capybara folks fix it on their end, I added this block of code to my cucumber features/support/env.rb file, you can also add it to your spec_helper.rb or to any testing framework file that gets loaded before running the test. Basically I am opening the class and overriding the method select and disabling the warning... just a temporary hack... not proud of it...

# June 30th, 2011
# a temporary hack to disable the annoying upstream warnings capybara > selenium-webdriver 0.2.2
# capybara folks know about this and are working on it. See:
# http://groups.google.com/group/ruby-capybara/browse_thread/thread/2cd042848332537a/7edb1699cb314862?show_docid=7edb1699cb314862
# Remove this whole block when Capybara 1.0.1 or greater are used
module Selenium
  module WebDriver
    class Element
      #
      # Select this element
      #

      def select
        #warn "#{self.class}#select is deprecated. Please use #{self.class}#click and determine the current state with #{self.class}#selected?"

        unless displayed?
          raise Error::ElementNotDisplayedError, "you may not select an element that is not displayed"
        end

        unless enabled?
          raise Error::InvalidElementStateError, "cannot select a disabled element"
        end

        unless selectable?
          raise Error::InvalidElementStateError, "you may only select options, radios or checkboxes"
        end

        click unless selected?
      end
    end
  end
end


for those of you that are still having trouble with this, you may simply use the latest capybara from the master branch which should fix everything including recent issues with Launchy.

Just change:

gem 'capybara'

to

gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'

then

bundle update

and you're good ^_^


You could also just add the following to your Gemfile

gem 'selenium-webdriver', '0.2.1'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜