RSpec + Capybara— testing date_picker

Pasha Kalashnikov
1 min readMar 13, 2020

--

author @no___tea/

I use bootstrap-datepicker for years because it’s easy to integrate to my favorite simple_form.

Here is some code of my module, which I use to test date_picker inputs.

# frozen_string_literal: truemodule DatePickerHelper
def click_on_date_picker_date(input_name, value:)
find("input[name='#{input_name}']").click
value_time = value.to_datetime.to_time
move_to_needed_month value_time
find('td.day:not(.old):not(.new)', exact_text: value.split('.').first.to_i.to_s).click
end
private def move_to_needed_month(value_time)
difference = TimeDifference.between(value_time, Time.now).in_months
difference = DateTime.now.day < 15 ? difference.round.floor : difference.floor
if value_time > Time.now
difference.times { find('th.next').click }
elsif value_time < Time.now
difference.times { find('th.prev').click }
end
end
end

Params:

  • input_name — name an attribute of date_picker in HTML code;
  • value — date, you want to set in this input (in this case we expecting value in Russian %d.%m.%Y , but I’m sure that this module can be used for many formats);

What does it do?

  • Click the input to open date_picker;
  • click on << or >> to get needed month and year
  • finding needed day in the month and click on it

Please, use it or share your own way to test date_pickers.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response