Monday, September 19, 2011

Convert Unicode to UTF-8

Here is a simple function for changing \u3232 characters to UTF-8 characters in a given string:

def unicodeToUtf8(str)
    return str.gsub(/\\u([a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9])/) {|p| [$1.to_i(16)].pack("U")}   
end

Tuesday, September 6, 2011

Timeout ...

IE has some problems with e.g. javascripts, where it is loading a page forever without ever timing out. To prevent your test runs from hanging, e.g. create a function for opening URLs:

def openURL url
   begin
      Timeout::timeout(60) do
         $ie.goto url
      end
   rescue Timeout::Error
      puts "Timeout occured! Continuing ..."
   end 
end

Wait for page redirect

If you need to wait for a page redirect to happen, here is an example how to do so:

begin
   Watir::Waiter.wait_until(20) { $ie.text.include? "Redirected" }
   rescue Watir::Exception::TimeOutException
   begin
       raise "Redirect didn't happen!"
   end
end

This will wait for maximum 20 seconds for the text 'Redirected' to appear. If it doesn't appear within the timeframe, TimeOutException is raised.

URL parsing

Here is an example on how to easily parse an URL.

e.g. http://www.someurl.com?a=1&b=2&c=3

$baseurl = $ie.url.split("?")[0]
$aparam = $ie.url.split("a=")[1].split("&")[0]
$bparam = $ie.url.split("b=")[1].split("&")[0]
$cparam = $ie.url.split("c=")[1]