Class: Hercules::UptimeMonitor::Browser

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

Instance Method Summary collapse

Constructor Details

#initialize(url, browser_name = "firefox", is_headless = false) ⇒ Browser

Returns a new instance of Browser.



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

def initialize(url, browser_name = "firefox", is_headless = false)
  start_headless if is_headless
  goto(url,browser_name)
end

Instance Method Details

#apply_action?(element, action) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/uptime_monitor/browser.rb', line 94

def apply_action?(element, action)
  if action.is_a?(Symbol) || action.is_a?(String)
    element.send(action.to_sym)
  elsif action.is_a? Hash
    key, value = action.first
    element.send(key, value)
  else
    message = "Cannot apply action: #{action.inspect}"
    raise(Hercules::UptimeMonitor::InvalidAction.new(error: message), message)
  end
  true
end

#apply_rest?(element, array) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
# File 'lib/uptime_monitor/browser.rb', line 49

def apply_rest?(element, array)
  if is_a_text?(array)
    apply_text?(element.text, array.first)
  elsif is_an_action?(array)
    apply_action?(element, array.first)
  else
    message = "Invalid options: #{array.inspect}"
    raise(Hercules::UptimeMonitor::InvalidRest.new(error: message), message)
  end
end

#apply_text?(assert_eq_text, text_hash) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
# File 'lib/uptime_monitor/browser.rb', line 84

def apply_text?(assert_eq_text, text_hash)
  if text_hash[:text]
    text_hash[:text] == assert_eq_text
  elsif text_hash[:includes_text]
    assert_eq_text.include? text_hash[:includes_text]
  else
    message = "Could not evaluate page_element text: #{text_hash.inspect}"
    raise(Hercules::UptimeMonitor::InvalidText.new(error: message), message)
  end
end

#apply_wait_until?(page_element) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
# File 'lib/uptime_monitor/browser.rb', line 106

def apply_wait_until?(page_element)
  Watir::Wait.until { page_element_exists?(page_element) }
  true
rescue Watir::Wait::TimeoutError
  false
end

#capture_screenshotObject



112
113
114
115
116
117
118
# File 'lib/uptime_monitor/browser.rb', line 112

def capture_screenshot
  filename = "#{RAGIOS_HERCULES_SCREENSHOT_DIR}/screenshot#{Time.now.to_i}.png"
  @browser.screenshot.save(filename)
  file = File.open(filename)
  uploader = Hercules::UptimeMonitor::ScreenShotUploader.new
  uploader.store(file)
end

#closeObject



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

def close
  @browser.close
  @headless.destroy if @headless
end

#exists?(page_element) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



15
16
17
18
19
# File 'lib/uptime_monitor/browser.rb', line 15

def exists?(page_element)
  message = "Invalid page element format: #{page_element.inspect}"
  raise(Hercules::UptimeMonitor::InvalidPageElementForm.new(error: message), message) unless page_element.is_a?(Array)
  is_wait_until?(page_element.first) ? apply_wait_until?(page_element.first[:wait_until_exists?]) : page_element_exists?(page_element)
end

#get_element(first) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/uptime_monitor/browser.rb', line 34

def get_element(first)
  if (first.is_a? Symbol) || (first.is_a? String)
    @browser.send(first.to_sym)
  elsif first.is_a? Hash
    key, value = first.first
    @browser.send(key, value)
  else
    message = "Invalid page element #{first.inspect}"
    raise(Hercules::UptimeMonitor::InvalidPageElement.new(error: message), message)
  end
end

#is_a_text?(text_array) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_a_text?(text_array)
  if text_array.is_a? Array
    if text_array.first.is_a? Hash
      text_hash = text_array.first
      key, value = text_hash.first
      if (([:text, :includes_text].include?(key)) && (value.is_a? String))
       return true
      end
    end
  end
  return false
end

#is_an_action?(array) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
# File 'lib/uptime_monitor/browser.rb', line 71

def is_an_action?(array)
  if array.is_a? Array
    if array.first.is_a?(Symbol) || array.first.is_a?(Hash) || array.first.is_a?(String)
      return true
    end
  end
  return false
end

#is_wait_until?(hash) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/uptime_monitor/browser.rb', line 79

def is_wait_until?(hash)
  return false unless hash.is_a? Hash
  key, value = hash.first
  key == :wait_until_exists?
end

#page_element_exists?(page_element) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/uptime_monitor/browser.rb', line 20

def page_element_exists?(page_element)
  element = get_element(page_element.first)
  begin
    first_exists = element.exists?
  rescue Exception => e
    message = "Cannot find page element in this form: #{page_element.first.inspect}, you may use a css selector form"
    raise(Hercules::UptimeMonitor::UnknownPageElement.new(error: message), message)
  end
  if first_exists && !rest(page_element).empty?
    rest_exists?(element, rest(page_element))
  else
    first_exists
  end
end

#rest(page_element) ⇒ Object



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

def rest(page_element)
  page_element[1..-1]
end

#rest_exists?(element, array) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/uptime_monitor/browser.rb', line 45

def rest_exists?(element, array)
 results = array.map { |e| apply_rest?(element, e) }
 results.include?(false) ? false : true
end