Module: SodaFireFox

Defined in:
lib/SodaFireFox.rb

Overview

SodaFireFox – MOdule

This module is for doing firefox things with soda.

Class Method Summary collapse

Class Method Details

.CloseBrowser(watirobj) ⇒ Object

CloseBrowser – function

This function trys to close browsers, using this because of a lame
watir bug.

Input:

watirobj: This is the watir object to use to execute js script.

Output:

returns the result of the jsscript.


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/SodaFireFox.rb', line 92

def SodaFireFox.CloseBrowser(watirobj)
   result = 0
   jssh = <<JS
   var windows = getWindows();
   var len = windows.length -1;
   var closed = 0;

   for(var i = len; i >= len; i--) {
      windows[i].close();
      closed += 1;
   }
   
   closed;
JS

   begin
      result = watirobj.js_eval(jssh)
      result = result.to_i()
   rescue Exception => e
      $curSoda.rep.ReportException(e, true, false)
   ensure

   end

   return result

end

.CreateFireFoxBrowser(options = nil) ⇒ Object

CreateFireFoxBrowser – Function

This function will create a new firewatir object.

Params:

options:  This is the same options that you would pass to a FireWatir.new

Results:

A hash is always returned, if the 'browser' keys value is nil there
was an error and the eaception for that error is returned in the hash.


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/SodaFireFox.rb', line 55

def SodaFireFox.CreateFireFoxBrowser(options = nil)
   result = {
      "browser" => nil,
      "exception" => nil
   }

   Watir::Browser.default = "firefox"

   begin
      if (options != nil)
         result['browser'] = FireWatir::Firefox.new(options)
      else 
         result['browser'] = Watir::Browser.new()
      end

      result['browser'].maximize()
   rescue Exception => e
      result['exception'] = e
      result['browser'] = nil
   ensure
   end

   return result
end

.KillProcessesObject

KillProcesses – function

This function kills all firefox processes.

Input:

None.

Output:

returns -1 on error else 0 on success.


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/SodaFireFox.rb', line 250

def SodaFireFox.KillProcesses()
   os = nil
   err = 0

   os = SodaUtils.GetOsType()
   print "Kill Process OS: #{os}\n"
   case (os)
      when /linux/i
         err = KillProcessUnix()
      when /windows/i
         print "Calling KillProcessWindows().\n"
         err = KillProcessWindows()
   end

   return err

end

.KillProcessUnixObject

KillProcessUnix – function

This function find all running firefox processes and then tries to 
kill them.

Input:

None.

Output:

returns -1 on error else 0 on success.


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/SodaFireFox.rb', line 193

def SodaFireFox.KillProcessUnix()
   firefox = []
   tmp = nil
   result = 0

   tmp = Kernel.open("| ps -e")
   lines = tmp.readlines()
   tmp.close()

   lines.shift()
   lines.each do |l|
      l = l.chomp()
      l = l.gsub(/^\s+/, "")

      if (l =~ /firefox/i)
         print "(*)#{l}\n"
         hash = {
            'pid' => nil,
            'name' => nil
         }

         data = l.split(/\s+/)
         hash['pid'] = data[0].to_i()
         hash['name'] = data[3]
         firefox.push(hash)
      end
   end

   if (firefox.length < 1)
      print "No firefox processes to kill, browser closed clean.\n"
   end

   firefox.each do |hash|
      begin
         print "Killing Process ID: #{hash['pid']}, Name:"+
            "#{hash['name']}\n"
         Process.kill("KILL", hash['pid'])
      rescue Exception => e
         print "(!)Exception: #{e.message}!\n"
         result = -1
      end
   end

   return result
end

.KillProcessWindowsObject

KillProcessWindows – function

This function find all running firefox processes and then tries to 
kill them.

Input:

None.

Output:

returns -1 on error else 0 on success.


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/SodaFireFox.rb', line 132

def SodaFireFox.KillProcessWindows()
   firefox = []
   tmp = nil
   result = 0

   tmp = Kernel.open("| tasklist /NH")
   lines = tmp.readlines()
   tmp.close()

   lines.each do |l|
      l = l.chomp()
      if (l =~ /firefox/i)
         hash = {
            'pid' => nil,
            'name' => nil
         }
         data = l.split(/\s+/)
         hash['name'] = data[0]
         hash['pid'] = data[1].to_i()
         firefox.push(hash)
      end
   end

   len = firefox.length()
   if (firefox.length < 1)
      print "(*)No firefox processes to kill, browser closed clean.\n"
   end

   firefox.each do |hash|
      begin
         res = false
         print "Killing Process ID: #{hash['pid']}, Name:"+
            "#{hash['name']}\n"
         cmd = "taskkill /F /T /PID #{hash['pid']}"
         res = Kernel.system(cmd)

         if (res =! true)
            print "Failed calling command: #{cmd}!\n"
            result = -1
         end
      rescue Exception => e
         print "(!)Exception : #{e.message}\n"
         result = -1
      end
   end

   return result
end