Class: Arachni::Browser::ElementLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/arachni/browser/element_locator.rb

Overview

Lazy-loaded, Arachni::Browser element representation.

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ElementLocator

Returns a new instance of ElementLocator.

Parameters:

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

    Data used to set attributes via setters.



27
28
29
30
# File 'lib/arachni/browser/element_locator.rb', line 27

def initialize( options = {} )
    options.each { |k, v| send( "#{k}=", v ) }
    @attributes ||= {}
end

Instance Attribute Details

#attributesHash

Returns Attributes of the element.

Returns:

  • (Hash)

    Attributes of the element.



23
24
25
# File 'lib/arachni/browser/element_locator.rb', line 23

def attributes
  @attributes
end

#tag_nameSymbol

Returns Tag name of the element.

Returns:

  • (Symbol)

    Tag name of the element.



19
20
21
# File 'lib/arachni/browser/element_locator.rb', line 19

def tag_name
  @tag_name
end

Class Method Details

.from_html(html) ⇒ Object



116
117
118
# File 'lib/arachni/browser/element_locator.rb', line 116

def self.from_html( html )
    from_node Nokogiri::HTML.fragment( html ).children.first
end

.from_node(node) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/arachni/browser/element_locator.rb', line 120

def self.from_node( node )
    attributes = node.attributes.inject({}) do |h, (k, v)|
        h[k.to_s] = v.to_s
        h
    end

    new tag_name: node.name, attributes: attributes
end

.from_rpc_data(data) ⇒ ElementLocator

Parameters:

Returns:



104
105
106
# File 'lib/arachni/browser/element_locator.rb', line 104

def self.from_rpc_data( data )
    new data
end

.supported_element_attributes_for(tag_name) ⇒ Set<Symbol>

Returns List of attributes supported by Watir.

Parameters:

  • tag_name (String)

    Opening HTML tag of the element.

Returns:

  • (Set<Symbol>)

    List of attributes supported by Watir.



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/arachni/browser/element_locator.rb', line 133

def self.supported_element_attributes_for( tag_name )
    @supported_element_attributes_for ||= {}

    tag_name = tag_name.to_sym

    if (klass = Watir.tag_to_class[tag_name])
        @supported_element_attributes_for[tag_name] ||=
            Set.new( klass.attribute_list )
    else
        @supported_element_attributes_for[tag_name] ||= Set.new
    end
end

Instance Method Details

#==(other) ⇒ Object



112
113
114
# File 'lib/arachni/browser/element_locator.rb', line 112

def ==( other )
    hash == other.hash
end

#cssObject



71
72
73
# File 'lib/arachni/browser/element_locator.rb', line 71

def css
    "#{tag_name}#{attributes.map { |k, v| "[#{k}=#{v.inspect}]"}.join}"
end

#dupObject



83
84
85
# File 'lib/arachni/browser/element_locator.rb', line 83

def dup
    self.class.new to_h
end

#hashObject



108
109
110
# File 'lib/arachni/browser/element_locator.rb', line 108

def hash
    to_hash.hash
end

#locatable_attributesHash

Returns Hash with attributes supported by ‘Watir` when locating elements.

Returns:

  • (Hash)

    Hash with attributes supported by ‘Watir` when locating elements.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/arachni/browser/element_locator.rb', line 50

def locatable_attributes
    attributes.inject({}) do |h, (k, v)|
        string_key = k.to_s
        attribute  = string_key.gsub( '-' ,'_' ).to_sym

        if !self.class.supported_element_attributes_for( tag_name ).include?( attribute ) &&
            !string_key.start_with?( 'data-' )
            next h
        end

        h[attribute] = v.to_s
        h
    end
end

#locate(browser) ⇒ Watir::HTMLElement

Returns Locates and returns the element based on #tag_name and #attributes.

Returns:



67
68
69
# File 'lib/arachni/browser/element_locator.rb', line 67

def locate( browser )
    browser.watir.element( css: css )
end

#to_hashHash Also known as: to_h

Returns:



88
89
90
91
92
93
# File 'lib/arachni/browser/element_locator.rb', line 88

def to_hash
    {
        tag_name:   tag_name,
        attributes: attributes
    }
end

#to_rpc_dataHash

Returns Data representing this instance that are suitable the RPC transmission.

Returns:

  • (Hash)

    Data representing this instance that are suitable the RPC transmission.



98
99
100
# File 'lib/arachni/browser/element_locator.rb', line 98

def to_rpc_data
    to_h.my_stringify_keys
end

#to_sString Also known as: inspect

Returns Locator as an HTML opening tag.

Returns:

  • (String)

    Locator as an HTML opening tag.



77
78
79
80
# File 'lib/arachni/browser/element_locator.rb', line 77

def to_s
    "<#{tag_name}#{' ' if attributes.any?}" <<
        attributes.map { |k, v| "#{k}=#{v.inspect}" }.join( ' ' ) << '>'
end