Class: Arachni::Page::DOM::Transition

Inherits:
Object
  • Object
show all
Defined in:
lib/arachni/page/dom/transition.rb

Overview

Author:

Defined Under Namespace

Classes: Error

Constant Summary collapse

NON_PLAYABLE =

Non-playable events.

Set.new([:request])
ZERO_DEPTH =

Events without a DOM depth.

Set.new([:request])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Transition

Note:

If arguments are provided they will be passed to #start.

Returns a new instance of Transition.

Parameters:

  • element (Browser::ElementLocator)
  • event (Symbol)
  • options (Hash)

    Extra options to associate with this transition.

  • block (Block)

    If a ‘block` has been given it will be executed and the transition will automatically be marked as finished.

Raises:



102
103
104
105
106
107
# File 'lib/arachni/page/dom/transition.rb', line 102

def initialize( *args, &block )
    @options = {}

    return if !args.any?
    start( *args, &block )
end

Instance Attribute Details

#elementBrowser::ElementLocator (readonly)

Returns HTML element which received the #event.

Returns:



78
79
80
# File 'lib/arachni/page/dom/transition.rb', line 78

def element
  @element
end

#eventSymbol

Returns Event triggered on #element.

Returns:

  • (Symbol)

    Event triggered on #element.



82
83
84
# File 'lib/arachni/page/dom/transition.rb', line 82

def event
  @event
end

#optionsHash (readonly)

Returns Extra options.

Returns:

  • (Hash)

    Extra options.



86
87
88
# File 'lib/arachni/page/dom/transition.rb', line 86

def options
  @options
end

#timeFloat

Returns Time it took to trigger the given #event on the #element.

Returns:

  • (Float)

    Time it took to trigger the given #event on the #element.



90
91
92
# File 'lib/arachni/page/dom/transition.rb', line 90

def time
  @time
end

Class Method Details

.from_rpc_data(data) ⇒ Transition

Parameters:

Returns:



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/arachni/page/dom/transition.rb', line 263

def self.from_rpc_data( data )
    instance = allocate
    data.each do |name, value|

        value = case name
                    when 'event'
                        value.to_sym

                    when 'element'
                        if value.is_a? String
                            value.to_sym
                        else
                            Browser::ElementLocator.from_rpc_data( value )
                        end

                    when 'options'
                        value.my_symbolize_keys

                    else
                        value
                end

        instance.instance_variable_set( "@#{name}", value )
    end
    instance
end

Instance Method Details

#==(other) ⇒ Object



294
295
296
# File 'lib/arachni/page/dom/transition.rb', line 294

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

#completeTransition

Note:

Will stop the timer for #time.

Marks the transition as finished.

Returns:

Raises:



166
167
168
169
170
171
172
173
174
# File 'lib/arachni/page/dom/transition.rb', line 166

def complete
    fail Error::Completed, 'Transition has completed.'   if completed?
    fail Error::NotRunning, 'Transition is not running.' if !running?

    @time  = Time.now - @clock
    @clock = nil

    self
end

#completed?Bool

Returns ‘true` if the transition has completed, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the transition has completed, `false` otherwise.

See Also:



219
220
221
# File 'lib/arachni/page/dom/transition.rb', line 219

def completed?
    !!@time
end

#depthInteger

Returns Depth for this transition.

Returns:

  • (Integer)

    Depth for this transition.

See Also:



180
181
182
# File 'lib/arachni/page/dom/transition.rb', line 180

def depth
    ZERO_DEPTH.include?( event ) ? 0 : 1
end

#dupObject



237
238
239
# File 'lib/arachni/page/dom/transition.rb', line 237

def dup
    deep_clone
end

#hashObject



290
291
292
# File 'lib/arachni/page/dom/transition.rb', line 290

def hash
    to_hash.tap { |h| h.delete :time }.hash
end

#play(browser) ⇒ Transition?

Returns New transition as a result of the play, ‘nil` if the play wasn’t successful.

Parameters:

  • browser (Browser)

    Browser to use to play the transition.

Returns:

  • (Transition, nil)

    New transition as a result of the play, ‘nil` if the play wasn’t successful.

Raises:



193
194
195
196
197
198
199
200
201
# File 'lib/arachni/page/dom/transition.rb', line 193

def play( browser )
    fail Error::NotPlayable, "Transition is not playable: #{self}" if !playable?

    if element == :page && event == :load
        return browser.goto options[:url], cookies: options[:cookies]
    end

    browser.fire_event element, event, options
end

#playable?Bool

Returns ‘true` if the transition is for an event that can be played, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the transition is for an event that can be played, `false` otherwise.

See Also:



228
229
230
# File 'lib/arachni/page/dom/transition.rb', line 228

def playable?
    !NON_PLAYABLE.include?( event )
end

#running?Bool

Returns ‘true` if the transition is in progress, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the transition is in progress, `false` otherwise.

See Also:



209
210
211
# File 'lib/arachni/page/dom/transition.rb', line 209

def running?
    !!@clock
end

#start(element, event, options = {}, &block) ⇒ Transition

Note:

Will start the timer for #time.

Returns ‘self`.

Parameters:

  • element (Browser::ElementLocator)
  • event (Symbol)
  • options (Hash) (defaults to: {})

    Extra options to associate with this transition.

  • block (Block)

    If a ‘block` has been given it will be executed and the transition will automatically be marked as finished.

Returns:

Raises:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/arachni/page/dom/transition.rb', line 135

def start( element, event, options = {}, &block )
    fail Error::Completed, 'Transition has completed.'   if completed?
    fail Error::Running, 'Transition is already running' if running?

    if ![Symbol, String, Browser::ElementLocator].include?( element.class )
        fail Error::InvalidElement
    end

    self.event = event
    @element   = element

    @options = options.my_symbolize_keys(false)
    @clock   = Time.now

    return self if !block_given?

    block.call
    complete
end

#to_hashHash Also known as: to_h

Returns:



242
243
244
245
246
247
248
249
250
# File 'lib/arachni/page/dom/transition.rb', line 242

def to_hash
    {
        element: element.is_a?( Browser::ElementLocator ) ?
                     element.to_h : element,
        event:   event,
        options: options,
        time:    time
    }
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.



255
256
257
258
259
# File 'lib/arachni/page/dom/transition.rb', line 255

def to_rpc_data
    h = to_hash.my_stringify_keys(false)
    h['element'] = element.to_rpc_data_or_self
    h
end

#to_sString

Returns:



233
234
235
# File 'lib/arachni/page/dom/transition.rb', line 233

def to_s
    "[#{time.to_f}s] '#{event}' on: #{element}"
end