Class: Brut::SpecSupport::Matchers::HaveHTMLAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/brut/spec_support/matchers/have_html_attribute.rb

Overview

Used in component or page specs to check that a given node has a particular HTML attribnute, or an attribute with a speecific value.

Examples:

result = generate_and_parse(page)
expect(result.e!("blockquote")).to have_html_attribute(:id)

Expecting a value as well

result = generate_and_parse(page)
expect(result.e!("blockquote")).to have_html_attribute(id: "foo")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result, attribute) ⇒ HaveHTMLAttribute

Returns a new instance of HaveHTMLAttribute.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/brut/spec_support/matchers/have_html_attribute.rb', line 42

def initialize(result, attribute)
  @error = nil
  if result.kind_of?(Nokogiri::XML::NodeSet)
    if result.length > 1
      @error = "Received #{result.length} matching nodes, when only one should've been returned"
    elsif result.length == 0
      @error = "Received no matching nodes to examine"
    else
      result = result.first
    end
  else
    object_to_check = if result.kind_of?(SimpleDelegator)
                        result.__getobj__
                      else
                        result
                      end
    if !object_to_check.kind_of?(Nokogiri::XML::Element)
      @error = "Received a #{result.class} instead of a NodeSet or Element, as could be returned by `.css(...)`"
    end
  end
  if !@error
    if attribute.kind_of?(Hash)
      attribute_name  = attribute.keys.first.to_s
      attribute_value = attribute.values.first.to_s
    else
      attribute_name = attribute.to_s
      attribute_value = :any
    end

    nokogiri_attribute = result.attribute(attribute_name)
    if nokogiri_attribute
      if attribute_value != :any
        found_value = result.attribute(attribute_name).value

        if found_value != attribute_value
          @error = "Value for '#{attribute_name}' was '#{found_value}'. Expected '#{attribute_value}'"
        end
      end
    else
      @error = "Did not find attribute '#{attribute_name}' on element.  Found: #{result.attributes.keys.join(", ")}"
    end
  end
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



40
41
42
# File 'lib/brut/spec_support/matchers/have_html_attribute.rb', line 40

def error
  @error
end

Instance Method Details

#matches?Boolean

Returns:

  • (Boolean)


86
# File 'lib/brut/spec_support/matchers/have_html_attribute.rb', line 86

def matches? = @error.nil?