20

次の HTML を生成する form_tag があります。

<form accept-charset="UTF-8" action="http://www.example.com/product_page" id="dates_form" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div>
  Min date: <input id="datepicker_mini" name="datepicker_mini" type="text" />
  Max date: <input id="datepicker_maxi" name="datepicker_maxi" type="text" />
</form>

上記は、生成された HTML のすべてです。この問題のデバッグをできるだけ簡単にするために、パーシャルとレイアウトをすべて削除しました。

datepicker フィールドは、jquery UI の datepicker を呼び出します。

テストに JavaScript を使用せず、日付フィールドにテキスト日付を入力するだけです。試してみfill_in "datepicker_mini", :with => "01/01/2010"ましたが、テストが失敗することはありませんが、テストに使用save_and_open_pageしたときにフィールドに入力されません。

更新: テストコード

it "runs report" do
  login_test_user
  within("#dates_form") do
    fill_in "datepicker_mini", :with => "01/01/2010"
    fill_in "datepicker_maxi", :with => "01/01/2020"
  end
  save_and_open_page
end

どんな助けでも大歓迎です。

ありがとう。

4

5 に答える 5

8

私はこの解決策が好きです:

page.execute_script("$('#show_sale_date').datepicker('setDate', '01/01/2010')")

ここから取得 http://api.jqueryui.com/datepicker/#method-setDate

于 2014-07-24T17:16:46.043 に答える
0

上記の解決策は私にはうまくいきませんでした。おそらく、私がテストを書いているアプリケーションに入力フィールドがないからでしょう。私のソリューションはかなり醜いですが、日付を選択し、GUI のみの日付ピッカーで選択を検証します。

##selects 'Custom' option, then specified dates, and verifies that specified dates are displayed.
Then(/^select "Custom" values, "([^"]*)" to "([^"]*)" and verify selection$/) do |arg1,arg2|
  step %{select 'Custom' from timespan drop-down}
  start = Date.strptime("#{arg1}",'%m/%d/%Y')
  start_day = start.day
  start_month = start.strftime('%b')
  start_year = start.year
  stop = Date.strptime("#{arg2}",'%m/%d/%Y')
  stop_day = stop.day
  stop_month = stop.strftime('%b')
  stop_year = stop.year
  within('.left.hasDatepicker') do
    find('.ui-datepicker-year').click
    find('option', :text => start_year).click
    find('.ui-datepicker-month').click
    find('option', :text => start_month).click
    find('.ui-state-default', :text => start_day, :match => :prefer_exact).click
  end
  within('.right.customdatepicker') do
    find('.ui-datepicker-year').click
    find('option', :text => stop_year).click
    find('.ui-datepicker-month').click
    find('option', :text => stop_month).click
    find('.ui-state-default', :text => stop_day, :match => :prefer_exact).click
  end
  find('input[value="Go"]').click
  date_picker = find('.timespanperiod').text
  start_date, end_date = date_picker.split(" - ")
  start_formatted = Date.strptime("#{start_date}",'%d/%m/%Y')
  end_formatted = Date.strptime("#{end_date}",'%d/%m/%Y')
  start_formatted.should eq (Date.strptime("#{arg1}",'%m/%d/%Y'))
  end_formatted.should eq (Date.strptime("#{arg2}",'%m/%d/%Y'))
end
于 2015-01-23T17:00:17.007 に答える