Tuesday, December 28, 2010

Watir (web application testing in ruby)

Hi,

I feel lucky to get a chance to work with Watir (Web Application Testing in Ruby).

Watir use for automating web testing , you can easily open browser and do all things as you do yourself with website like type,click,submit etc...

Its a very easy to use if you already know ruby. Yeah its not only for ruby people
its also available for .Net and java, For .Net its Watin and for java its Watij.

Installation:

- For windows :
gem install watir

- For linux :

1. sudo gem install firewatir

2. Install JSSH extension for firefox.

For more Details on watir Installation go to http://watir.com/installation/


I Do not want to give you whole explanation because its already given with its doc.

Here I Just want to show simple example which helpful you to start with watir.


Example For IE:

require 'rubygems'
require 'watir'
require 'watir/ie'

ie = Watir::IE.new
puts "open google.com\n"
ie.goto('google.com')
ie.text_field(:name,"q").set "hello world"
ie.button(:name,"btnG").click


if ie.text.include? "hello world"
puts " Yeah hello world Found"
else
puts " Sorry No Result Found !"
end

ie.close

Example for FireFox :

(need to install jssh extension)

require 'rubygems'
require 'firewatir'

ff = FireWatir::Firefox.new
puts "open google.com\n"
ff.goto('google.com')
ff.text_field(:name,"q").set "hello world"
ff.button(:name,"btnG").click


if ff.text.include? "hello world"
puts " Yeah hello world Found"
else
puts " Sorry No Result Found !"
end

ff.close

Hope this basic thing helpful to you.

For more watir example check http://watir.com/examples/

Thanks,
Priyank Shah

Thursday, November 18, 2010

Digest Authentication

hi,

You all know about basic authentication method use in most places here I share something about digest authentication method.

Below code is for digest authentication ask using pop-up when try to enter site.

Installation:

If you already have installed ruby gem then you have to now install one more gem for
digest authentication gem.

gem install net-http-digest_auth

check http://rubygems.org/gems/net-http-digest_auth for dependency for this gem.

Example code :

require 'rubygems'
require 'uri'
require 'net/http'
require 'net/http/digest_auth'


def authentication_check

#change your site which have digest authentication, this url use just for example.
uri = URI.parse 'http://api.del.icio.us/v1/tags/get'
uri.user = 'username'
uri.password = 'password'

digest_auth = Net::HTTP::DigestAuth.new
auth = digest_auth.auth_header uri, res['www-authenticate'], 'GET'
req = Net::HTTP::Get.new uri.request_uri
req.add_field 'Authorization', auth
res = h.request req
#return res.code # for get http return code
return res.message # for http message

end


Thanks,
Priyank Shah

Tuesday, November 9, 2010

Firewatir Jssh error : Unable to connect to machine : 127.0.0.1 on port 9997

Hello,


I get following error when i work with firewatir for firefox on Centos - 64 bit ,

/usr/local/lib/ruby/gems/1.8/gems/firewatir-1.6.7/lib/firewatir/firefox.rb:158:in `set_defaults': Unable to connect to machine : 127.0.0.1 on port 9997. Make sure that JSSh is properly installed and Firefox is running with '-jssh' option (Watir::Exception::UnableToStartJSShException)
from /usr/local/lib/ruby/gems/1.8/gems/firewatir-1.6.7/lib/firewatir/firefox.rb:50:in `initialize'
from /home/test.rb:39:in `new'
from /home/test.rb:39




firewatir require Jssh extension for working, but even installing Jssh extension with firefox but I continue getting error.

I have done many googling and try to change in firefox.rb as i found some where but my problem not solved.

Not solution but i found suggestion for me and you also :

I don't know in which cases this problem occur but I found that JSSH not support with 64 bit Os. so I then try with centos - 32 bit and my error solved and firefox browser works fine.


So if any one know about how it work with 64 bit they can also post there solutions here.

Thanks,
Priyank Shah

Tuesday, August 17, 2010

how to parse css (css_parser)

How to parse css and get css file elements in Ruby?
You can parse both local css file as well as remote css file also by Using Css_parser gem.

For install simple do "gem install css_parser." or use http://github.com/alexdunae/css_parser for github.

Below is the example of use Remote Css :
require 'rubygems'
require 'css_parser'
include CssParser

parser = CssParser::Parser.new
parser.load_uri!('http://example.com/styles/style.css')

# load a remote file, setting the base_uri and media_types
parser.load_uri!('../style.css', 'http://example.com/styles/inc/', [:screen, :handheld])

# load a local file, setting the base_dir and media_types
parser.load_file!('print.css', '~/styles/', :print)

# lookup a rule by a selector
parser.find('#content')
#=> 'font-size: 13px; line-height: 1.2;'

# lookup a rule by a selector and media type
parser.find('#content', [:screen, :handheld])

# iterate through selectors by media type
parser.each_selector(:screen) do |selector, declarations, specificity|
...
end

# add a block of CSS
css = <<-EOT
body { margin: 0 1em; }
EOT

parser.add_block!(css)

# output all CSS rules in a single stylesheet
parser.to_s
=> #content { font-size: 13px; line-height: 1.2; }
body { margin: 0 1em; }




How to parse local Css??

For that you have to do some change in css_parser gem's file "lib/css_parser/parser.rb"

and add following method into it after "load_uri! method.

# Load a local CSS file.
def load_file!(file_name, base_dir = nil, media_types = :all)
file_name = File.expand_path(file_name, base_dir)
return unless File.readable?(file_name)

src = IO.read(file_name)
base_dir = File.dirname(file_name)

add_block!(src, {:media_types => media_types, :base_dir => base_dir})
end

For more changes and details you have to check :

http://github.com/zapnap/css_parser/blob/master/lib/css_parser/parser.rb


Example For Local css parse:
require 'rubygems'
require 'css_parser'
include CssParser

parser = CssParser::Parser.new
parser.load_file!("test.css")
@info = parser.find_by_selector("body")