Using assert() in a loop


#1

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 = 
"http://localhost/fc_codebase/_web/?urn=com.firstconsult/1/101/1010022"

		link_arr = {
		"Description" => "198"
		}

		href_addr = [
		"%23ah_70002::th_unused",
		"%23ah_70089::th_",
		"#ah_70002::th_unused",
		"%23ah_70002::",
		"#ah_70002::",
		"%23ah_70002::",
		"#ah_70002::",
		"%23ah_70002::t)(*&^%$",
		"#ah_70002::t)(*&^%$",
		"%23ah_70002::%23tyh_888",
		"#ah_70002::#tyh_888",
		"%23ah_70567::th_unsed",
		"%23ah_70567::th_70002",
		"%23ah_70567::th_unused",
		"%23ah_unused::th_unused",
		"#ah_unused::th_unused",
		"#ah_70567::th_",
		"%23ah_70567::th_",
		"%23ah_70567::th_70002&%",
		"#ah_70567::th_70002%%",
		]

		$ie.goto(href_start)

		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)
				$ie.link(:text, key).click
				puts $ie.text_field(:name, "id_href").getContents() + " " + 
href_arr_val

				begin

				   assert(result);

				rescue => e

				    puts "*FAILED*." + e.message
				end

			end
		end

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
toby


#2

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.

begin
assert(result);
rescue => e
add_failure(1)
puts “FAILED.” + e.message
end

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.


#3

Toby wrote:

loop and doesn’t check the rest of the urls.
http://localhost/fc_codebase/_web/?urn=com.firstconsult/1/101/1010022
#ah_70002::”,
#ah_unused::th_unused”,

rescue => e

Any help would be gratfully recieved

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

HTH

Kind regards

robert