class Watir::PerformanceHelper

Adds helper for window.performance to Watir::Browser. @see dev.w3.org/2006/webapi/WebTiming/

Public Class Methods

new(data) click to toggle source
# File lib/watir-performance.rb, line 7
def initialize(data)
  @data = data
  @hash = {}
end

Public Instance Methods

data_to_hash() click to toggle source
# File lib/watir-performance.rb, line 16
def data_to_hash
  @data.each_key do |key|
    next if key == '__fxdriver_unwrapped'
    @hash[key.to_sym] = {}
    next unless @data[key].respond_to? :each
    @data[key].each do |k, v|
      next if k == '__fxdriver_unwrapped'
      @hash[key.to_sym][underscored(k).to_sym] = v
    end
  end
  @hash
end
munge() click to toggle source
# File lib/watir-performance.rb, line 12
def munge
  OpenStruct.new(summarized_hash(data_to_hash))
end
summarized_hash(hash) click to toggle source
# File lib/watir-performance.rb, line 29
def summarized_hash(hash)
  hash[:summary] = {}
  timing = hash[:timing]
  hash[:summary][:redirect] = redirect_timer(timing)
  hash[:summary][:app_cache] = app_cache(timing)
  hash[:summary][:dns] = hash[:timing][:domain_lookup_end] -
    hash[:timing][:domain_lookup_start] if hash[:timing][:domain_lookup_start] > 0
  hash[:summary][:tcp_connection] = hash[:timing][:connect_end] -
    hash[:timing][:connect_start] if hash[:timing][:connect_start] > 0
  hash[:summary][:tcp_connection_secure] = hash[:timing][:connect_end] -
    hash[:timing][:secure_connection_start] if
      ((hash[:timing][:secure_connection_start] != nil) and
       (hash[:timing][:secure_connection_start] > 0))
  hash[:summary][:request] = hash[:timing][:response_start] -
    hash[:timing][:request_start] if hash[:timing][:request_start] > 0
  hash[:summary][:response] = hash[:timing][:response_end] -
    hash[:timing][:response_start] if hash[:timing][:response_start] > 0
  hash[:summary][:dom_processing] = hash[:timing][:dom_content_loaded_event_start] -
    hash[:timing][:dom_loading] if hash[:timing][:dom_loading] > 0
  hash[:summary][:time_to_first_byte] = hash[:timing][:response_start] -
    hash[:timing][:domain_lookup_start] if hash[:timing][:domain_lookup_start] > 0
  hash[:summary][:time_to_last_byte] = hash[:timing][:response_end] -
    hash[:timing][:domain_lookup_start] if hash[:timing][:domain_lookup_start] > 0
  hash[:summary][:response_time] = latest_timestamp(hash) - earliest_timestamp(hash)
  hash
end

Private Instance Methods

app_cache(hash) click to toggle source
# File lib/watir-performance.rb, line 87
def app_cache(hash)
  hash[:domain_lookup_start] - hash[:fetch_start] if hash[:fetch_start] > 0
end
earliest_timestamp(hash) click to toggle source
# File lib/watir-performance.rb, line 91
def earliest_timestamp(hash)
  timers = %i[navigation_start redirect_start redirect_end fetch_start]
  timer_checks(timers, hash)
end
latest_timestamp(hash) click to toggle source
# File lib/watir-performance.rb, line 96
def latest_timestamp(hash)
  timers = %i[load_event_end load_event_start dom_complete
              dom_content_loaded_event_end dom_content_loaded_event_start
              dom_interactive response_end]
  timer_checks(timers, hash)
end
redirect_timer(hash) click to toggle source
# File lib/watir-performance.rb, line 83
def redirect_timer(hash)
  hash[:redirect_end] - hash[:redirect_start] if hash[:redirect_end] > 0
end
timer_checks(timers, hash) click to toggle source
# File lib/watir-performance.rb, line 68
def timer_checks(timers, hash)
  timers.each do |timer|
    return hash[:timing][timer] if hash[:timing][timer] > 0
  end
end
timer_summerize(t_start, t_end, standard, hash) click to toggle source
# File lib/watir-performance.rb, line 74
def timer_summerize(t_start, t_end, standard, hash)
  case standard
  when true
    hash[t_end] - hash[t_start] if hash[t_end] > 0
  else
    hash[t_end] - hash[t_start] if hash[t_start] > 0
  end
end
underscored(camel_cased_word) click to toggle source
# File lib/watir-performance.rb, line 58
def underscored(camel_cased_word)
  word = camel_cased_word.to_s.dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', '_')
  word.downcase!
  word
end