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]

Thursday, June 23, 2011

Ruby constants and variables

foobar 
A variable whose name begins with a lowercase letter (a-z) or underscore (_) is a local variable.

@foobar
A variable whose name begins with '@' is an instance variable of self.

@@foobar
A class variable is shared by all instances of a class.

$foobar
A variable whose name begins with '$' has a global scope; meaning it can be accessed from anywhere within the program during runtime.

FOOBAR
A variable whose name begins with an uppercase letter (A-Z) is a constant. A constant can be reassigned a value after its initialization, but doing so will generate a warning,

More detailed information about Ruby constants and variables can be found from here:
http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Variables_and_Constants

Thursday, June 16, 2011

Browser version

How to get browser version e.g.

$browserversion = $ie.document.invoke('parentWindow').navigator.appVersion
            $tmp_str =/MSIE\s(.*?);/.match($browserversion)
            $browserversion = "MSIE " + $tmp_str[1] 
puts "Browser: " + $browserversion

Changing browser settings

 You can modify browser settings by using Windows Registry Editor e.g.
  
system("echo Windows Registry Editor Version 5.00 > useragent.reg") 
system("echo. >> useragent.reg")     
system("echo [HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\5.0\\User Agent] >> useragent.reg")
system("echo \"Compatible\"=\"Testclient\" >> useragent.reg")      system("regedit /s useragent.reg")   
system("del /f useragent.reg")