Skip to content

Instantly share code, notes, and snippets.

@croaky
Created September 24, 2010 21:34
Show Gist options
  • Save croaky/596096 to your computer and use it in GitHub Desktop.
Save croaky/596096 to your computer and use it in GitHub Desktop.
how we do something similar to the first part of http://mislav.uniqpath.com/2010/09/cuking-it-right/
When I follow the comments link
Then the page should not have the leaderboard
And I should see the activity feed
And the activity feed should have a loading indicator
When /^(?:|I )press ([^"].*)$/ do |named_element|
locate(selector_for(named_element)).click
end
When /^(?:|I )follow ([^"].*)$/ do |named_element|
link = selector_for(named_element)
click_link(link)
end
When /^(?:|I )follow "([^\"]*)" within ([^"].*)$/ do |link, named_element|
parent = selector_for(named_element)
within(parent) do
click_link(link)
end
end
When /^(?:|I )select "([^\"]*)" from ([^"].*)$/ do |value, named_element|
locate(selector_for(named_element)).select(value)
end
When /^(?:|I )check ([^"].*)$/ do |named_element|
selector = selector_for(named_element)
page.locate(selector).set('checked')
end
When /^(?:|I )uncheck ([^"].*)$/ do |named_element|
selector = selector_for(named_element)
page.locate(selector).set('')
end
When /^(?:|I )choose ([^"].*)$/ do |named_element|
field = field_by_selector(selector_for(named_element))
choose(field)
end
When /^(?:|I )choose "([^"]*)" within ([^"]*)?$/ do |field, named_selector|
selector = selector_for(named_selector)
with_scope(selector) do
choose(field)
end
end
Then /^(?:|I )should see "([^\"]*)" within ([^"].*)$/ do |text, named_element|
selector = selector_for(named_element)
within selector do
page.should have_content(text)
end
end
Then /^(?:|I )should see the value "([^\"]*)" in ([^"].*) field$/ do |text, named_element|
selector = selector_for(named_element)
page.locate(selector).value.should include(text)
end
Then /^(?:|I )should not see ([^"].*) on the page$/ do |named_element|
selector = selector_for(named_element)
page.should_not have_css(selector)
end
Then /^(?:|I )should not see "([^\"]*)" within ([^"].*)$/ do |text, named_element|
selector = selector_for(named_element)
within(selector) do
page.should have_no_content(text)
end
end
Then 'the page should not have $named_element' do |named_element|
selector = selector_for(named_element)
page.should have_no_css(selector)
end
Then /^the ([^"].*) field should contain "([^\"]*)"$/ do |named_element, value|
field = selector_for(named_element)
page.locate(field).value.should == value
end
Then /^the "([^\"]*)" field within ([^"].*) should contain "([^\"]*)"$/ do |named_element, named_selector, value|
selector = selector_for(named_element)
within(selector) do
field = selector_for(named_element)
page.locate(field).value.should == value
end
end
Then /^the ([^"].*) field should not contain "([^\"]*)"$/ do |named_element, value|
field = selector_for(named_element)
if defined?(Spec::Rails::Matchers)
field_by_selector(field).value.should_not =~ /#{value}/
else
assert_no_match(/#{value}/, field_labeled(field).value)
end
end
When /^(?:|I )fill in ([^"].*) with "([^"]*)"$/ do |named_element, value|
selector = selector_for(named_element)
locate(selector).set(value)
end
Then /^the ([^"]*) checkbox should be checked$/ do |named_element|
selector = selector_for(named_element)
unless locate(selector)['checked']
raise "Expected #{named_element} to be checked"
end
end
Then /^the ([^"]*) checkbox should not be checked$/ do |named_element|
selector = selector_for(named_element)
if locate(selector)['checked']
raise "Did not expect #{named_element} to be checked"
end
end
Then /^([^"].*) should not be on the page$/ do |named_element|
page.should have_no_css(selector_for(named_element))
end
Then /^([^"].*) should be on the page$/ do |named_element|
page.should have_css(selector_for(named_element))
end
Then /^I should see "([^\"]*)" before "([^\"]*)" within ([^"].*)$/ do |first, second, named_element|
first_expression = Regexp.escape(first)
second_expression = Regexp.escape(second)
joint_expression = /#{first_expression}.*#{second_expression}/m
selector = selector_for(named_element)
within(selector) do
page.body.should =~ joint_expression
end
end
When /^I press "([^\"]*)" within ([^"].*)$/ do |button, named_element|
selector = selector_for(named_element)
within selector do
click_button button
end
end
Then /^I should( not)? see the "([^"]*)" button within ([^"].*)$/ do |not_see,button_text,named_element|
selector = selector_for(named_element)
within selector do
if not_see
page.should_not have_button(button_text)
else
page.should have_button(button_text)
end
end
end
Then /^the ([^\"].*) should be (visible|hidden)$/ do |named_element, visibility|
selector = selector_for(named_element)
Then %(the "#{selector}" element should be #{visibility})
end
Then /^the ([^\"].*) should start out hidden$/ do |named_element|
selector = selector_for(named_element)
locate(selector).should_not be_visible
end
Then /^I should not see any links within (.*)$/ do |named_element|
scope_selector = selector_for(named_element)
page.should have_no_css("#{scope_selector} a[href]")
end
Then /^(.*) should have a loading indicator$/ do |named_element|
selector = selector_for(named_element)
locate(selector)['class'].should include('loading')
end
Then /^the ([^\"]+) button should be disabled$/ do |named_element|
selector = selector_for(named_element)
button = page.locate(selector).node
raise "Expected #{named_element} button to be disabled" unless button['disabled']
end
module NamedElements
# Accepts a named element (such as "the user info area") and returns a
# selector to find that element on the page (such as "#user-info").
def selector_for(named_element)
case named_element
when /the page body/
'body'
when /the leaderboard/i
'.member-leaderboard'
when /a spinner/
'.spinner'
when /the comments link/
'a.comments'
when /the activity feed/
'#activity-feed'
# Add more named elements here
else
raise %{No selector defined for an element with name "#{named_element}"\n} +
"Check out features/support/named_elements.rb and add one"
end
end
end
World(NamedElements)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment