Thursday, December 8, 2011

Watir-WebDriver


WebDriver is a common browser automation tool that uses what ever is the most appropriate mechanism to control a browser, but with a common API. WebDriver supports not only real browsers (IE, Chrome & Firefox) but also headless ones (using HtmlUnit). It’s basically a nice Watir (ruby) implementation on WebDriver, so it gives you four browsers (three real, one headless) using one neat API, out of the box.

Running Watir-WebDriver

There are essentially two components you need: the Watir-WebDriver ruby gem, and the remote WebDriver server. The remote WebDriver server is only needed if you want to run your tests in headless mode without a real browser (or want to use Opera).

The Watir-WebDriver ruby gem

It’s a simple matter of opening a command prompt and typing:

gem install watir-webdriver (windows)

The remote WebDriver Server

This is the slightly tricky part. This is so that WebDriver can run headless without a real browser, and isn’t needed for real browser support (bar Opera). The quickest easiest way to get up and running is to download this java jar file, open a command prompt where you have saved it, and run:

java -jar selenium-server-standalone-2.0b1.jar

Example:

require 'rubygems'
require 'watir-webdriver'
b = Watir::Browser.new :chrome
b.goto 'www.google.com'
b.text_field(:name => 'q').set 'watir'
b.button(:name => 'btnK').click
b.div(:id => 'center_col').wait_until_present
puts "Displaying page: '#{b.title}' with results: '#{b.div(:id => "center_col").text}'"
b.close

The only difference for Firefox:

b = Watir::Browser.new :firefox

The only difference for IE:

b = Watir::Browser.new :ie

Watir 2.0.4


Watir 2.0.4 has been released. This version has the following changes:

* IE#execute_script escapes multi-line JavaScript scripts
* allow css and xpath locators for element collection methods, fixes
* Zero based Indexing

Install it with:
gem install watir

Tuesday, November 15, 2011

SSL error with Ruby 1.9

For some sites, whenever you try to open/load a https url it gives a certificate verification failed error.Below is the error detail


C:/Ruby192/lib/ruby/1.9.1/net/http.rb:678:in `connect': SSL_connect returned=1 e
rrno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL
::SSL::SSLError)

Ruby 1.9 installation doesn’t find the certification authority certificates (CA Certs) used to verify the authenticity of secured web servers.Basically ruby can't find any root certificates to trust and hence it throws this error.



The solution is to install the certificate and tell your http object to use it in below 2 steps:

   1. sudo port install curl-ca-bundle

   2. https.ca_file = '/opt/local/share/curl/curl-ca-bundle.crt'


or add the following, just next to http.use_ssl:

http.verify_mode = OpenSSL::SSL::VERIFY_NONE

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.

Tuesday, August 30, 2011

Failed to create WIN32OLE object from AutoItX3.Control


This is a  problem of registering the dll file, and hence it create an object of AutoIt. To register the autoit dll from command prompt, use following command


1.regsvr32 C:\ruby192\lib\ruby\gems\1.9.1\gems\watir-1.8.1\lib\watir\AutoItX3.dll


Although it does call the register code before creating an object, the 
error was in creating an autoit object.


2.You can also try adding line to top of the file


require 'win32ole'


Some more drag drop methods

With reference to my earlier posts on drag and drop, here are few more methods. You will need to have WindowsInput module,posted in earlier posts, along with below.

def drag_drop_on(target, src_offset=0, dst_offset=0)

     assert_target target
     drop_x = target.left_edge_absolute + dst_offset
     drop_y = target.top_edge_absolute + dst_offset
      drag_to(drop_x, drop_y, src_offset)
end

def drag_drop(target, src_offset=1, dst_offset=1)
            drag_x = left_edge_absolute + src_offset
    drag_y = top_edge_absolute + src_offset
           drop_x = target.left_edge_absolute + dst_offset
           drop_y = target.top_edge_absolute + dst_offset
           WindowsInput.move_mouse(drag_x, drag_y)
           WindowsInput.left_down
           WindowsInput.move_mouse(drop_x, drop_y)
          WindowsInput.left_up
end

def drag_drop_distance(distance_x, distance_y, src_offset=0, dst_offset=0)
   drag_x, drag_y = source_x_y(src_offset)
   drop_x = drag_x + distance_x + dst_offset
   drop_y = drag_y + distance_y + dst_offset
   drag_to(drop_x, drop_y, src_offset)
end

def drag_drop_at(drop_x, drop_y, src_offset=0)
   drag_to(drop_x, drop_y, src_offset)
end

def drag_drop_below(target, src_offset=0, dst_offset=0)
   assert_target target
   drop_x = target.left_edge_absolute + dst_offset
   drop_y = target.bottom_edge_absolute + 2 + dst_offset
   drag_to(drop_x, drop_y, src_offset)
end

def drag_drop_above(target, src_offset=0, dst_offset=0)
   assert_target target
   drop_x = target.left_edge_absolute + dst_offset
   drop_y = target.top_edge_absolute - 2 + dst_offset
   drag_to(drop_x, drop_y, src_offset)
end

def drag_to(drop_x, drop_y, src_offset)
   drag_x, drag_y = source_x_y(src_offset)
   WindowsInput.move_mouse(drag_x, drag_y)
   WindowsInput.left_down
   WindowsInput.move_mouse(drop_x, drop_y)
   WindowsInput.left_up
end

Thursday, August 25, 2011

Working with PDF files


In our project, we needed to read check the contents of a 'PDF report'  that comes embedded in a 'IE' window.The process is a little complicated and not so straightforward.


First you will need to download pdftk. Download these files and extract the files only in the C:\windows\system32 folder. http://www.accesspdf.com/article.php/20041130153545577 


Secondly you will need to download and isntall xpdf : http://pdf-toolkit.rubyforge.org/ . Extract those files into the C:\windows\system32 folder.Then you will need the PDF::TOOLKIT gem. This can be found here http://rubyforge.org/projects/pdf-toolkit/ 


Basically this will convert the pdf to a textfile and you can do what 
you like with it. In the following example I have just read a file on
my c:\ and displayed it using the 'puts' command.



require 'rubygems'
require 'pdf/toolkit' 

my_pdf = PDF::Toolkit.open("c:\\file.pdf")
text = my_pdf.to_text.read
puts text



I hope this helps