Using assert() in a loop


Hello everybody

I hope this question isn’t answered somewhere else, but the search was
disabled so i hope that excuses me.

I have been introducted to Ruby thorugh the need to write Watir tests to
do functional testing. I have an array of URLs that i wish to check and
a loop to go through them. I use the assert() function to find out
whether a particular link has scrolled to the correct position. If the
test fails (ie assert(false)) ruby breaks out of the loop and doesn’t
check the rest of the urls.

I have added a begin/rescue statement which stops ruby breaking out the
loops but Watir then reports zero failures.

Code looks like this:

def test_href_Exists

		href_start = 

		link_arr = {
		"Description" => "198"

		href_addr = [


		link_arr.each do |key, value|
			href_addr.each do |href_arr_val|

				href_complete = href_start + href_arr_val
				$ie.text_field(:name, "id_href").set(href_complete)
				$, key).click
				puts $ie.text_field(:name, "id_href").getContents() + " " + 



				rescue => e

				    puts "*FAILED*." + e.message


I am fairly sure there is a simple explaination but nothing i have tried
works so far this week.

Any help would be gratfully recieved

thanks loads


As with many of these problems, hours of head banging are solved with a
single line.

i used the begin/rescue code but added a watir function call that
updates the failures. this works perfectly and returns a full list of
everything that has failed back to the ci server.

rescue => e
puts “FAILED.” + e.message

I can see the logic in ‘if one fails the whole thing fails’, i just
think it’s good practice to see exactly what has failed rather than just
the tip of the iceberg. I’d forgotten how frustrating it is to learn a
new language, loving the challenge tho and getting to love ruby.


Toby wrote:

loop and doesn’t check the rest of the urls.

rescue => e

Any help would be gratfully recieved

Well, the test fails as soon as a single URL fails. That’s the reason
the behavior you’re seeing. If you want a single error message that
all failues the way to go is to collect all failures somehow (Array,
and have it fail only if there is at least one error.


Kind regards