watchr – Agile development tool – Ruby gem
January 25, 2012
watchr - Agile development tool that monitors a directory tree, and triggers a user defined action whenever an observed file is modified. Its most typical use is continuous testing, and as such it is a more flexible alternative to autotest.
Installation:
1. Open Command prompt
2. CMD>gem install watchr
Sample Usage:
code.rb
puts Time.now.to_s
To Run:
in CMD_EXACTPATH>watchr code.rb
Note:
Change the code and save frequently. Whenever you are saving the file, WATCHR will execute and displays output in command prompt.
Demo Available here –> http://bit.ly/j_watchr
Features
watchr is:
- Simple to use
- Highly flexible
- Evented ( Listens for filesystem events with native c libs )
- Portable ( Linux, *BSD, OSX, Solaris, Windows )
- Fast ( Immediately reacts to file changes )
Most importantly it allows running tests in an environment that is agnostic to:
- Web frameworks ( rails, merb, sinatra, camping, invisible, … )
- Test frameworks ( test/unit, minitest, rspec, test/spec, expectations, … )
- Ruby interpreters ( ruby1.8, ruby1.9, MRI, JRuby, Rubinius, … )
- Package frameworks ( rubygems, rip, … )
For more details –> https://github.com/mynyml/watchr/blob/master/README.md
Ruby – Some Useful Links
January 24, 2012
Ruby:
Homepage : http://www.ruby-lang.org
Downlaod : http://www.ruby-lang.org/en/downloads/
Documentation: http://www.ruby-lang.org/en/documentation/
Libraries: http://www.ruby-lang.org/en/libraries/
Community: http://www.ruby-lang.org/en/community/
News: http://www.ruby-lang.org/en/news/
Security: http://www.ruby-lang.org/en/security/
About Ruby: http://www.ruby-lang.org/en/about/
Documentation:
Getting Started –>
http://pine.fm/LearnToProgram/
http://www.ruby-lang.org/en/documentation/quickstart/
http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/
http://www.techotopia.com/index.php/Ruby_Essentials
http://www.meshplex.org/wiki/Ruby/Ruby_on_Rails_programming_tutorials
Manual –>
http://www.ruby-doc.org/docs/ProgrammingRuby/
http://www.rubyist.net/~slagell/ruby/
http://en.wikibooks.org/wiki/Ruby_programming_language
Reference Documentation –>
http://www.ruby-doc.org/stdlib/
Further details –>
books –>
http://www.ruby-doc.org/bookstore
http://antoniocangiano.com/ruby-and-rails-recommended-books/
http://www.rubycentral.com/book/
http://www.freeprogrammingresources.com/ruby-tutorial.html
http://www.programmingbooks.org/Ruby
http://pspxworld.com/book/programming/ruby.php
http://www.sitepoint.com/books/rails1/
http://www.buildingwebapps.com/learning_rails
http://lifehacker.com/software/ruby/free-e+book-teaches-ruby-programming-225976.php
http://kickjava.com/books/ruby.htm
http://www.onlinecomputerbooks.com/free-ruby-books.php
http://www.easywebtech.com/ebooks/Free_Ruby_ebooks_Download-0.html
http://www.scribd.com/doc/1622/Free-Ruby-eBook
http://www.infoq.com/minibooks/ruby
http://www.sapphiresteel.com/The-Little-Book-Of-Ruby
http://www.rorsecurity.info/the-book/
http://www.freebooksclub.net/ruby-ebooks/
http://www.freebookcentre.net/programming-books-download/Whys-(Poignant)-Guide-to-Ruby.html
http://knowfree.net/category/it-ebooks/ruby
http://www.webstuffscan.com/2007/07/13/download-free-ruby-ebooks-pdf/
http://www.webappers.com/2007/10/07/sitepoint-giving-away-ruby-on-rails-complete-free-book/
http://www.freetechbooks.com/ruby-f49.html
http://nealenssle.com/blog/2007/01/05/free-ruby-e-book/
http://www.sapphiresteel.com/The-Book-Of-Ruby-free-in-depth
http://www.asiaing.com/mr.-neighborlys-humble-little-ruby-book-free-ebook.html
http://www.humblelittlerubybook.com/book/
http://2020ok.com/books/37/guide-to-ruby-34437.htm
http://www.bookrags.com/shortguide-ruby-in-the-smoke/
Course –>
Ruby – Big Decimal sample usage
January 24, 2012
Situation:
To subtract Float and Fixnum values
Normally we will write the code like below
puts f=123.6 - 123
Output:
0.599999999999994
Problem:
The expected output is 0.6 but it returns 0.599999999999994
Solution:
require 'bigdecimal'
x = BigDecimal("123.6") - BigDecimal("123")
puts x.to_f
Output:
0.6
Ruby — Encryption and Decryption using OpenSSL
January 23, 2012
Code:
require 'rubygems'
require 'openssl'
$key = "A75435F0B240012A9489000C2952E41F"
class String
def encrypt(key=$key)
e = OpenSSL::Cipher::Cipher.new 'DES-EDE3-CBC'
e.encrypt key
s = e.update self
s << e.final
s = s.unpack('H*')[0].upcase
s
end
def decrypt(key=$key)
e = OpenSSL::Cipher::Cipher.new 'DES-EDE3-CBC'
e.decrypt key
s = self.to_a.pack("H*").unpack("C*").pack("c*")
s = e.update s
s << e.final
end
end
puts "raveendran".encrypt("password")
puts "509067DC2076497134DBF9A7DE5992B2".decrypt("password")
Output
509067DC2076497134DBF9A7DE5992B2
raveendran
Note:
You can chnage the $key vlaue.
You can encrypt the string with password. The password should match when trying to decrypt the encrypted string.
RSpec + Watir WebDriver
January 19, 2012
Installation:
1. Install Ruby
2. CMD>gem install watir-webdriver
3. CMD>gem install rspec
Code:
google_search.rb
require 'rubygems'
require 'watir-webdriver'
class Google
def search(browser,term,result)
if browser.downcase=="ie"
br= :ie
elsif browser.downcase=="ff"
br= :ff
elsif browser.downcase=="chrome"
br= :chrome
else
br= :ie
end
$ie=Watir::Browser.new br
$ie.goto("http://google.com")
$ie.text_field(:name,'q').set(term)
sleep 3
$ie.button(:name,'btnG').click
sleep 3
$result=$ie.text.downcase.include?(result)
$ie.close
end
end
googleSearch_spec.rb
require 'rubygems'
require 'rspec'
require 'google_search'
describe Google, "#Searchresult" do
it "returns the expected result in search result page" do
bowling = Google.new
bowling.search("chrome","Raveendran","ruby")
$result.should eq(true)
end
end
describe Google, "#Searchresult" do
it "returns the expected result in search result page" do
bowling = Google.new
bowling.search("chrome","Raveendran","wordpress")
$result.should eq(true)
end
end
describe Google, "#Searchresult" do
it "returns the expected result in search result page" do
bowling = Google.new
bowling.search("chrome","Watir, Selenium,Cucumber highline","raveendran")
$result.should eq(true)
end
end
describe Google, "#Searchresult" do
it "returns the expected result in search result page" do
bowling = Google.new
bowling.search("chrome","Ruby highline","raveendran")
$result.should eq(true)
end
end
RUN THE RSPEC code:
1. Navigate to the folder where files available
>rspec googleSearch_spec.rb
OUTPUT:
It will launch Chrome browser and will execute the test cases. Finally You will get the output like,
Started ChromeDriver
port=4113
version=14.0.836.0
.Started ChromeDriver
port=4164
version=14.0.836.0
.Started ChromeDriver
port=4218
version=14.0.836.0
.Started ChromeDriver
port=4260
version=14.0.836.0
.
Finished in 77.28 seconds
4 examples, 0 failures
Convert Webpage into PDF files using Ruby gems — Watir-Webdriver + Prawn
December 10, 2011
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.
Chart Creation using Ruby Gems — Google Charts + Watir WebDriver
December 10, 2011
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
Watir WebDriver — Handling New Window
December 1, 2011
Situation:
1. You are in Parent Page.
2. Clicking link “Open” in Parent page.
3. It opens new window
4. You need to do actions there and come back to your parent window
Solution:
To Click the Link
require 'rubygems'
require 'watir-webdriver'
ff=Watir::Browser.new :ff
ff.goto("website.com")
ff.link(:text,'open').click
To handle the New window and performing some actions within that window,
ff.window(:title,/TITLE of the new window/i).use do
ff.send_keys('SampleText')
ff.button(:id,'insert').click
puts ff.title #returns the new window title
end
within a loop The button belongs to Newly opened window.
puts ff.title #returns the parent window title
Watir-webdriver cheatsheet
October 6, 2011
Getting Started Load the Watir Webdriver libraryrequire 'watir-webdriver'Open a browser (Ex: Internet Explorer)driver = Watir::Browser.new :ieGo to a specified URLdriver.goto 'http://www.orbitz.com/'Close the browserdriver.closeAccess an Element Type something in the Text box or text areadriver.text_field(:id,'airOrigin').set("MAA")To Clear the text from text fielddriver.text_field(:id,'airOrigin').clearButton To click the buttondriver.button(:id,'BUTTON_ID'').clickDrop down list To select the value from listdriver.select_list(:id,'airStartTime').select("1 am")To get the selected value from select fielddriver.select_list(:id,'airStartTime').valueCheck box To clik the check boxdriver.checkbox(:id,'airNonStopsPreferred').clickORdriver.checkbox(:id,'airNonStopsPreferred').setTo know the clicked? or not ?driver.checkbox(:id,'airNonStopsPreferred').set?Radio buttondriver.radio(:id,'htlChoice').clickTo verify Flights radio button selected or notdriver.radio(:id,'htlChoice').set?#if it returns TRUE then radio button already selected. To get the title of the webpageputs driver.titleReturn true if the specified text appears on the TAGputs driver.li(:class,'welcomeText').text.include("Welcome to Orbitz")To Click SPAN Elementsdriver.span(:text,'Find Flights').click
Situation :
User want to click the field which is placed within 2 IFrames using selenium webdriver.
Field — Friendship
Source code for “Friendship” — <nobr>Friendship</nobr>
Frame ID’s –” nav” and “JobplaceFrame”
Code:
require ‘rubygems’
require ‘selenium-webdriver’
$browser = Selenium::WebDriver.for :ie
$browser.get "http://URL.com”
current_title=$browser.title
if current_title == “TITLE OF THE WEBPAGE”
puts “step 1 passed”
else
puts “step1 failed”
exit
end
$browser.switch_to.frame(‘nav’) #parent Frame
$browser.switch_to.frame(“JobplaceFrame”) #Child frame
options=$browser.find_elements(:tag_name=>”nobr”)
options.each do |nobr_field|
if nobr_field.text == 'Friendship'
nobr_field.click
break
end
end


