List Info

Thread: Basic question re looping




Basic question re looping
country flaguser name
United States
2007-04-12 05:58:53
Hi,

I am trying to write a simple script (very new to Ruby) to
gain an understanding of looping. My script is basically
going into google, typing something into the search engine,
and pressing enter. I want the script to check for a link,
if it's not there press next page, search for a link, and
basically carry on until the link is found and then press
it.
I've had lots of help on here already, and now have a script
below.
When I play this script, it loads the first page of the
search engine, then ends saying test completed successfully.
(So basically, no Next pressed, and no link found, but no
errors reported).

Can someone please have a look and tell me where I'm going
wrong.
(I know the next link will only work to go to the second
page, but not sure how to just define the Next link as
looking for the button instead of the actual url).

TIA!!

require 'watir'
include Watir
require 'test/unit'
class TC_recorded < Test::Unit::TestCase
  
	def test_recorded
		ie = IE.new
    ie.goto("http://www.google.co.uk
")
    ie.bring_to_front
    
    ie.text_field(:name, 'q').set("barry
manilow")
    ie.button(:name, 'btnG').click
  end      
   
def click_links ie
unless ie.link(:url, "http:/
/www.classicbands.com/manilow.html").exists? 
ie.link(:url, 'http://www.google.co.uk/search?q=b
arry+manilow&hl=en&start=10&sa=N').click
click_links ie
else
ie.link(:url, "http:/
/www.classicbands.com/manilow.html").click
end
end
end
_______________________________________________
Wtr-general mailing list
Wtr-generalrubyforge.org
htt
p://rubyforge.org/mailman/listinfo/wtr-general

Re: Basic question re looping
country flaguser name
United States
2007-04-12 07:14:57
Give this a shot

require 'watir'
include Watir

# Lets setup our data so the code is easier to read
url 			= "http://www.google.co.uk
"
search_string 	= "barry manilow"
target_url		= "http:/
/www.classicbands.com/manilow.html"

# Get IE ready to go
ie = IE.new

# Goto the website
ie.goto(url)

# Set the text in the search box
ie.text_field(:name, 'q').set(search_string)

# Click the search button
ie.button(:name, 'btnG').click

# Lets setup the control to our loop
found = false

# "While NOT found" (ie, keep searching until it
IS found)
while !found
	# If the link exists, do the following
	if ie.link(:url, target_url).exists?
		ie.link(:url, target_url).click
		found = true
	# If not keep searching
	else
		ie.div(:id, 'nn').click
	end
end

# The end
_______________________________________________
Wtr-general mailing list
Wtr-generalrubyforge.org
htt
p://rubyforge.org/mailman/listinfo/wtr-general

Re: Basic question re looping
country flaguser name
United States
2007-04-12 08:04:26
Thanks so much, you're brilliant!!!
_______________________________________________
Wtr-general mailing list
Wtr-generalrubyforge.org
htt
p://rubyforge.org/mailman/listinfo/wtr-general

Re: Basic question re looping
country flaguser name
United States
2007-04-12 08:04:49
Nicola Kennedy wrote:
> require 'watir'
> include Watir
> require 'test/unit'
> class TC_recorded < Test::Unit::TestCase
>   
> 	def test_recorded
> 		ie = IE.new
>     ie.goto("http://www.google.co.uk
")
>     ie.bring_to_front
>     
>     ie.text_field(:name, 'q').set("barry
manilow")
>     ie.button(:name, 'btnG').click
>   end      
>    
> def click_links ie
> unless ie.link(:url, "http:/
/www.classicbands.com/manilow.html").exists? 
> ie.link(:url, 'http://www.google.co.uk/search?q=b
arry+manilow&hl=en&start=10&sa=N').click
> click_links ie
> else
> ie.link(:url, "http:/
/www.classicbands.com/manilow.html").click
> end
> end
> end
>   
Your problem is that your test method never calls your
click_link 
method. You need to add a call to it, presumably as the last
line of 
test_recorded.

You also have run into a little-known limitation of Watir.
It does not 
support Barry Manilow. And I'm not sure it ever will.
Sorry.

Bret
_______________________________________________
Wtr-general mailing list
Wtr-generalrubyforge.org
htt
p://rubyforge.org/mailman/listinfo/wtr-general

Re: Basic question re looping
country flaguser name
United States
2007-04-12 08:13:47
Inspired by the suggestion given by Bret on a different
thread 

#-----------------------------------------------
require 'watir'
include Watir

# Lets setup our data so the code is easier to read
url 			= "http://www.google.co.uk
"
search_string 	= "barry manilow"
target_url		= "http:/
/www.classicbands.com/manilow.html"

def click_links(ie,target_url) 
  unless ie.link(:url, target_url).exists?
    ie.div(:id, 'nn').click
    click_links(ie,target_url) 
  else
    ie.link(:url, target_url).click
   end
end

# Get IE ready to go
ie = IE.new

# Goto the website
ie.goto(url)

# Set the text in the search box
ie.text_field(:name, 'q').set(search_string)

# Click the search button
ie.button(:name, 'btnG').click

click_links(ie,target_url) 

#The end
#-----------------------------------------------
_______________________________________________
Wtr-general mailing list
Wtr-generalrubyforge.org
htt
p://rubyforge.org/mailman/listinfo/wtr-general

[1-5]

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