Class: Ragios::Plugins::UptimeMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/uptime_monitor/uptime_monitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUptimeMonitor

Returns a new instance of UptimeMonitor.



13
14
15
16
# File 'lib/uptime_monitor/uptime_monitor.rb', line 13

def initialize
  @result_set = []
  @test_result = {results: @result_set}
end

Instance Attribute Details

#browser_infoObject (readonly)

Returns the value of attribute browser_info.



9
10
11
# File 'lib/uptime_monitor/uptime_monitor.rb', line 9

def browser_info
  @browser_info
end

#has_screenshotObject (readonly)

Returns the value of attribute has_screenshot.



8
9
10
# File 'lib/uptime_monitor/uptime_monitor.rb', line 8

def has_screenshot
  @has_screenshot
end

#monitorObject (readonly)

Returns the value of attribute monitor.



4
5
6
# File 'lib/uptime_monitor/uptime_monitor.rb', line 4

def monitor
  @monitor
end

#s_exprObject (readonly)

Returns the value of attribute s_expr.



10
11
12
# File 'lib/uptime_monitor/uptime_monitor.rb', line 10

def s_expr
  @s_expr
end

#screenshot_urlObject (readonly)

Returns the value of attribute screenshot_url.



7
8
9
# File 'lib/uptime_monitor/uptime_monitor.rb', line 7

def screenshot_url
  @screenshot_url
end

#successObject (readonly)

Returns the value of attribute success.



6
7
8
# File 'lib/uptime_monitor/uptime_monitor.rb', line 6

def success
  @success
end

#test_resultObject (readonly)

Returns the value of attribute test_result.



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

def test_result
  @test_result
end

#validationsObject (readonly)

Returns the value of attribute validations.



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

def validations
  @validations
end

Instance Method Details

#close_browserObject



51
52
53
# File 'lib/uptime_monitor/uptime_monitor.rb', line 51

def close_browser
  @browser.close if @browser.respond_to?('close')
end

#exists(page_elements) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/uptime_monitor/uptime_monitor.rb', line 59

def exists(page_elements)
  for i in 0..(page_elements.length - 1)
    if @browser.exists?(page_elements[i])
      result!(@validations[i], true)
    else
      take_screenshot
      result!(@validations[i], false)
    end
  end
end

#init(monitor) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/uptime_monitor/uptime_monitor.rb', line 18

def init(monitor)
  @monitor = OpenStruct.new(monitor)
  message = "A url must be provided for uptime_monitor: #{@monitor.monitor}"
  raise(Hercules::UptimeMonitor::NoUrlProvided.new(error: message), message) if @monitor.url.nil?
  message = "A browser must be provided for uptime_monitor: #{@monitor.monitor}"
  raise(Hercules::UptimeMonitor::NoBrowserProvided.new(error: message), message) if @monitor.browser.nil?
  @browser_info = Hercules::UptimeMonitor::BrowsersLangParser.new.parse(@monitor.browser)
  message = "A validation (exists?) must be provided for uptime_monitor: #{@monitor.monitor}"
  raise(Hercules::UptimeMonitor::NoValidationProvided.new(error: message), message) if @monitor.exists?.nil?
  @s_expr = Hercules::UptimeMonitor::MaestroLangParser.new.parse(@monitor.exists?)
  @validations = Hercules::UptimeMonitor::MaestroLangParser.new.parse(@monitor.exists?, description = true)
  {ok: true}
end

#result!(validation, state) ⇒ Object



70
71
72
73
74
# File 'lib/uptime_monitor/uptime_monitor.rb', line 70

def result!(validation, state)
  @success = false if state == false
  result = state ? [validation, "exists_as_expected"] : [validation, "does_not_exist_as_expected"]
  @result_set << result
end

#start_browser(url, browser_name, headless) ⇒ Object



55
56
57
# File 'lib/uptime_monitor/uptime_monitor.rb', line 55

def start_browser(url, browser_name, headless)
  @browser = Hercules::UptimeMonitor::Browser.new(url, browser_name, headless)
end

#take_screenshotObject



76
77
78
79
80
81
# File 'lib/uptime_monitor/uptime_monitor.rb', line 76

def take_screenshot
  if RAGIOS_HERCULES_ENABLE_SCREENSHOTS && not(@monitor.disable_screenshots) && not(@has_screenshot)
    @screenshot_url = @browser.capture_screenshot
    @has_screenshot = true
  end
end

#test_command?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/uptime_monitor/uptime_monitor.rb', line 32

def test_command?
  @result_set = []
  @success = true
  @has_screenshot = false
  start_browser(@monitor.url, browser_info[:browser], !!browser_info[:headless] )
  exists(@s_expr)
  @test_result = {results: @result_set}
  @test_result[:screenshot] = @screenshot_url if @has_screenshot
  close_browser
  @success
rescue Net::ReadTimeout => e
  close_browser rescue nil
  @test_result = {results: ["Page Load Timeout", "Page could not load after 2 minutes"]}
  return true
rescue Exception => e
  close_browser rescue nil
  raise e
end