List Info

Thread: Good 'wrapper' sample




Good 'wrapper' sample
country flaguser name
United States
2007-04-13 09:16:57
I had some time to kill this morning so I decided to toss
together another 'sample'. I know when I start learning
something new there is nothing better then working examples
that I can modify and see how they break, so here you go.

This is a 'wrapper' for the Google website. I wrote it with
the intention of show a number of different things about
ruby. The goal is that you should be able to take the
'Google' class and quickly generate new scripts from it.

Run it, update it, modify it and have some fun 

------------

require 'watir'
include Watir

# A wrapper for our global ie object
# contains some special logic for creating a new IE if one
doesnt' exist
def browser
	if $ie == nil or !$ie.exists?
		$ie = IE.new
	else
		$ie
	end
end

# I found that the search box and the search button are the
same on the home page
# and the results page, so I made a module to hold them
both
module Generic_Search_Functions
	def search_box( search_string )
		browser.text_field(:name, 'q').set( search_string )
	end
	
	def google_search()
		browser.button(:name, 'btnG').click
	end
end

# This class handles issues dealing with the results page
class Results
	# this includes the functions in the module
	include Generic_Search_Functions
	
	# Since the buttons are named differently I wrapped it
	# this is just calling the function in the above module
	def search()
		google_search()
	end
	
	# click the next button, handle it not being there
	def next()
		element = browser.div(:id, 'nn')
		if element.exists?
			element.click
		else
			puts 'End of the list'
		end
	end
	
	# click the previous button, handle it not being there
	def previous()
		element = browser.div(:id, 'np')
		if element.exists?
			element.click
		else
			puts 'Start of the list'
		end		
	end
	
	# this just outputs the href for the search results
	# if it can't find it, it lets you know
	def search_result(index)
		element =
browser.div(:index,'2').div(:index,'1').link(:index,index)
		
		if element.exists?
			puts element.href
		else
			puts 'Unable to find element'
		end
	end
end

# here is our main class, google
# it contains both the module and the results class
class Google
	# heres that module again!
	include Generic_Search_Functions
	
	# lets define our url in here
	# lets also create a results object so we have access to
it
	def initialize()
		url = 'http://www.google.com'
		results = Results.new()
	end
	
	# ooooo, magic!
	attr_accessor :results
	
	# this navigates us to the page and checks to make sure the
logo is there
	def go()
		browser.goto(url)
		wait_until{ browser.image(:src, /logo.gif/).exists? }
	end

	# wrapper for the I'm feeling Lucky botton
	def im_feeling_lucky()
		browser.button(:name, 'btnI').click
	end
end

# create our new google object
google = Google.new()

# go to the website
google.go

# Input some text into the search box (this function is from
the module)
google.search_box('Cats')

# click search (this is from the module also)
google.google_search()

# show us the href of the first link
google.results.search_result(1)

# next page please!
google.results.next()

# show us the href of first link on this page
google.results.search_result(1)

# lets go page, i liked those results better
google.results.previous()

# How about the second link, maybe its better
google.results.search_result(2)

# change the search!
google.results.search_box('Dogs')

# click!
google.results.search()

# maybe this is what i really wanted!
google.results.search_result(1)
_______________________________________________
Wtr-general mailing list
Wtr-generalrubyforge.org
htt
p://rubyforge.org/mailman/listinfo/wtr-general

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )