public
Last active

Cucumter select datetime

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
# ripped from http://github.com/robholland/cucumber-rails/commit/10323c578906fe7a3ed2fdab090b7e326796b386
 
module Cucumber
module Rails
module CapybaraSelectDatesAndTimes
def select_date(field, options = {})
date = Date.parse(options[:with])
within(:xpath, Capybara::XPath.fieldset(field).append(%Q{//p[label[contains(., "#{field}")]]})) do
find(:xpath, '//select[contains(@id, "_1i")]').select(date.year)
find(:xpath, '//select[contains(@id, "_2i")]').select(date.strftime('%B'))
find(:xpath, '//select[contains(@id, "_3i")]').select(date.day) if find(:xpath, '//select[contains(@id, "_3i")]').present?
end
end
def select_time(field, options = {})
time = Time.parse(options[:with])
within(:xpath, Capybara::XPath.fieldset(field).append(%Q{//p[label[contains(., "#{field}")]]})) do
find(:xpath, '//select[contains(@id, "_4i")]').select(time.hour.to_s.rjust(2,'0'))
find(:xpath, '//select[contains(@id, "_5i")]').select(time.min.to_s.rjust(2,'0'))
end
end
def select_datetime(field, options = {})
select_date(field, options)
select_time(field, options)
end
end
end
end
 
World(Cucumber::Rails::CapybaraSelectDatesAndTimes)
 
When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" time$/ do |time, selector|
select_time(selector, :with => time)
end
 
When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" date$/ do |date, selector|
select_date(selector, :with => date)
end
 
When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" date and time$/ do |datetime, selector|
select_datetime(selector, :with => datetime)
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.