Class: SodaField

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

Overview

SodaField – Class

Class Method Summary collapse

Class Method Details

.alertHack(alert = nil, modify = true) ⇒ Object

alertHack – Method

This method auto answers java alerts & confirms.

Input: alert: true or false, to cancel or ok dialog.

Output: always retutns true.



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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/fields/SodaField.rb', line 196

def self.alertHack(alert = nil, modify = true)
   if (alert == nil) 
      return true
   end

   begin
      if (modify)
         if (Watir::Browser.default == 'firefox')
            alertConfirm = "var old_alert = browser.contentWindow.alert;"
            alertConfirm += "var old_confirm = browser.contentWindow."+
               "confirm;"
            alertConfirm += "browser.contentWindow.alert = function()"+
               "{return #{alert};};"
            alertConfirm += "browser.contentWindow.confirm = function()"+
               "{return #{alert};};"
            if (alert)
               alertConfirm += "browser.contentWindow.onbeforeunload = null;"
            end
            $jssh_socket.send(alertConfirm + "\n", 0)
            $curSoda.browser.read_socket();
         end

         if (Watir::Browser.default == 'ie')
            alertConfirm = "var old_alert = window.alert;"
            alertConfirm += "var old_confirm = window.confirm;"
            alertConfirm += "window.alert = function(){return #{alert};};"
            alertConfirm += "window.confirm = function(){return #{alert};};"
            if (alert)
               alertConfirm += "window.onbeforeunload = null;"
            end
            $curSoda.browser.document.parentWindow.eval(alertConfirm + "\n")
         end
      else
         if (Watir::Browser.default == 'firefox')
            alertConfirm = "browser.contentWindow.alert = old_alert;"
            alertConfirm += "browser.contentWindow.confirm = old_confirm;"
            if (alert)
               alertConfirm += "browser.contentWindow.onbeforeunload = null;"
            end
            $jssh_socket.send(alertConfirm + "\n", 0)
            $curSoda.browser.read_socket();
         end

         if (Watir::Browser.default == 'ie')
            alertConfirm = "var old_alert = window.alert;"
            alertConfirm += "var old_confirm = window.confirm;"
            alertConfirm += "window.alert = old_alert;"
            alertConfirm += "window.confirm = old_confirm;"
            if (alert)
               alertConfirm += "window.onbeforeunload = null;"
            end
            $curSoda.browser.document.parentWindow.eval(alertConfirm + "\n")
         end
      end
   rescue Exception => e
      $curSoda.rep.ReportException(e, true)
   ensure
   end
end

.append(field, value) ⇒ Object

append – Method:

This method appends a value to the existing value for a watir text
field.  Checks that the field is enabled before appending.

Params:

field: this is the watir object for the field.
value: this is the vale to append to the field.

Results:

returns -1 on error, or 0 on success.


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/fields/SodaField.rb', line 159

def self.append(field, value)
   result = 0
   msg = "Appending to Element: "

   begin
      tmp = FieldUtils.WatirFieldToStr(field, $curSoda.rep)
      tmp = "Unknown" if (tmp == nil)
      $curSoda.rep.log("#{msg}#{tmp}: Value => '#{value}'.\n")

      if (!field.enabled?)
         $curSoda.rep.ReportFailure(
            "Error: Trying to set a value for a disabled Element!\n")
            result = -1
       else
         field.append(value)
         result = 0
       end
   rescue Exception => e
      $curSoda.rep.ReportException(e, true)
      result = -1
   ensure
   end

   $curSoda.rep.log("Append finished.\n")

   return result 
end

.assert(field, value) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/fields/SodaField.rb', line 42

def self.assert(field, value)
   comp = (!field.value.empty?)? field.value: field.text
   
   $curSoda.rep.log("Field Value: #{field.value}\n")

   if (value.kind_of?(Regexp))
      return value.match(comp)
   end
   return value == comp
end

.clear(field) ⇒ Object



328
329
330
# File 'lib/fields/SodaField.rb', line 328

def self.clear(field)
   return field.clear
end

.click(field, sugarwait = false) ⇒ Object

click – Method

This method fires a watir element object's click method.

Params:

field: This is the watir object to click.
sugarwait: true/false, will calling SodaUtils.WaitSugarAjaxDone if true.

Results:

Always returns 0


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/fields/SodaField.rb', line 268

def self.click(field, sugarwait = false)
   result = 0
   msg = "Clicking element: "
   retry_count = 10
   i = 0

   begin
      self.focus(field)
   rescue Exception => e
      if (Watir::Browser.default !~ /ie/i)
         $curSoda.rep.ReportException(e, true)
      end
   ensure
   end

   for i in 0..retry_count
      result = 0

      begin
         tmp = FieldUtils.WatirFieldToStr(field, $curSoda.rep)
         tmp = "Unknown" if (tmp == nil) 

         $curSoda.rep.log("#{msg}#{tmp}.\n")
         field.click()
         $curSoda.browser.wait()

         if (sugarwait)
            SodaUtils.WaitSugarAjaxDone($curSoda.browser, $curSoda.rep)
         end
      rescue Exception => e
         result = -1

         if (e.message !~ /missing ; before statement/i)
            $curSoda.rep.ReportException(e, true)
            break
         else
            sleep(1)
         end
      ensure
      end

      break if (result == 0)
      $curSoda.rep.log("Retrying: #{i}\n", SodaUtils::WARN)
   end

   $curSoda.rep.log("Click finished.\n")

   return result 
end

.disabled(field) ⇒ Object



356
357
358
# File 'lib/fields/SodaField.rb', line 356

def self.disabled(field)
   return field.disabled()
end

.enabled(field) ⇒ Object



349
350
351
# File 'lib/fields/SodaField.rb', line 349

def self.enabled(field)
   return field.enabled?()
end

.focus(field) ⇒ Object



321
322
323
# File 'lib/fields/SodaField.rb', line 321

def self.focus(field)
   return field.focus
end

.getStringTrue(value) ⇒ Object

return true or false based on a string value



363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/fields/SodaField.rb', line 363

def self.getStringTrue(value)
   if value.is_a?(String)
      value.downcase!
      
      if value == 'true' or value == 'yes' or value == '1'
         return true
      else
         return false
      end 
   end

   return value    
end

.getText(field) ⇒ Object



342
343
344
# File 'lib/fields/SodaField.rb', line 342

def self.getText(field)
   return field.text()
end

.getValue(field) ⇒ Object



335
336
337
# File 'lib/fields/SodaField.rb', line 335

def self.getValue(field)
   return field.value
end

.jsevent(field, jsevent, wait = true) ⇒ Object

jsevent - Method

THis method fires a javascript event.

Params:

field: The field to fire the event on.
jsevent: The event to fire: onmoseover...

Results:

None.


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fields/SodaField.rb', line 65

def self.jsevent(field, jsevent, wait = true)
   info = nil
   elm_type = nil
   msg = "Firing JavaScript Event: '#{jsevent}' for Element: "

   begin
      tmp = FieldUtils.WatirFieldToStr(field, $curSoda.rep)
      tmp = "Unknown" if (tmp == nil)
      $curSoda.rep.log("#{msg}#{tmp}.\n")

      if (Watir::Browser.default !~ /ie/i)
         self.focus(field)
      end

      if (Watir::Browser.default =~ /firefox/i)
         field.fire_event("#{jsevent}", wait)
      elsif (Watir::Browser.default =~ /ie/i)
         field.fire_event("#{jsevent}")
      end

   rescue Exception => e
      $curSoda.rep.ReportException(e, true)
   ensure
   end

   $curSoda.rep.log("JavaScript Event finished.\n") 
end

.set(field, value, nonzippy = false) ⇒ Object

set – Method

This method sets the value for the field.  Checks to make sure that the
field is enabled before trying to set.

Params:

field: this is the watir object for the field.
value: The vale to set the field to.
nonzippy: this turns on zippy text entry.

Results:

returns 0 on success, or -1 on error


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/fields/SodaField.rb', line 114

def self.set(field, value, nonzippy = false)
   result = 0
   msg = "Setting Element: "

   begin
      tmp = FieldUtils.WatirFieldToStr(field, $curSoda.rep)
      tmp = "Unknown" if (tmp == nil)
      tmp_value = "#{value}"
      tmp_value = tmp_value.gsub("\n", '\n')
      $curSoda.rep.log("#{msg}#{tmp}: Value => '#{tmp_value}'.\n")

      if (!field.enabled?)
         $curSoda.rep.ReportFailure(
            "Error: Trying to set a value for a disabled Element!\n")
         result = -1
      else
         if (nonzippy)
            field.set(value)
         else
            field.value = value
         end
         result = 0
      end
   rescue Exception => e
      $curSoda.rep.ReportException(e, true)
      result = -1
   ensure
   end

   return result 
end

.uploadFile(fieldname, file) ⇒ Object



96
97
98
# File 'lib/fields/SodaField.rb', line 96

def self.uploadFile(fieldname, file)
   $curSoda.file_field(:value, fieldname).set(file)
end