web-dev-qa-db-ja.com

カピバラで隠しフィールドを埋める方法は?

テキストフィールド、テキストエリア、またはパスワードフィールドに値を設定する場合、fill_in something, :with => some_valuesomethingとしてid、名前、またはラベルを使用できることは既にわかっています。ただし、<input type="hidden">フィールドに値を設定しようとすると、このようなアプローチは失敗します(通常、これらは個別にテストするクライアント側のスクリプトで満たされているため、実行したいです)。カピバラでこのような隠しフィールドを設定するにはどうすればよいですか?出来ますか?

HTML:

<input id='offer_latitude' name='offer[latitude]' type='hidden'>
<input id='offer_longitude' name='offer[longitude]' type='hidden'>

仕様:

describe "posting new offer" do
  it "should add new offer" do
    visit '/offer/new'
    fill_in 'offer[latitude]', :with => '11.11'
    fill_in 'offer[longitude]', :with => '12.12'
    click_on 'add'
  end
end

与える:

1) posting new offer should add new offer
   Failure/Error: fill_in 'offer[latitude]', :with => '11.11'
   Capybara::ElementNotFound:
     cannot fill in, no text field, text area or password field with id, name, or label 'offer[latitude]' found
56
skalee

非表示フィールドを見つけて、その値を設定する必要があります。いくつかの方法がありますが、これはおそらく最も簡単です

find(:xpath, "//input[@id='my_hidden_field_id']").set "my value"

本番環境でclient_sideスクリプトを実行している場合、javascript準拠のドライバーで実行するようにcapybaraに指示することができます

page.execute_script("$('hidden_field_id').my_function()")
82
DVG

同じ結果を得るには多くの方法があります。私が一番好きなのは:

first('input#id.class', visible: false).set("your value")
50
Luis D Urraca

Poltergeist/phantomjsをドライバーとして使用していて、jqueryがyaで機能しない場合は、常に昔ながらのjsがあります。

page.execute_script("document.getElementById('#some-id').value = 'some-value'");
1
penner