class Arachni::Browser::ElementLocator
Lazy-loaded, {Browser} element representation.
@author Tasos “Zapotek” Laskos <tasos.laskos@arachni-scanner.com>
Constants
- ARACHNI_ID
Attributes
attributes[RW]
@return [Hash]
Attributes of the element.
tag_name[RW]
@return [Symbol]
Tag name of the element.
Public Class Methods
from_html( html )
click to toggle source
# File lib/arachni/browser/element_locator.rb, line 139 def self.from_html( html ) from_node Parser.parse_fragment( html ) end
from_node( node )
click to toggle source
# File lib/arachni/browser/element_locator.rb, line 143 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 )
click to toggle source
@param [Hash] data {#to_rpc_data} @return [ElementLocator]
# File lib/arachni/browser/element_locator.rb, line 127 def self.from_rpc_data( data ) new data end
new( options = {} )
click to toggle source
@param [Hash] options
Data used to set attributes via setters.
# File lib/arachni/browser/element_locator.rb, line 29 def initialize( options = {} ) options.each { |k, v| send( "#{k}=", v ) } @attributes ||= {} end
supported_element_attributes_for( tag_name )
click to toggle source
@param [String] tag_name
Opening HTML tag of the element.
@return [Set<Symbol>]
List of attributes supported by Watir.
# File lib/arachni/browser/element_locator.rb, line 156 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
Public Instance Methods
==( other )
click to toggle source
# File lib/arachni/browser/element_locator.rb, line 135 def ==( other ) hash == other.hash end
attributes=( attributes )
click to toggle source
@param [Hash] attributes
Attributes used to locate the element.
@return [Hash]
Frozen and stringified version of the hash.
# File lib/arachni/browser/element_locator.rb, line 46 def attributes=( attributes ) @attributes = (attributes || {}).stringify_recursively_and_freeze end
css()
click to toggle source
# File lib/arachni/browser/element_locator.rb, line 73 def css attrs = {} # If there's an ID attribute that's good enough, don't include anything # else to avoid risking broken selectors due to dynamic attributes and # values. if attributes['id'] attrs['id'] = attributes['id'] # If we have our own attribute trust it more than the rest, # 'class' attributes and others can change dynamically. elsif attributes[ARACHNI_ID] attrs[ARACHNI_ID] = attributes[ARACHNI_ID] # Alternatively, exclude data attributes (except from ours ) to prevent # issues and use whatever other attributes are available. else attrs = attributes.reject do |k, v| k.to_s.start_with?( 'data-' ) end end "#{tag_name}#{attrs.map { |k, v| "[#{k}=\"#{v.escape_double_quote}\"]"}.join}" end
dup()
click to toggle source
# File lib/arachni/browser/element_locator.rb, line 106 def dup self.class.new to_h end
hash()
click to toggle source
# File lib/arachni/browser/element_locator.rb, line 131 def hash to_hash.hash end
locatable_attributes()
click to toggle source
@return [Hash]
Hash with attributes supported by `Watir` when locating elements.
# File lib/arachni/browser/element_locator.rb, line 52 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 )
click to toggle source
@return [Selenium::WebDriver::Element]
Locates and returns the element based on {#css}.
# File lib/arachni/browser/element_locator.rb, line 69 def locate( browser ) browser.selenium.find_element( :css, css ) end
tag_name=( name )
click to toggle source
@param [String, Symbol] name
@return [Symbol]
# File lib/arachni/browser/element_locator.rb, line 37 def tag_name=( name ) @tag_name = name.to_sym end
to_hash()
click to toggle source
@return [Hash]
# File lib/arachni/browser/element_locator.rb, line 111 def to_hash { tag_name: tag_name, attributes: attributes } end
Also aliased as: to_h
to_rpc_data()
click to toggle source
@return [Hash]
Data representing this instance that are suitable the RPC transmission.
# File lib/arachni/browser/element_locator.rb, line 121 def to_rpc_data to_h.my_stringify_keys end
to_s()
click to toggle source
@return [String]
Locator as an HTML opening tag.
# File lib/arachni/browser/element_locator.rb, line 100 def to_s "<#{tag_name}#{' ' if attributes.any?}" << attributes.map { |k, v| "#{k}=\"#{v.escape_double_quote}\"" }.join( ' ' ) << '>' end
Also aliased as: inspect