Module: Test::Unit::Capybara::Assertions

Defined in:
lib/test/unit/capybara.rb

Constant Summary collapse

AssertionMessage =
::Test::Unit::Assertions::AssertionMessage

Instance Method Summary collapse

Instance Method Details

#assert_all(*args) ⇒ Array<::Capybara::Element> #assert_all(node, *args) ⇒ Array<::Capybara::Element>

Returns The found elements.

Overloads:

  • #assert_all(*args) ⇒ Array<::Capybara::Element>

    Passes if the selector finds one or more elements from the current node.

    Examples:

    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">]

    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")
  • #assert_all(node, *args) ⇒ Array<::Capybara::Element>

    Passes if the selector finds one or more elements from @node@.

    Examples:

    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">]

    Parameters:

Parameters:

  • args (...)

    (see Capybara::Node::Finders#all)

Returns:

  • (Array<::Capybara::Element>)

    The found elements.

See Also:

  • Capybara::Node::Finders#all


276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/test/unit/capybara.rb', line 276

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

#assert_body(expected, options = {}) {|expected_response, actual_response| ... } ⇒ Object

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@.

Examples:

Pass case

# Actual response:
#   Content-Type: application/json
#   Body: {"status": true}
assert_body({"status" => true}, :content_type => :json)

Failure case

# Actual response:
#   Content-Type: text/html
#   Body: <html><body>Hello</body></html>
assert_body("<html><body>World</body></html>")

Parameters:

  • expected (Object)

    the expected body content. The actual body may be parsed. It depends on @:content_type@ option.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :content_type (String) — default: 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”@

Yields:

  • (expected_response, actual_response)

    the optional compared responses normalizer.

Yield Parameters:

  • expected_response (Hash)

    the expected response constructed in the method.

  • actual_response (Hash)

    the actual response constructed in the method.

Yield Returns:

  • (expected_response, actual_response)

    the normalized compared responses.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/test/unit/capybara.rb', line 196

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

#assert_not_find(*args, &block) ⇒ Object #assert_not_find(node, *args, &block) ⇒ Object

Overloads:

  • #assert_not_find(*args, &block) ⇒ Object

    Passes if the selector doesn’t find any elements from the current node.

    Examples:

    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")

    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")
  • #assert_not_find(node, *args, &block) ⇒ Object

    Passes if the selector doesn’t find any element from @node@.

    Examples:

    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")

    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")

    Parameters:

Parameters:

  • args (...)

    (see Capybara::Node::Finders#find)

See Also:

  • Capybara::Node::Finders#find


373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/test/unit/capybara.rb', line 373

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])
    else
      element = first(*args[:finder_arguments])
    end
  rescue ::Capybara::ExpectationNotMet
    element = nil
  end
  format = <<-EOT
<?>(?) expected to not find a 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

#flunk_find(base_node, options = {}) ⇒ Object

Fails always with Capybara::Node::Element is not found message.

Parameters:

  • base_node (::Capybara::Node::Element)

    The node used as search target.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :message (String)

    The user custom message added to failure message.

  • :locator (String)

    The query used to find a node.

    It should be specified for useful failure message.

  • :kind (String)

    The kind of query.

    It should be specified for useful failure message.



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/test/unit/capybara.rb', line 421

def flunk_find(base_node, options={})
  format = <<-EOT
<?>(?) expected to find a 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