module Hermes # A few actions built on top of Capybara. module Actions POSTFIXES = { :year => '1i', :month => '2i', :day => '3i', :hour => '4i', :minute => '5i' }.freeze def select_datetime(datetime, options={}) select_prefix = select_prefix_from_options(options) select_time(datetime, :id_prefix => select_prefix) select_date(datetime, :id_prefix => select_prefix) end def select_time(time, options={}) select_prefix = select_prefix_from_options(options) find_and_select_option(select_prefix, POSTFIXES[:hour], string_with_double_digits(time.hour)) find_and_select_option(select_prefix, POSTFIXES[:minute], string_with_double_digits(time.min)) end def select_date(date, options={}) select_prefix = select_prefix_from_options(options) find_and_select_option(select_prefix, POSTFIXES[:year], date.year) find_and_select_option(select_prefix, POSTFIXES[:month], date.month) find_and_select_option(select_prefix, POSTFIXES[:day], string_with_double_digits(date.day)) end private def find_and_select_option(select_prefix, postfix, value) no_select_msg = "cannot select option, no select box with id prefix '#{select_prefix}' found" no_option_msg = "cannot select option, no option with text '#{value}' in select box '#{select_prefix}'" select = find(:css, "##{select_prefix}_#{postfix}", :message => no_select_msg) select.find(:xpath, ".//option[contains(./@value, '#{value}')]", :message => no_option_msg).select_option end def string_with_double_digits(number) "%.2d" % number end def select_prefix_from_options(options) if options.slice(:id_prefix, :from).values.blank? raise(ArgumentError, 'You must supply either :from or :id_prefix option') end select_prefix = options[:id_prefix] select_prefix ||= find_prefix_by_label(options[:from]) select_prefix end def find_prefix_by_label(label) message = "cannot select option, select with label '#{label}' not found" find(:xpath, "//label[contains(normalize-space(string(.)), '#{label}')]/@for", :message => message ).text end end end