2011-06-29 12 views
4

capybara 1.0.0とselenium-webdriver 0.2.0を使用し、テストでは次のようなものがドロップダウンから選択できます。Selenium :: WebDriver :: Element#クリックすると、SELECT要素からオプションを選択できますか?

私はウェブを検索した
Selenium::WebDriver::Element#select is deprecated. Please use Selenium::WebDriver::Element#click ... 

、ドキュメントはまばらで、誰でもselect要素のオプションを選択する]をクリックし使用する方法を知っている:

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

テストパスは、私は以下の苦情を取得します?

答えて

4

だけの変更:

gem 'capybara' 

その後、

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

bundle update 

に、あなたは簡単な正直のために^ _^

+0

本当に。私のテストで、 'Capybara :: Driver :: RackTest :: Node 'クラスの未定義メソッド' click'を取得しました – simianarmy

+0

この回答は若干古いかもしれませんので、@simianarmy – fivetwentysix

10

カピバラ-1.0.0ソースを見てから:

# 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 

警告が生成されているセレンwebdriverを0.2.2コードを見てから:

# 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 

ようにあなたのspec_helper.rbまたは任意のテストフレームワークにそれを追加することができます。このコードブロックは、私のキュウリのfeatures/support/env.rbファイルに追加されました。テストを実行する前にロードされるファイル。 は基本的に私はあなたにもちょうどあなたに以下を追加することができ

# 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 
+0

+1もいいです。 :) –

+0

私の端末を一時的に掃除するための+1。 :-) – Gav

+0

+1のためにハニ。兄弟、あなたロック。 – Tass

1

...それを誇りに思っていない...ただ、一時的なハック...クラスを開き、selectメソッドをオーバーライドして、警告を無効にしていますGemfileまだあなたは、単にLaunchyのとの最近の問題を含め、すべてを修正する必要がmasterブランチから最新のカピバラを使用することができ、これで問題が発生したことあなたのそれらのための

gem 'selenium-webdriver', '0.2.1' 
+0

-1悲しいことに、これはcapybara 1.0.0では動作しません。Firefoxには接続できません。私は0.2.1 nad 0.2.0で試しましたが、0.2.2しか動作しません。 – oma

関連する問題