This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Adding test for select_datetime

  • Loading branch information...
vinibaggio committed Feb 28, 2011
1 parent 2c1c98a commit d255a3984d384944f2eee9b2a571db2deea85f73
Showing with 94 additions and 13 deletions.
  1. +36 −13 lib/hermes/actions.rb
  2. +57 −0 test/hermes/actions_test.rb
  3. +1 −0 test/test_helper.rb
View
@@ -2,23 +2,46 @@ module Hermes
# A few actions built on top of Capybara.
module Actions
def select_datetime(datetime, options={})
no_select_msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found"
input = find(:xpath, XPath::HTML.select(options[:from]), :message => no_select_msg)
select_id = input[:id].sub(/_\di$/, '')
find_and_select_option(select_id, 1, datetime.year)
find_and_select_option(select_id, 2, datetime.month)
find_and_select_option(select_id, 3, datetime.day)
find_and_select_option(select_id, 4, datetime.hour)
find_and_select_option(select_id, 5, datetime.min)
select_prefix = select_prefix_from_options(options)
find_and_select_option(select_prefix, 1, datetime.year)
find_and_select_option(select_prefix, 2, datetime.month)
find_and_select_option(select_prefix, 3, string_with_double_digits(datetime.day))
find_and_select_option(select_prefix, 4, string_with_double_digits(datetime.hour))
find_and_select_option(select_prefix, 5, string_with_double_digits(datetime.min))
end
private
def find_and_select_option(select_id, order, value)
no_select_msg = "cannot select option, no select box with id, name, or label '#{select_id}' found"
no_option_msg = "cannot select option, no option with text '#{value}' in select box '#{select_id}'"
select = find(:css, "##{select_id}_#{order}i", :message => no_select_msg)
def find_and_select_option(select_prefix, order, 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}_#{order}i", :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
@@ -0,0 +1,57 @@
require 'test_helper'
class ActionsTest < Hermes::IntegrationCase
setup do
visit '/users/new'
end
test "select date and time should raise error when id_prefix is not supplied" do
assert_raise ArgumentError do
select_datetime(Time.current, {})
end
end
test "select date and time using id_prefix" do
date = DateTime.new(2011, 2, 28, 16, 0)
select_datetime(date, :id_prefix => 'user_active_at')
assert find_selected_option('user[active_at(1i)]', '2011')
assert find_selected_option('user[active_at(2i)]', '2')
assert find_selected_option('user[active_at(3i)]', '28')
assert find_selected_option('user[active_at(4i)]', '16')
assert find_selected_option('user[active_at(5i)]', '00')
end
test "select date and time using from as label" do
date = DateTime.new(2011, 2, 28, 16, 0)
select_datetime(date, :from => 'Active at')
assert find_selected_option('user[active_at(1i)]', '2011')
assert find_selected_option('user[active_at(2i)]', '2')
assert find_selected_option('user[active_at(3i)]', '28')
assert find_selected_option('user[active_at(4i)]', '16')
assert find_selected_option('user[active_at(5i)]', '00')
end
test "select date and time didn't find the label" do
msg = "cannot select option, select with label 'Unexistant' not found"
assert_raise_with_message Capybara::ElementNotFound, msg do
select_datetime(Time.current, :from => 'Unexistant')
end
end
test "select date and time didn't find the id prefix" do
msg = "cannot select option, no select box with id prefix 'unexistant' found"
assert_raise_with_message Capybara::ElementNotFound, msg do
select_datetime(Time.current, :id_prefix => 'unexistant')
end
end
private
def find_selected_option(select_name, value)
find("select[name='#{select_name}'] > option[selected][value='#{value}']")
end
end
View
@@ -25,3 +25,4 @@
# Support
require 'support/models'
require 'support/assertions'

0 comments on commit d255a39

Please sign in to comment.