Skip to content

Instantly share code, notes, and snippets.

@Inviz
Created April 13, 2009 09:49
Show Gist options
  • Select an option

  • Save Inviz/94374 to your computer and use it in GitHub Desktop.

Select an option

Save Inviz/94374 to your computer and use it in GitHub Desktop.
Then /it should (not )?have "(.*?)" (listing|saying|listing exactly|saying exactly|saying like|mentioning|mentioning any of) "(.*?)"/ do |negation, selector, action, text|
value = negation.blank?
should = value ? :should : :should_not
mode = value ? "" : " attempt to"
And %{I#{mode} focus at "#{selector}"}
case action
when "saying exactly"
text.send(should) == @focused_scope.to_s.gsub(/<\/?[^>]*>/, " ").gsub(/\s+/, ' ').gsub(/,\s*/, ', ').strip
when "saying like"
@focused_scope.to_s.gsub(/<\/?[^>]*>/, " ").gsub(/\s+/, ' ').gsub(/\s*,\s*/, ', ').strip.include?(text).send(should) == true
when "saying"
And %{I #{negation}should see "#{text}"}
when "listing exactly"
items = []
@focused_scope.to_s.gsub(/<li[^>]*>(.*?)<\/li>/mi) do |item|
items.push item.to_s.gsub(/<.*?class=['"][^'"]*?(badge)[^'"].*?>.*?<.*?>/, "").gsub(/<\/?[^>]*>/, "").gsub(/\s+/, ' ').strip.gsub(/\.$/, '')
end
items.send(should) == text.split(",").map{|i| i.strip}
when "listing"
items = []
@focused_scope.to_s.gsub(/<li[^>]*>(.*?)<\/li>/mi) do |item|
items.push item.to_s.gsub(/<.*?class=['"][^'"]*?(badge).*?>.*?<.*?>/m, "").gsub(/<\/?[^>]*>/, "").gsub(/\s+/, ' ').strip.gsub(/\.$/, '')
end
text.split(",").all?{|i| items.include? i.strip}.send(should) == true
when "mentioning"
text.split(",").each do |bit|
And %{I should #{negation}see "#{bit.strip}"}
end
when "mentioning any of"
text.split(",").each do |bit|
And %{I should #{negation}see "#{bit.strip}"}
end
end
And %{I stop focusing}
end
When /I (try to|attempt to)? ?(?:focus|look) (?:at|on) "(.*)"/ do |attempt, selector|
@focused_scope = nil
begin
response_body.should(have_selector(selector) do |scope|
@focused_scope = scope
end)
rescue Exception => e
@focused_scope = ''
raise e if attempt.blank?
end
if (!@response_patched)
@response_patched = true
instance_eval do
def response_body
@focused_scope || super
end
end
end
end
When /I stop (?:focus|look)ing/ do
@focused_scope = nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment