Module: Cucumber::Rails::Capybara::SelectDatesAndTimes

Defined in:
lib/cucumber/rails/capybara/select_dates_and_times.rb

Overview

This module defines methods for selecting dates and times

Instance Method Summary collapse

Instance Method Details

#select_date(date, options) ⇒ Object

Select a Rails date. Options hash must include from: label



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cucumber/rails/capybara/select_dates_and_times.rb', line 9

def select_date(date, options)
  date = Date.parse(date)
  base_dom_id = get_base_dom_from_options(options)

  # Rails 7 use HTML5 input type="date" by default. If input is not present fallback to plain select boxes alternative.
  # It's safe to use has_css? without waiting/retry. We already know field's label is visible
  if html5_input_field_present?(base_dom_id)
    fill_in options[:from], with: date
  else
    find(:xpath, ".//select[@id='#{base_dom_id}_1i']").select(date.year.to_s)
    find(:xpath, ".//select[@id='#{base_dom_id}_2i']").select(I18n.l(date, format: '%B'))
    find(:xpath, ".//select[@id='#{base_dom_id}_3i']").select(date.day.to_s)
  end
end

#select_datetime(datetime, options) ⇒ Object

Select a Rails datetime. Options hash must include from: label



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cucumber/rails/capybara/select_dates_and_times.rb', line 40

def select_datetime(datetime, options)
  base_dom_id = get_base_dom_id_from_label_tag(options[:from])

  # Rails 7 use HTML5 input type="datetime-local" by default. If input is not present fallback to plain select boxes alternative.
  # It's safe to use has_css? without waiting/retry. We already know field's label is visible
  if html5_input_field_present?(base_dom_id)
    fill_in options[:from], with: DateTime.parse(datetime)
  else
    extended_options = options.merge(base_dom_id: base_dom_id)
    select_date(datetime, extended_options)
    select_time(datetime, extended_options)
  end
end

#select_time(time, options) ⇒ Object

Select a Rails time. Options hash must include from: label



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cucumber/rails/capybara/select_dates_and_times.rb', line 25

def select_time(time, options)
  time = Time.zone.parse(time)
  base_dom_id = get_base_dom_from_options(options)

  # Rails 7 use HTML5 input type="time" by default. If input is not present fallback to plain select boxes alternative.
  # It's safe to use has_css? without waiting/retry. We already know field's label is visible
  if html5_input_field_present?(base_dom_id)
    fill_in options[:from], with: time
  else
    find(:xpath, ".//select[@id='#{base_dom_id}_4i']").select(time.hour.to_s.rjust(2, '0'))
    find(:xpath, ".//select[@id='#{base_dom_id}_5i']").select(time.min.to_s.rjust(2,  '0'))
  end
end