Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save oriolgual/1205778 to your computer and use it in GitHub Desktop.
Save oriolgual/1205778 to your computer and use it in GitHub Desktop.
Cucumber steps for selecting time and date (using Capybara)
require "xpath" # XPath is a separate gem now
module Cucumber
module Rails
module CapybaraSelectDatesAndTimes
def select_date(field, options = {})
date = Date.parse(options[:with])
find(:xpath, %Q{//select[contains(@id, "#{field}_1i")]}).find(:xpath, ::XPath::HTML.option(date.year.to_s)).select_option
find(:xpath, %Q{//select[contains(@id, "#{field}_2i")]}).find(:xpath, %Q{option[@value='#{date.month.to_s}']}).select_option
find(:xpath, %Q{//select[contains(@id, "#{field}_3i")]}).find(:xpath, ::XPath::HTML.option(date.day.to_s)).select_option
end
def select_time(field, options = {})
time = Time.parse(options[:with])
find(:xpath, %Q{//select[contains(@id, "#{field}_4i")]}).find(:xpath, ::XPath::HTML.option(time.hour.to_s.rjust(2,'0'))).select_option
find(:xpath, %Q{//select[contains(@id, "#{field}_5i")]}).find(:xpath, ::XPath::HTML.option(time.min.to_s.rjust(2,'0'))).select_option
end
def select_datetime(field, options = {})
select_date(field, options)
select_time(field, options)
end
end
end
end
World(Cucumber::Rails::CapybaraSelectDatesAndTimes)
# Examples
# select_time(selector, :with => time)
# select_date(selector, :with => date)
# select_datetime(selector, :with => datetime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment