module Test::Unit::Capybara::Assertions
Constants
- AssertionMessage
@private
- CONTENT_TYPE_SHORTCUTS
@private
Public Instance Methods
@param […] args (see {::Capybara::Node::Finders#all}) @return [Array<::Capybara::Element>] The found elements.
@see Capybara::Node::Finders#all
@overload assert_all
(*args)
Passes if the selector finds one or more elements from the current node. @example Pass case # Actual response: # <html> # <body> # <h1>Hello</h1> # <h2>Yay!</h2> # <div class="section"> # <h2>World</h2> # </div> # </body> # </html> h2_elements = assert_page_all("h2") p h2_elements # => [#<Capybara::Element tag="h2" path="/html/body/h2">, # #<Capybara::Element tag="h2" path="/html/body/div/h2">] @example Failure case # Actual response: # <html> # <body> # <h1>Hello</h1> # <h2>Yay!</h2> # <div class="section"> # <h2>World</h2> # </div> # </body> # </html> assert_page_all("h3")
@overload assert_all
(node, *args)
Passes if the selector finds one or more elements from @node@. @param [::Capybara::Node::Base] node The target node. @example Pass case (simple) # Actual response: # <html> # <body> # <h1>Hello</h1> # <h2>Yay!</h2> # <div class="section"> # <h2>World</h2> # </div> # </body> # </html> section = assert_find("div.section") p section # => #<Capybara::Element tag="h2" path="/html/body/div"> h2_elements = assert_all(section, "h2") p h2_elements # => [#<Capybara::Element tag="h2" path="/html/body/div/h2">]
# File lib/test/unit/capybara.rb, line 283 def assert_all(*args) node = nil node = args.shift if args[0].is_a?(::Capybara::Node::Base) args = normalize_page_finder_arguments(args) format = <<-EOT <?>(?) expected to find one or more elements in <?> EOT current_context = node || page.send(:current_scope) current_context_source = node_source(current_context) source_in_message = AssertionMessage.literal(current_context_source) full_message = build_message(args[:message], format, args[:locator], args[:kind], source_in_message) if node elements = node.all(*args[:finder_arguments]) else elements = all(*args[:finder_arguments]) end assert_block(full_message) do not elements.empty? end elements end
Passes if @expected@ == @source@. @source@ is a method provided by Capybara::DSL.
@source@ may be parsed depended on response Content-Type before comparing. Here are parsed Content-Types:
-
@“application/json”@ := It's parsed by @JSON.parse@.
@param [Object] expected the expected body
content. The actual body may be parsed. It depends on @:content_type@ option.
@option options [String] :content_type (nil)
the expected Content-Type. If this value is @nil@, Content-Type will not be compared. This value can be specified by abbreviated. Here are abbreviations: - @:json@ := @"application/json"@
@yield [expected_response, actual_response] the
optional compared responses normalizer.
@yieldparam [Hash] expected_response the expected
response constructed in the method.
@yieldparam [Hash] actual_response the actual
response constructed in the method.
@yieldreturn [expected_response, actual_response] the
normalized compared responses.
@example Pass case
# Actual response: # Content-Type: application/json # Body: {"status": true} assert_body({"status" => true}, :content_type => :json)
@example Failure case
# Actual response: # Content-Type: text/html # Body: <html><body>Hello</body></html> assert_body("<html><body>World</body></html>")
# File lib/test/unit/capybara.rb, line 203 def assert_body(expected, options={}, &block) content_type = options[:content_type] actual_response = { :content_type => page_content_type, :body => parsed_page_body, } expected_response = {:body => expected} if content_type expected_response[:content_type] = normalize_content_type(content_type) else actual_response.delete(:content_type) end if block_given? expected_response, actual_response = yield(expected_response, actual_response) end assert_equal(expected_response, actual_response) end
@param […] args (see {::Capybara::Node::Finders#find})
@see ::Capybara::Node::Finders#find
@overload assert_not_find
(*args, &block)
Passes if the selector doesn't find any elements from the current node. @example Pass case # Actual response: # <html> # <body> # <h1>Hello</h1> # <h2>Yay!</h2> # <div class="section"> # <h2>World</h2> # </div> # </body> # </html> assert_not_find("h3") @example Failure case # Actual response: # <html> # <body> # <h1>Hello</h1> # <h2>Yay!</h2> # <div class="section"> # <h2>World</h2> # </div> # </body> # </html> assert_not_find("h1")
@overload assert_not_find
(node, *args, &block)
Passes if the selector doesn't find any element from @node@. @param [::Capybara::Node::Base] node The target node. @example Pass case # Actual response: # <html> # <body> # <h1>Hello</h1> # <h2>Yay!</h2> # <div class="section"> # <h2>World</h2> # </div> # </body> # </html> section = find("section") p section # => #<Capybara::Element tag="h2" path="/html/body/div"> assert_not_find(section, "h1") @example Failure case # Actual response: # <html> # <body> # <h1>Hello</h1> # <h2>Yay!</h2> # <div class="section"> # <h2>World</h2> # </div> # </body> # </html> section = find("section") p section # => #<Capybara::Element tag="h2" path="/html/body/div"> assert_not_find(section, "h2")
# File lib/test/unit/capybara.rb, line 380 def assert_not_find(*args, &block) node = nil node = args.shift if args[0].is_a?(::Capybara::Node::Base) args = normalize_page_finder_arguments(args) begin if node element = node.first(*args[:finder_arguments], **args[:finder_options]) else element = first(*args[:finder_arguments], **args[:finder_options]) end rescue ::Capybara::ExpectationNotMet element = nil end format = <<-EOT <?>(?) expected to not find an element but was <?> in <?> EOT element_source = nil element_source = node_source(element) if element current_context = node || page.send(:current_scope) current_context_source = node_source(current_context) source_in_message = AssertionMessage.literal(current_context_source) full_message = build_message(args[:message], format, args[:locator], args[:kind], AssertionMessage.literal(element_source), source_in_message) assert_block(full_message) do element.nil? end end
Fails always with {::Capybara::Node::Element} is not found message.
@param [::Capybara::Node::Element] base_node The
node used as search target.
@option options [String] :message The user custom
message added to failure message.
@option options [String] :locator The query used to
find a node. It should be specified for useful failure message.
@option options [String] :kind The kind of query.
It should be specified for useful failure message.
# File lib/test/unit/capybara.rb, line 430 def flunk_find(base_node, options={}) format = <<-EOT <?>(?) expected to find an element in <?> EOT base_html = AssertionMessage.literal(node_source(base_node)) full_message = build_message(options[:message], format, options[:locator], options[:kind], base_html) assert_block(full_message) do false end end
Private Instance Methods
# File lib/test/unit/capybara.rb, line 485 def node_source(node) if node if node.base.respond_to?(:source) node.base.source elsif node.base.respond_to?(:html) node.base.html elsif node.base.respond_to?(:driver) and node.base.driver.respond_to?(:evaluate_script) node.base.driver.evaluate_script("arguments[0].outerHTML", node.base) else node.base.native.to_s end else source end end
# File lib/test/unit/capybara.rb, line 507 def normalize_content_type(content_type) CONTENT_TYPE_SHORTCUTS[content_type] || content_type end
# File lib/test/unit/capybara.rb, line 460 def normalize_page_finder_arguments(args) args = args.dup options = {} options = args.pop if args.last.is_a?(Hash) if args.size == 1 locator = args[0] if locator[0, 1] == "/" kind = :xpath args.unshift(kind) else kind = ::Capybara.default_selector end else kind, locator, = args end { :kind => kind, :locator => locator, :message => options.delete(:message), :finder_arguments => args, :finder_options => options, } end
# File lib/test/unit/capybara.rb, line 447 def page_content_type page.response_headers["Content-Type"] end
# File lib/test/unit/capybara.rb, line 451 def parsed_page_body case page_content_type when "application/json" ::JSON.parse(source) else source end end