using ruby to validate web pages

K

Kate Bond

Hi, just wondering if anybody can tell me what's wrong with this code:


# the Watir gem
require 'rubygems'

# the Watir controller
require 'watir'

# set variables
validator = "http://validator.w3.org/"
url1 = "http://www.google.com"
url2 = "http://pingmag.jp/2008/01/17/chimpom/"
url3 = "http://www.google.com/"

# open the IE browser
ie = Watir::IE.new

puts "Beginning validation"

ie.goto validator

ie.text_field:)id, "uri").set(url1)

puts "Checking URL #1....."

ie.button:)value, "Check").click

if ie.text.include? "This Page Is Valid"
puts "URL#1 validates!"
else
"URL#1 does not validate!"

end

ie.goto validator

ie.text_field:)id, "uri").set(url2)

puts "Checking URL #2....."

ie.button:)value, "Check").click

if ie.text.include? "This Page Is Valid"
puts "URL#2 validates!"
else
"URL #2 does not validate!"

end

ie.goto validator

ie.text_field:)id, "uri").set(url3)

puts "Checking URL #3....."

ie.button:)value, "Check").click

if ie.text.include? "This Page Is Valid"
puts "URL#3 validates!"
else
"URL #3 does not validate!"

end

puts "End of validation"


I get results for URL's #2 and #3, only the 'Checking URL....' and 'End
of validation' messages.
 
L

lrlebron

Hi, just wondering if anybody can tell me what's wrong with this code:

# the Watir gem
require 'rubygems'

# the Watir controller
require 'watir'

# set variables
validator = "http://validator.w3.org/"
url1 = "http://www.google.com"
url2 = "http://pingmag.jp/2008/01/17/chimpom/"
url3 = "http://www.google.com/"

# open the IE browser
ie = Watir::IE.new

puts "Beginning validation"

ie.goto validator

ie.text_field:)id, "uri").set(url1)

puts "Checking URL #1....."

ie.button:)value, "Check").click

if ie.text.include? "This Page Is Valid"
puts "URL#1 validates!"
else
"URL#1 does not validate!"

end

ie.goto validator

ie.text_field:)id, "uri").set(url2)

puts "Checking URL #2....."

ie.button:)value, "Check").click

if ie.text.include? "This Page Is Valid"
puts "URL#2 validates!"
else
"URL #2 does not validate!"

end

ie.goto validator

ie.text_field:)id, "uri").set(url3)

puts "Checking URL #3....."

ie.button:)value, "Check").click

if ie.text.include? "This Page Is Valid"
puts "URL#3 validates!"
else
"URL #3 does not validate!"

end

puts "End of validation"

I get results for URL's #2 and #3, only the 'Checking URL....' and 'End
of validation' messages.

Your are missing the "puts" before the "URL # does not validate"
 
S

Siep Korteling

Your are missing the "puts" before the "URL # does not validate"

If you had 200 pages to validate, you'd have to correct 200 lines of
code.
Consider this (untested, I don't know Watir):

require 'watir'

# set variables
validator = "http://validator.w3.org/"
urls =[]
urls << "http://www.google.com"
urls << "http://pingmag.jp/2008/01/17/chimpom/"
urls << "http://www.google.com/"

# open the IE browser
ie = Watir::IE.new
puts "Beginning validation"
ie.goto validator # maybe this should be in the block.

urls.each do|url|
puts "Checking " + url
ie.text_field:)id, "uri").set(url)
ie.button:)value, "Check").click
if ie.text.include? "This Page Is Valid"
puts url + " validates!"
else
url + " does not validate!" #!! This line needs a 'puts' !!
end
end
ie.close #or something like it
puts "End of validation"

Both the the list of urls and the code handling this list are now much
easier to maintain.

Regards,

Siep
 
K

Kate Bond

Siep,

Wow! Had to add 'goto validator' to the block but otherwise worked a
treat... Thanks very much! :)


# the Watir gem
require 'rubygems'

# the Watir controller
require 'watir'

# set variables
validator = "http://validator.w3.org/"
urls =[]
urls << "http://www.catnic.com/"
urls << "http://pingmag.jp/2008/01/17/chimpom/"
urls << "http://www.google.com/"

# open the IE browser
ie = Watir::IE.new
puts "Beginning validation"

urls.each do|url|
puts "Checking " + url
ie.goto validator # maybe this should be in the block.
ie.text_field:)id, "uri").set(url)
ie.button:)value, "Check").click
if ie.text.include? "This Page Is Valid"
puts url + " validates!"
else
puts url + " does not validate!" #!! This line needs a 'puts' !!
end
end
ie.close #or something like it
puts "End of validation"



I'm very new to Ruby (programming in general, actually) but I wonder if
there is a way to incorporate this into a webpage? This might not seem
much but is going to make my job as a tester a little easier!
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top