Module: SeleniumStatistics

Extended by:
SeleniumStatistics
Included in:
SeleniumStatistics
Defined in:
lib/selenium_statistics.rb,
lib/selenium_statistics/logger.rb,
lib/selenium_statistics/statistics.rb

Overview

Defined Under Namespace

Classes: Logger

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(command, time = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/selenium_statistics/statistics.rb', line 12

def method_missing(command, time = nil)
  command = command.to_s
  command.gsub!('get_element_location_once_scrolled_into_view', 'get_element_location')
  command.gsub!('get_element_value_of_css_property', 'get_element_css_value')

  return @commands[command] unless command =~ /=$/

  command = command.chomp('=')

  @commands[command] ||= {}

  @commands[command][:time] ||= 0
  @commands[command][:count] ||= 0

  @commands[command][:time] += time
  @commands[command][:count] += 1
  @commands[command][:average] =  @commands[command][:time] / @commands[command][:count]

  @executions += 1
  @time += time

  SeleniumStatistics.logger.info "Executed #{command} in #{time} sec"
  SeleniumStatistics.logger.debug "#{command} executed total of #{@commands[command][:count]} times in #{ @commands[command][:time]} seconds"
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



5
6
7
# File 'lib/selenium_statistics/statistics.rb', line 5

def commands
  @commands
end

#executionsObject (readonly)

Returns the value of attribute executions.



5
6
7
# File 'lib/selenium_statistics/statistics.rb', line 5

def executions
  @executions
end

#timeObject (readonly)

Returns the value of attribute time.



5
6
7
# File 'lib/selenium_statistics/statistics.rb', line 5

def time
  @time
end

Class Method Details

.loggerObject



11
12
13
# File 'lib/selenium_statistics.rb', line 11

def self.logger
  @logger ||= SeleniumStatistics::Logger.new
end

Instance Method Details



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/selenium_statistics/statistics.rb', line 45

def print_results(sort=nil)
  str = "Executed a total of #{executions} commands in #{time.round(1)} seconds\n\n"

  results(sort).each do |k,v|
    str << "#{k.to_s}:".ljust(27, ' ')
    str << "executions: #{v[:count].to_s.rjust(executions.size, ' ')}; "
    str << "total seconds: #{v[:time].round(1).to_s.rjust(time.round(1).to_s.size, ' ')}; "
    str << "avg sec/cmd: #{v[:average].round(3).to_s.rjust(5, ' ')}; "
    str << "total: #{(100*v[:average_total]).round(2).to_s.rjust(5, ' ')}%\n"
  end

  SeleniumStatistics.logger.warn str
end

#reset!Object



59
60
61
62
63
64
# File 'lib/selenium_statistics/statistics.rb', line 59

def reset!
  @commands = {}
  @time = 0
  @executions = 0
  @test_start = Time.now
end

#results(sort = nil) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/selenium_statistics/statistics.rb', line 37

def results(sort=nil)
  sort ||= :count
  @test_time = Time.now - @test_start

  @commands.each { |_k, v| v[:average_total] = v[:time]/@test_time}
  @commands.sort_by { |_k, v| v[sort] }.reverse.to_h
end