Skip to content

Instantly share code, notes, and snippets.

@chrisdarroch
Created July 8, 2010 07:33
Show Gist options
  • Save chrisdarroch/467738 to your computer and use it in GitHub Desktop.
Save chrisdarroch/467738 to your computer and use it in GitHub Desktop.
# Now I can do stuff like this!
# ...
When I follow "Personal Details"
Then I should see an inline error "can't be blank" for the following formtastic fields:
| label | within |
| Full Name | .identity |
| Date of birth | .identity |
| Gender | .identity |
| Marital status | .citizenship |
And I should see an inline error "must be selected" for the formtastic field "Title" within ".identity"
And I should see an inline error "Please indicate your citizenship status" for the formtastic field "Citizenship status"
And I should see an inline error "Please indicate how many dependants you have" for the formtastic field "Number of dependants"
Then /^I should see an inline error "([^\"]*)" for the following formtastic fields:?$/ do |text, label_table|
label_table.hashes.each do |hash|
field = hash["label"]
scope = hash["within"]
if scope
Then "I should see an inline error \"#{text}\" for the formtastic field \"#{field}\" within \"#{scope}\""
else
Then "I should see an inline error \"#{text}\" for the formtastic field \"#{field}\""
end
end
end
Then /^I should see (?:the|an) (?:|inline )error "([^\"]*)" for the formtastic field "([^\"]*)"(?: within "([^\"]*)")?$/ do |text, label, scope|
with_scope(scope) do
selector = Capybara::XPath.new("//label[contains(text(),'#{label}')]/ancestor::li")
selector = selector.append("//fieldset/legend/descendant-or-self::*[contains(normalize-space(.),'#{label}')]/ancestor::li")
element = find(:xpath, selector)
if element.nil?
msg = "The the formastic field '#{label}' could not be found"
msg << " within #{scope}" if scope
msg << "."
msg << " Perhaps it is hidden?"
msg << "\n" << "The XPath produced was: #{selector}"
raise Capybara::ElementNotFound, msg
end
within(:xpath, selector) do
result = find(:xpath, Capybara::XPath.content(text))
if result.nil?
msg = "The text '#{text}' could not be found for the formastic field '#{label}'"
msg << " within #{scope}" if scope
msg << "."
msg << " Perhaps it is hidden?"
raise Capybara::ElementNotFound, msg
end
if page.respond_to? :should
page.should have_content(text)
else
assert page.has_content?(text)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment