Watir and Ruby; validate list of expected links

R

reed.adam

All,

The most important part of the site I need to test is content. I need
to make sure that specific links exist, and direct to the correct pages
(this is all dynamic content set in a form by a user).

Being new to Watir and having done the scripting 101 test and viewing
examples, one method to test would be the following. Of course, it's
just looking for text not links, but the general purpose is the same:

if ie.contains_text("Home")
puts "Test Passed. Found the test string: 'Home'."
else
puts "Test Failed! Could not find: 'Home'"
end
if ie.contains_text("This Link")
puts "Test Passed. Found the test string: 'This Link'."
else
puts "Test Failed! Could not find: 'This Link'"
end
etc,etc,etc,etc,etc!!!!!

That's 5 lines per link -- there has to be a better way! I researched
the assert commands and such, but there isn't much documentation on how
to use them.

How would a more experienced Watir/Ruby programmer address this issue?
I will be verifying links like this, specific images, body text. . .
believe it or not, the automated functions like logging in and moving
around seem much easier to me!

Thanks in advance,
Adam
 
U

uncutstone

(e-mail address removed) wrote:
All,

The most important part of the site I need to test is content. I need
to make sure that specific links exist, and direct to the correct pages
(this is all dynamic content set in a form by a user).

Being new to Watir and having done the scripting 101 test and viewing
examples, one method to test would be the following. Of course, it's
just looking for text not links, but the general purpose is the same:

if ie.contains_text("Home")
puts "Test Passed. Found the test string: 'Home'."
else
puts "Test Failed! Could not find: 'Home'"
end
if ie.contains_text("This Link")
puts "Test Passed. Found the test string: 'This Link'."
else
puts "Test Failed! Could not find: 'This Link'"
end
etc,etc,etc,etc,etc!!!!!

That's 5 lines per link -- there has to be a better way! I researched
the assert commands and such, but there isn't much documentation on how
to use them.

If what you want is to shorten your code, you can do it this way:

def ie.assert_contains(sth)
if self.contains_text(sth)
puts "Test Passed. Found the test string: #{sth}."
else
puts "Test Failed! Could not find: #{sth}"
end
end
ie.assert_contains('home')
ie.assert_contains('this link')

Best regards.
 
R

reed.adam

So, where you put "sth", what exactly does that mean? The rest makes
sense to me -- I was trying to find a way that a variable/string could
be used like that instead of repeating the code. Thank you very much!

Last question, where did you learn to do that? I could not find use of
ie.assert in the documentation that came with Watir.


More replies are always welcome, I'd love to get a few perspectives.
 
S

senthil.nayagam

Adam,

I am a great fan of Watir, but I do not use it for all the work, as it
becomes slower.

For my company's new site, which is on rails, I had to do link
validations and also analysis of links, keywords, page size etc.

I used Ruby WWW::Mechanize, to build a custom bot to identify and crawl
all pages, and collect statistics

Results: my site has 120+ pages and 7000+ internal links, 50+ external
links, this includes content from our CMS.

also it found few broken links, which we were able to fix.


webserver logs, website tracking tools are useful for analysis

I use Watir for small testing tasks like authentication, js bugs,
screenshots.

regards
SenthilNayagam
 
R

reed.adam

Thank you for the input Senthi -- I am just bringing my company into
the 19th century (no typo) as far as QA/Testing is concerned. We have
3 sites that are no longer going to be updated which I am writing these
simple tasks for while I learn Ruby/Watir.

Over the next 12 months we will have about 7-8 new sites for testing.
At that point hopefully I will know quite a bit more about Ruby and be
able to support those projects.

I know the answer to the question is relative to the person answering
it, but how difficult was it for you to design that bot? Like I said,
I am just getting to know Watir and Ruby -- I have the logic for
scripting, it just hasn't been available to our company until recently.

I am sure something like that would serve us well. Our sites will
require link testing on static pages, and then automated login/action
on the dynamic pages (most likely they will judge those as "too many
links" to test).

And of course, can you share the source for that bot?
 
S

senthil.nayagam

getting the bot to work was about an hour,

the difficult part were the analysis stuff, which took 2 days.

can't opensource it now, probably will add it as a service from my
website, will keep you updated

regards
Senthil Nayagam
 
U

uncutstone

Ok, so sth is a variable you created to be able to assign each link name/etc at the >end of that code? That's not ruby-reserver, right? (I'm an idiot)

No, it is't ruby-reserved, it's just a argument name, a argument of a
function.
 
R

reed.adam

Well, I definitely wasn't expecting you to say "an hour" even just for
the bot section -- that's very encouraging. While I doubt I'll be able
to do it in the same timeframe right now, it's nice to know that it can
eventually be done.

Thanks again for the info!
 
B

Bret Pettichord

Your script does not test for links, but rather for text. This will
find links (using Watir):

require 'test/unit/assertions'
assert ie.link:)text, "Home").exists?
assert ie.link:)text, "This Link").exists?

I also like the suggestion to use Mechanize.

Bret
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top