Created
May 28, 2010 18:33
-
-
Save ihoka/417536 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DateHelpers | |
DATE_TIME_SUFFIXES = { | |
:year => '1i', | |
:month => '2i', | |
:day => '3i', | |
:hour => '4i', | |
:minute => '5i' | |
} | |
def select_date(date_to_select, options ={}) | |
date = date_to_select.is_a?(Date) || date_to_select.is_a?(Time) ? | |
date_to_select : Chronic.parse(date_to_select) | |
id_prefix = id_prefix_for(options) | |
select date.year.to_s, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:year]}" | |
select date.strftime('%B'), :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:month]}" | |
select date.day.to_s, :from => "#{id_prefix}_#{DATE_TIME_SUFFIXES[:day]}" | |
end | |
def id_prefix_for(options = {}) | |
msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found" | |
locate(:xpath, Capybara::XPath.select(options[:from]), msg)['id'].gsub(/_#{DATE_TIME_SUFFIXES[:year]}$/,'') | |
end | |
def doc_table_diff(expected_table) | |
doc = Nokogiri::HTML(page.body) | |
actual_table = [] | |
yield doc, actual_table | |
expected_table.diff!(Cucumber::Ast::Table.new(actual_table)) | |
end | |
def flatten_children(node) | |
return [] if node.nil? | |
children = node.children.to_a | |
return [node.content] if children.empty? | |
children.map { |c| flatten_children(c) }.flatten | |
end | |
end | |
World(DateHelpers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" date$/ do |date, date_label| | |
select_date(date, :from => date_label) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment