I am using Capybara 1.0.0 for my acceptance test and really liked it until I got stuck in one place. My problem is I have used jQuery datepicker, and right now I am confused how to select date from datepicker in my test spec.
Any help would be highly appreciated.
The datepicker is tied to a standard form input field. Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay. Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.
By default, the date format of the jQuery UI Datepicker is the US format mm/dd/yy, but we can set it to a custom display format, eg: for European dates dd-mm-yyyy and so on. The solution is to use the date picker dateFormat option.
The JavaScript DatePicker (Calendar Picker) is a lightweight and mobile-friendly control that allows end users to enter or select a date value. It has month, year, and decade view options to quickly navigate to the desired date.
Maybe it's a good idea to test it because date formats might cause issues when saving into the database. Here is how I did it:
page.execute_script %Q{ $('#auction_event_date').trigger("focus") } # activate datetime picker
page.execute_script %Q{ $('a.ui-datepicker-next').trigger("click") } # move one month forward
page.execute_script %Q{ $("a.ui-state-default:contains('15')").trigger("click") } # click on day 15
The test must run with the javascript driver.
I don't like using javascript. I simply expose the alt field if Rails.env.test? and target the alt directly with Capybara.
I create a helper method that generates the datepicker input and hidden fields (its a bit long, and I think unnecessary to show it all). In that method I use:
def date_picker(options={})
alt_field = Rails.env.test? ? "string" : "hidden"
input = "#{ options[:f].input options[:field], as: alt_field.to_sym, input_html: {id: "#{options[
The "as: alt_field.to_sym" is what exposes the date_picker hidden alt field. I can then target the alt field input (which is the one handled by the model) in Capybara with:
fill_in 'userdoc[issued_date]', with: "2013-05-02
Easy, fairly clean, no javascript...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With