Tuesday, September 21, 2010

Exception handling in Ruby

The Ruby Exception Handling mechanism includes:

1. rescue and final clauses - for handling unwanted errors and
exit gracefully.

2. raise - for deliberately creating and raising exceptions in
code.

3. catch and throw clause - for continuing execution at some
point up the function call stack


I have been mainly using rescue in my code and have managed to handle most of the exceptions.

Example

i=0
while i<=10
  begin
    if i ==0
      1/0
    end
    raise "random exception"
   rescue ZeroDivisionError
    i+=1
  end
end

Rescuing Exceptions Inside Methods

def  foo_method
  puts "Hi"  raise
  puts "Bye"
rescue
  puts "Rescuing exceptions"

Adding support for html elements in WATIR

Any HTML element or tag can be to WATIR framework by updating non_control_elements.rb in watir framework.

1. Go to C:\ruby\lib\ruby\gems\1.8\gems\watir-1.6.5\lib\watir
2. Open non_control_elements.rb and add tags which are not there in the list and you wish to validate

class Ul < NonControlElement
    TAG = 'UL'
  end
  class H1 < NonControlElement
    TAG = 'H1'
  end
  class H2 < NonControlElement
    TAG = 'H2'
  end
class LEGEND < NonControlElement
    TAG = 'LEGEND'
  end
  class TH < NonControlElement
    TAG = 'TH'
  end

This can be then used to validate any text/links/listboxes etc on the html page.