Automating ‘Auto Complete’ text field using Watir-WebDriver
February 9, 2012
Requirement:
1. Navigate to Google.com
require 'rubygems'
require 'watir-webdriver'
browser=Watir::Browser.new :ie
browser.goto("google.com")
browser.text_field(:name,"q").set("Ravee")
sleep 3
auto_content=browser.table(:class,'gssb_m').trs
auto_content.each do |g|
if g.text.downcase == "raveendran"
g.click
sleep 3
break
end
end
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
watir-webdriver –> Ruby gem
May 30, 2011
Installation:
1. Install Ruby
2. Open Command Prompt >gem update –system
3. >gem install watir-webdriver
Usage:
The same script will work for multiple browsers like IE,FF,Chrome.
Code:
FOR IE :
require 'watir-webdriver'
browser = Watir::Browser.new :ie
browser.goto "http://google.com"
browser.text_field(:name => 'q').set("ruby watir watir-webdriver")
browser.button(:name => 'btnG').click
puts browser.url
browser.close
FOR Firefox,
change the second line --> browser = Watir::Browser.new :firefox
For Chrome,
change the second line --> browser = Watir::Browser.new :chrome
Overview about Watir-webdriver :



