Class: Arachni::Browser::ElementLocator
- Defined in:
- lib/arachni/browser/element_locator.rb
Overview
Lazy-loaded, Arachni::Browser element representation.
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
Attributes of the element.
-
#tag_name ⇒ Symbol
Tag name of the element.
Class Method Summary collapse
- .from_html(html) ⇒ Object
- .from_node(node) ⇒ Object
- .from_rpc_data(data) ⇒ ElementLocator
-
.supported_element_attributes_for(tag_name) ⇒ Set<Symbol>
List of attributes supported by Watir.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #dup ⇒ Object
- #hash ⇒ Object
-
#initialize(options = {}) ⇒ ElementLocator
constructor
A new instance of ElementLocator.
-
#locatable_attributes ⇒ Hash
Hash with attributes supported by ‘Watir` when locating elements.
-
#locate(browser) ⇒ Watir::HTMLElement
Locates and returns the element based on #tag_name and #attributes.
- #to_hash ⇒ Hash (also: #to_h)
-
#to_rpc_data ⇒ Hash
Data representing this instance that are suitable the RPC transmission.
-
#to_s ⇒ String
Locator as an HTML opening tag.
Constructor Details
#initialize(options = {}) ⇒ ElementLocator
Returns a new instance of ElementLocator.
27 28 29 30 |
# File 'lib/arachni/browser/element_locator.rb', line 27 def initialize( = {} ) .each { |k, v| send( "#{k}=", v ) } @attributes ||= {} end |
Instance Attribute Details
#attributes ⇒ Hash
Returns Attributes of the element.
23 24 25 |
# File 'lib/arachni/browser/element_locator.rb', line 23 def attributes @attributes end |
#tag_name ⇒ Symbol
Returns 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
111 112 113 |
# File 'lib/arachni/browser/element_locator.rb', line 111 def self.from_html( html ) from_node Nokogiri::HTML.fragment( html ).children.first end |
.from_node(node) ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/arachni/browser/element_locator.rb', line 115 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
99 100 101 |
# File 'lib/arachni/browser/element_locator.rb', line 99 def self.from_rpc_data( data ) new data end |
.supported_element_attributes_for(tag_name) ⇒ Set<Symbol>
Returns List of attributes supported by Watir.
128 129 130 131 132 |
# File 'lib/arachni/browser/element_locator.rb', line 128 def self.supported_element_attributes_for( tag_name ) @supported_element_attributes_for ||= {} @supported_element_attributes_for[tag_name.to_sym] ||= Set.new( Watir.tag_to_class[tag_name.to_sym].attribute_list ) end |
Instance Method Details
#==(other) ⇒ Object
107 108 109 |
# File 'lib/arachni/browser/element_locator.rb', line 107 def ==( other ) hash == other.hash end |
#dup ⇒ Object
78 79 80 |
# File 'lib/arachni/browser/element_locator.rb', line 78 def dup self.class.new to_h end |
#hash ⇒ Object
103 104 105 |
# File 'lib/arachni/browser/element_locator.rb', line 103 def hash to_hash.hash end |
#locatable_attributes ⇒ Hash
Returns 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.
67 68 69 |
# File 'lib/arachni/browser/element_locator.rb', line 67 def locate( browser ) browser.watir.send( tag_name, locatable_attributes ) end |
#to_hash ⇒ Hash Also known as: to_h
83 84 85 86 87 88 |
# File 'lib/arachni/browser/element_locator.rb', line 83 def to_hash { tag_name: tag_name, attributes: attributes } end |
#to_rpc_data ⇒ Hash
Returns Data representing this instance that are suitable the RPC transmission.
93 94 95 |
# File 'lib/arachni/browser/element_locator.rb', line 93 def to_rpc_data to_h.my_stringify_keys end |
#to_s ⇒ String
Returns Locator as an HTML opening tag.
73 74 75 76 |
# File 'lib/arachni/browser/element_locator.rb', line 73 def to_s "<#{tag_name}#{' ' if attributes.any?}" << attributes.map { |k, v| "#{k}=#{v.inspect}" }.join( ' ' ) << '>' end |