Installation : 

1. Install Ruby 1.8.7 or 1.9.2

2. Install WatirWebdriver — Refer  http://rubygems.org/gems/watir-webdriver

a. CMD>gem install watir-webdriver

3. Install Prawn Library

a. CMD>gem install prawn

Sample Code:

require 'rubygems'
require 'watir-webdriver'
require 'prawn'

ff=Watir::Browser.new :ff

ff.goto("http://raveendran.wordpress.com")

Prawn::Document.generate('c:\\test123\\hello.pdf') do |pdf|
pdf.text(ff.text)
end

ff.close

Output:

hello PDF  file contains all the TEXT from Webpage

 

Open Issues:

Alignment issue occurs depends upon web page Design. But You wont miss to collect all the Text from Webpage.

Installation : 

1. Install Ruby 1.8.7 or 1.9.2

2. Install WatirWebdriver — Refer  http://rubygems.org/gems/watir-webdriver

a. CMD>gem install watir-webdriver

3. Install Google Charts Library — Refer http://googlecharts.rubyforge.org/

Sample Code:

require 'rubygems'
require 'watir-webdriver'
require 'gchart'

ff=Watir::Browser.new :ff

first=Gchart.bar(:data => [300, 100, 30, 200])
ff.goto(first)
ff.driver.save_screenshot 'c:\\test123\\first.jpg'

second=Gchart.pie_3d(:title => 'Open Source Tools', :size =>
'400x200',:data => [45, 35, 20], :labels => ["Watir", "Selenium", "Others"] )
ff.goto(second)
ff.driver.save_screenshot 'c:\\test123\\second.jpg'
three=Gchart.line(:data => [300, 100, 30, 200, 100, 200, 300, 10],
:axis_with_labels => 'x,r',:axis_labels => [['Jan','July','Jan','July','Jan'], ['2005','2006','2007']])
ff.goto(three)
ff.driver.save_screenshot 'c:\\test123\\three.jpg'

ff.close

Output:

1. Usually Google Charts gives  http urls as a output. Using Watir WebDriver we can open the URL and navigate to the mentioned page and we can save that web page as a Image.

2.  Run the code –> The out folder contains the images

RAutomation – Rubygem

March 7, 2011

RAutomation is a small and easy to use library for helping out to automate windows and their controls for automated testing. RAutomation provides: * Easy to use and user-friendly API (inspired by Watir http://www.watir.com) * Cross-platform compatibility * Easy extensibility – with small scripting effort it’s possible to add support for not yet supported platforms or technologies

Installation:

>gem install rautomation
>gem install ffi

Sample Code:

require “rautomation”
window = RAutomation::Window.new(:title => /part of the title/i)
window.exists? # => true
window.title # => “blah blah part Of the title blah”
window.text # => “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ultricies…”
window.text_field(:class => “Edit”, :index => 0).set “hello, world!”
button = window.button(:text => “&Save”)
button.exists? # => true
button.click
all_windows = RAutomation::Window.windows
all_windows.each {|window| puts window.hwnd}
window = RAutomation::Window.new(:title => /part of the title/i)
windows = window.windows
puts windows.size # => 2
windows.map {|window| window.title } # => ["part of the title 1", "part of the title 2"]
window.windows(:title => /part of other title/i) # => all windows with matching specified title
window.buttons.each {|button| puts button.value}
window.buttons(:value => /some value/i).each {|button| puts button.value}
window2 = RAutomation::Window.new(:title => “Other Title”, :adapter => :autoit) # use AutoIt adapter
# use adapter’s (in this case AutoIt’s) internal methods not part of the public API directly
window2.WinClose(“[TITLE:Other Title]“)

Way 1:

Feature File:

Scenario: Google
Given I am in Google Search page
When the user clicks on the next button
When clicks next
When selects Next
Then the title should be “Raveendran – Watir”

In your ruby file,

Given /^I am in Google Search page$/ do
puts “I am in google page”
end

When /^(?:the? )?(?:user? )?(?:clicks|selects)?(?: on? )?(?:the?)?(?: next?)?(?: button?)?$/i do
puts “The same script working for 3 step files :)
end

Then /^the title should be (.*)$/ do |arg1|
#Code for verification
puts “It works”
end

Solution :

Only 1 step is enough Instead of 3 steps if it targets same script.

Way 2:

Feature File:

Scenario: Google
Given I have verified the result page
When nothing changed
Then the title should be “Raveendran – Watir”

In Your Ruby file,

Given /^I have verified the result page$/ do
Given “I am in Google Search page”
And “the user clicks on the next button”
And “clicks next”
And “selects Next”
And “the title should be “Raveendran – Watir”"
end

When /^nothing changed$/ do
#Some Code
end

Then /^the title should be “([^\"]*)”$/ do |arg1|
#Some Code
end

Code:

def rando
letter=(“a”..”z”).to_a
return @name=
letter[rand(letter.length)]+”#{rand(9)}”+letter[rand(letter.length)]+
letter[rand(letter.length)]+letter[rand(letter.length)]+”#{rand(9)}”+letter[rand(letter.length)]+
letter[rand(letter.length)]+”#{rand(9)}”+letter[rand(letter.length)]+letter[rand(letter.length)]+
letter[rand(letter.length)]+letter[rand(letter.length)]+”#{rand(9)}”
end

dir = “../setup/videos”
$files = Dir.entries(dir)
puts “Before rename”
puts $files
$files.each do |f|
next if f == “.” or f == “..”
oldFile = dir + “\\” + f
newFile = dir + “\\#{rando;@name}” + “.”+f.split(“.”)[1]
File.rename(oldFile, newFile)
@file_path=File.expand_path(newFile)
@file_path=@file_path.gsub(“/”,”\\”)
end
puts “After Renamed”
$files = Dir.entries(dir)
puts $files

OUTPUT:

Before rename
.
..
four.flv
one.flv
three.flv
two.flv
After Renamed
.
..
f5vdc7zw3vplz8.flv
h5yyy5xj1jmbg8.flv
m6roa8sp5lqbq1.flv
q4rtq5ui0zdvx0.flv

Random names in Ruby

August 1, 2009

Code:

def rando
letter=(“a”..”z”).to_a
return @name=
letter[rand(letter.length)]+”#{rand(9)}”+letter[rand(letter.length)]+
letter[rand(letter.length)]+letter[rand(letter.length)]+”#          {rand(9)}”+letter[rand(letter.length)]+
letter[rand(letter.length)]+”#    {rand(9)}”+letter[rand(letter.length)]+letter[rand(letter.length)]+
letter[rand(letter.length)]+letter[rand(letter.length)]+”#{rand(9)}”
end

rando
puts @name

Output:

o1epp7yn0clbk4

Examples

Basic usage:

require "highline/import"

  ask("Company?  ") { |q| q.default = "none" }

Validation:

  ask("Age?  ", Integer) { |q| q.in = 0..105 }
  ask("Name?  (last, first)  ")
 { |q| q.validate = /\A\w+, ?\w+\Z/ }

Type conversion for answers:

  ask("Birthday?  ", Date)
  ask("Interests?  (comma sep list)  ",
lambda { |str| str.split(/,\s*/) })

Reading passwords:

  ask("Enter your password:  ") { |q| q.echo = false }
  ask("Enter your password:  ") { |q| q.echo = "x" }

ERb based output (with HighLine‘s ANSI color tools):

  say("This should be <%= color('bold', BOLD) %>!")

Menus:

  choose do |menu|
    menu.prompt = "Please choose your favorite
programming language?  "

    menu.choice(:ruby) { say("Good choice!") }
    menu.choices(:python, :perl)
{ say("Not from around here, are you?") }
  end

Reference --> http://highline.rubyforge.org/doc/

Accessing Excel in Ruby

CODE:

require ‘win32ole’
excel = WIN32OLE.new(‘excel.application’)
excel.visible = true
work=excel.workbooks.open(“C:\\Documents and Settings\\raveendran\\Desktop\\output.xls”)
excel.range(“A1″).value = “name”
excel.range(“B1″).value = “address”
excel.range(“C1″).value = “city “
excel.range(“D1″).value = “state “
excel.range(“E1″).value = “zip”
work.save

OUPUT:

Excel

Ruby – Big Decimal

June 29, 2009

Code:

require ‘bigdecimal’
x = BigDecimal(“123.6″) – BigDecimal(“123″)
puts x.to_f
puts  f=123.6 – 123

require ‘bigdecimal’

x = BigDecimal(“123.6″) – BigDecimal(“123″)

puts x.to_f

puts  f=123.6 – 123

OUTPUT:

0.6

0.599999999999994

Ruby - convert number to english word

CODE :

class Fixnum
 def english_word
  @h = {0=>"zero", 1=>"One", 2=>"Two", 3=>"Three",
 4=>"Four", 5=>"Five",6=>"six", 7=>"seven", 8=>"Eight",
9=>"Nine",10=>"Ten",11=>"Eleven",12=>"Twelve",
13=>"Thirteen",14=>"Fourteen",15=>"Fifteen",
16=>"Sixteen",17=>"Seventeen",18=>"Eighteen",
19=>"Nineteen",20=>"Twenty",30=>"Thirty",
40=>"Fourty",50=>"Fifty",60=>"Sixty",70=>"Seventy",
80=>"Eighty",90=>"Ninty"}
  @i=0
  @array=[]
  @result=""a
  if self > 99
    str_num=self.to_s #@num.to_s
    str_num_len=str_num.length
    str_full_num=str_num.insert(0,"0"*(11-str_num_len))
    str_full_num=str_num.insert(8,"0")
    str_full_num.scan(/../) { |x|  @array<<x }
    6.times do
    self.def_calc
    @i+=1
    end
  else
     if self > 9
        puts
(self.proc_double_dig((self/10)*10))+
(self.proc_single_dig(self%10))
     else
       if self > 0
       puts self.proc_single_dig(self)
       else
        return "AMOUNT NOT KNOWN or NILL"
       end
     end
  end
  end

  def def_calc
    case @i
      when 0
        str=self.proc_unit(@array[@i])
        if (str.scan(/\w+/)).length!=0
             then str=str+ "hundred & "
              @result=@result+str
        end
      when 1
        str=self.proc_unit(@array[@i])
        if (str.scan(/\w+/)).length!=0
             then str=str+ " Crore, "
             @result=@result+str
        end
      when 2
        str=self.proc_unit(@array[@i])
        if (str.scan(/\w+/)).length!=0
             then str=str+ " Lakh, "
             @result=@result+str
        end
      when 3
        str=self.proc_unit(@array[@i])
        if (str.scan(/\w+/)).length!=0
             then str=str+ " Thousand, "
             @result=@result+str
        end
      when 4
        str=self.proc_unit(@array[@i])
        if (str.scan(/\w+/)).length!=0
             then str=str+ " Hundred, "
             @result=@result+str
        end
      when 5
        str=self.proc_unit(@array[@i])
        if (str.scan(/\w+/)).length!=0
             then str=str+ ". "
             @result=@result+str
        end
        print @result.sub(/..$/,"")
    else
   end
  end

  def proc_unit(x)
    if x.to_i>0
      if x.to_i<=10
        return self.proc_single_dig(x.to_i)
      else
        if x.to_i<=20
        return self.proc_double_dig(x.to_i)
        else
        return
(self.proc_double_dig((x.to_i/10)*10))+
(self.proc_single_dig(x.to_i%10))
        end
     end
    end
  return ""
  end

  def proc_double_dig(z)
    if z==0
      return ""
    else
      return @h[z]
    end
 end

  def proc_single_dig(y)
    if y==0
      return ""
    else
      return @h[y]
    end
  end
protected :def_calc, :proc_unit, :proc_double_dig,
  :proc_single_dig

end

puts 453645445.english_word

#FourtyFive Crore, Thirtysix Lakh, FourtyFive Thousand,
Four Hundred,FourtyFive

Note:
The above code works only up to billions.
Follow

Get every new post delivered to your Inbox.

Join 529 other followers