|
1 |
| -require "capybara-bootstrap-datepicker/version" |
| 1 | +require 'capybara-bootstrap-datepicker/version' |
2 | 2 | require 'rspec/core'
|
3 | 3 |
|
4 | 4 | module Capybara
|
5 | 5 | module BootstrapDatepicker
|
6 |
| - def select_date(value, options = {}) |
7 |
| - raise "Must pass a hash containing 'from' or 'xpath'" unless options.is_a?(Hash) and [:from, :xpath].any? { |k| options.has_key? k } |
| 6 | + def select_date(value, datepicker: :boostrap, format: nil, from: nil, xpath: nil) |
| 7 | + fail "Must pass a hash containing 'from' or 'xpath'" if from.nil? && xpath.nil? |
8 | 8 |
|
9 |
| - picker = options.delete :datepicker |
10 |
| - format = options.delete :format |
11 |
| - from = options.delete :from |
12 |
| - xpath = options.delete :xpath |
| 9 | + value = Date.parse(value) unless value.respond_to? :to_date |
13 | 10 |
|
14 |
| - date = value.is_a?(Date) || value.is_a?(Time) ? value : Date.parse(value) |
| 11 | + date_input = xpath ? find(:xpath, xpath) : find_field(from) |
15 | 12 |
|
16 |
| - date_input = xpath ? find(:xpath, xpath, options) : find_field(from, options) |
17 |
| - |
18 |
| - case picker |
| 13 | + case datepicker |
19 | 14 | when :bootstrap
|
20 |
| - date_input.click |
21 |
| - datepicker = find(:xpath, '//body').find('.datepicker') |
| 15 | + select_bootstrap_date date_input, value |
| 16 | + else |
| 17 | + select_simple_date date_input, value |
| 18 | + end |
| 19 | + end |
22 | 20 |
|
23 |
| - datepicker_years = datepicker.find('.datepicker-years', visible: false) |
24 |
| - datepicker_months = datepicker.find('.datepicker-months', visible: false) |
25 |
| - datepicker_days = datepicker.find('.datepicker-days', visible: false) |
| 21 | + def select_simple_date(date_input, value) |
| 22 | + value = value.strftime format if format.present? |
26 | 23 |
|
27 |
| - datepicker_current_decade = datepicker_years.find('th.datepicker-switch', visible: false) |
28 |
| - datepicker_current_year = datepicker_months.find('th.datepicker-switch', visible: false) |
29 |
| - datepicker_current_month = datepicker_days.find('th.datepicker-switch', visible: false) |
| 24 | + date_input.set "#{value}\e" |
| 25 | + first(:xpath, '//body').click |
| 26 | + end |
30 | 27 |
|
31 |
| - datepicker_current_month.click if datepicker_days.visible? |
32 |
| - datepicker_current_year.click if datepicker_months.visible? |
| 28 | + def select_bootstrap_date(date_input, value) |
| 29 | + date_input.click |
| 30 | + picker = find(:xpath, '//body').find('.datepicker') |
33 | 31 |
|
34 |
| - decade_start, decade_end = datepicker_current_decade.text.split('-').map &:to_i |
| 32 | + picker_years = picker.find('.datepicker-years', visible: false) |
| 33 | + picker_months = picker.find('.datepicker-months', visible: false) |
| 34 | + picker_days = picker.find('.datepicker-days', visible: false) |
35 | 35 |
|
36 |
| - if date.year < decade_start.to_i |
37 |
| - gap = decade_start/10 - date.year/10 |
38 |
| - gap.times { datepicker_years.find('th.prev').click } |
39 |
| - elsif date.year > decade_end |
40 |
| - gap = date.year/10 - decade_end/10 |
41 |
| - gap.times { datepicker_years.find('th.next').click } |
42 |
| - end |
| 36 | + picker_current_decade = picker_years.find('th.datepicker-switch', visible: false) |
| 37 | + picker_current_year = picker_months.find('th.datepicker-switch', visible: false) |
| 38 | + picker_current_month = picker_days.find('th.datepicker-switch', visible: false) |
43 | 39 |
|
44 |
| - datepicker_years.find('.year', text: date.year).click |
45 |
| - datepicker_months.find('.month', text: date.strftime("%b")).click |
46 |
| - day_xpath = <<-eos |
47 |
| - .//*[contains(concat(' ', @class, ' '), ' day ') |
48 |
| - and not(contains(concat(' ', @class, ' '), ' old ')) |
49 |
| - and not(contains(concat(' ', @class, ' '), ' new ')) |
50 |
| - and normalize-space(text())='#{date.day}'] |
51 |
| - eos |
52 |
| - datepicker_days.find(:xpath, day_xpath).trigger :click |
| 40 | + picker_current_month.click if picker_days.visible? |
| 41 | + picker_current_year.click if picker_months.visible? |
53 | 42 |
|
54 |
| - expect(Date.parse date_input.value).to eq date |
55 |
| - expect(page).to have_no_css '.datepicker' |
56 |
| - when :jquery |
57 |
| - raise "jQuery UI datepicker support is not implemented." |
58 |
| - else |
59 |
| - date = date.strftime format unless format.nil? |
| 43 | + decade_start, decade_end = picker_current_decade.text.split('-').map(&:to_i) |
60 | 44 |
|
61 |
| - date_input.set "#{date}\e" |
62 |
| - first(:xpath, '//body').click |
| 45 | + if value.year < decade_start.to_i |
| 46 | + gap = decade_start / 10 - value.year / 10 |
| 47 | + gap.times { picker_years.find('th.prev').click } |
| 48 | + elsif value.year > decade_end |
| 49 | + gap = value.year / 10 - decade_end / 10 |
| 50 | + gap.times { picker_years.find('th.next').click } |
63 | 51 | end
|
| 52 | + |
| 53 | + picker_years.find('.year', text: value.year).click |
| 54 | + picker_months.find('.month', text: value.strftime('%b')).click |
| 55 | + day_xpath = <<-eos |
| 56 | + .//*[contains(concat(' ', @class, ' '), ' day ') |
| 57 | + and not(contains(concat(' ', @class, ' '), ' old ')) |
| 58 | + and not(contains(concat(' ', @class, ' '), ' new ')) |
| 59 | + and normalize-space(text())='#{value.day}'] |
| 60 | + eos |
| 61 | + picker_days.find(:xpath, day_xpath).trigger :click |
| 62 | + |
| 63 | + fail if Date.parse(date_input.value) != value |
| 64 | + fail unless page.has_no_css? '.datepicker' |
64 | 65 | end
|
65 | 66 | end
|
66 | 67 | end
|
67 | 68 |
|
68 | 69 | RSpec.configure do |c|
|
69 | 70 | c.include Capybara::BootstrapDatepicker
|
70 | 71 | end
|
71 |
| - |
0 commit comments