Module: Capybara::BootstrapDatepicker

Defined in:
lib/capybara-bootstrap-datepicker.rb,
lib/capybara-bootstrap-datepicker/version.rb

Overview

Adds datepicker interaction facilities to Capybara

Defined Under Namespace

Classes: Picker

Constant Summary collapse

VERSION =

the current version of the gem

'0.2.4'

Instance Method Summary collapse

Instance Method Details

#select_bootstrap_date(date_input, value) ⇒ Object

Selects a date by simulating human interaction with the datepicker

Parameters:

  • date_input

    the input field

  • value (Date)

    the date to set

  • format (String, nil)

    a valid date format used to format value



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/capybara-bootstrap-datepicker.rb', line 41

def select_bootstrap_date(date_input, value)
  date_input.click

  picker = Picker.new

  picker.goto_decade_panel
  picker.navigate_through_decades value.year

  picker.find_year(value.year).click
  picker.find_month(value.month).click
  picker.find_day(value.day).click

  fail if Date.parse(date_input.value) != value
end

#select_date(value, datepicker: :bootstrap, format: nil, from: nil, xpath: nil, **args) ⇒ Object

Selects a date by simulating human interaction with the datepicker or filling the input field

Parameters:

  • value (#to_date, String)

    any object that responds to ‘#to_date` or a parsable date string

  • datepicker (:bootstrap, :simple) (defaults to: :bootstrap)

    the datepicker to use (are supported: bootstrap or input field)

  • format (String, nil) (defaults to: nil)

    a valid date format used to format value

  • from (String, nil) (defaults to: nil)

    the path to input field (required if ‘xpath` is nil)

  • xpath (String, nil) (defaults to: nil)

    the xpath to input field (required if ‘from` is nil)

  • args (Hash)

    extra args to find the input field



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/capybara-bootstrap-datepicker.rb', line 13

def select_date(value, datepicker: :bootstrap, format: nil, from: nil, xpath: nil, **args)
  fail "Must pass a hash containing 'from' or 'xpath'" if from.nil? && xpath.nil?

  value = value.respond_to?(:to_date) ? value.to_date : Date.parse(value)
  date_input = xpath ? find(:xpath, xpath, **args) : find_field(from, **args)

  case datepicker
  when :bootstrap
    select_bootstrap_date date_input, value
  else
    select_simple_date date_input, value, format
  end

  first(:xpath, '//body').click
end

#select_simple_date(date_input, value, format = nil) ⇒ Object

Selects a date by filling the input field

Parameters:

  • date_input

    the input field

  • value (Date)

    the date to set

  • format (String, nil) (defaults to: nil)

    a valid date format used to format value



33
34
35
36
37
# File 'lib/capybara-bootstrap-datepicker.rb', line 33

def select_simple_date(date_input, value, format = nil)
  value = value.strftime format unless format.nil?

  date_input.set "#{value}\e"
end