Friday, September 30, 2011

WATIR 2.0


Watir 2.0 got released recently. Here are some features in the newest version:


1. No FireWatir:
There won’t be any new releases of FireWatir due to the fact that JSSH extension, which is used to control Firefox by FireWatir, is not available for Firefox versions 4 and newer.


2. Zero based indexing:
Watir has used one based indexing so far but with 2.0 it is using Zero based indexing. If you are using Watir 1.x and you’d like to access first div element on the page, you’d write code like this:


browser.div(:index => 1)
In Watir 2.0 and newer, you have to write code like this instead:


browser.div(:index => 0)


3. Multiple locators:
All elements support multiple locators for searching. 
e.g. browser.span(:name => "something", :class => "else")


4. Default locator:
In other words – if no other locators are specified, then :index => 0 will be used as a default. for e.g.
browser.span(:class => "something").div(:index => 1).span(:class => "else")  //1.x
browser.span(:class => "something").div.span(:class => "else") //2.0


5. Aliased methods
In Watir 2.0 you can use #tr, #trs, #td, #tds, #a, #as, #img and #imgs instead of instead of browser.row, browser.td or browser.cell.

Friday, September 23, 2011

Connecting to MySQL database

Here is one way to connect to MySQL database using ruby. This can be easily enhanced further to interact with the db like reading and writing records.


require "mysql"

   begin
     # connect to the MySQL server
     dbh = Mysql.real_connect("localhost", "testuser", "testpass", "test")
     # get server version string and display it
     puts "Server version: " + dbh.get_server_info
   rescue Mysql::Error => e
     puts "Error code: #{e.errno}"
     puts "Error message: #{e.error}"
     puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
   ensure
     # disconnect from server
     dbh.close if dbh
   end
Let me know if you need any specific.