Makes have_tag work outside of Merb.

This commit is contained in:
Amos King 2009-01-23 21:32:54 -06:00
parent 0a021059d6
commit 483559f279
3 changed files with 74 additions and 1 deletions

View File

@ -37,6 +37,7 @@
* Minor enhancements * Minor enhancements
* have_tag and the corresponding asserts work outside of merb, including selenium mode. (Amos King)
* Create tmp/pids directory if directory does not exist. (Amos King) * Create tmp/pids directory if directory does not exist. (Amos King)
* Added Selenium grid configuration and support. (Amos King && Cornel Borcean) * Added Selenium grid configuration and support. (Amos King && Cornel Borcean)
* Support passing an ActiveRecord model to #within when in Rails mode [#68] (Luke Melia) * Support passing an ActiveRecord model to #within when in Rails mode [#68] (Luke Melia)

View File

@ -1,4 +1,4 @@
require "webrat/selenium/matchers/have_xpath" require "webrat/selenium/matchers/have_xpath"
require "webrat/selenium/matchers/have_selector" require "webrat/selenium/matchers/have_selector"
# require "webrat/core/matchers/have_tag" require "webrat/selenium/matchers/have_tag"
require "webrat/selenium/matchers/have_content" require "webrat/selenium/matchers/have_content"

View File

@ -0,0 +1,72 @@
module Webrat
module Selenium
module Matchers
class HaveTag < HaveSelector #:nodoc:
# ==== Returns
# String:: The failure message.
def failure_message
"expected following output to contain a #{tag_inspect} tag:\n#{@document}"
end
# ==== Returns
# String:: The failure message to be displayed in negative matches.
def negative_failure_message
"expected following output to omit a #{tag_inspect}:\n#{@document}"
end
def tag_inspect
options = @expected.last.dup
content = options.delete(:content)
html = "<#{@expected.first}"
options.each do |k,v|
html << " #{k}='#{v}'"
end
if content
html << ">#{content}</#{@expected.first}>"
else
html << "/>"
end
html
end
def query
options = @expected.last.dup
selector = @expected.first.to_s
selector << ":contains('#{options.delete(:content)}')" if options[:content]
options.each do |key, value|
selector << "[#{key}='#{value}']"
end
Nokogiri::CSS::Parser.parse(selector).map { |ast| ast.to_xpath }
end
end
def have_tag(name, attributes = {}, &block)
HaveTag.new([name, attributes], &block)
end
alias_method :match_tag, :have_tag
# Asserts that the body of the response contains
# the supplied tag with the associated selectors
def assert_have_tag(name, attributes = {})
ht = HaveTag.new([name, attributes])
assert ht.matches?(response), ht.failure_message
end
# Asserts that the body of the response
# does not contain the supplied string or regepx
def assert_have_no_tag(name, attributes = {})
ht = HaveTag.new([name, attributes])
assert !ht.matches?(response), ht.negative_failure_message
end
end
end
end