Starting Method/Class issues

T

TJ Wilkes

Hi I've been learning Ruby to help with scripting some tests, but the
books I have are only using mathematical method/class creations and my
attempts to make my own methods are failing. Looking for some help with
the creation of two time savers.

Note: Using ruby 1.8.7 and Watir-webdriver as well as IE 9.

I'm trying to set up some page validation scripts but every attempt has
failed, these run prefectly fine in the program if I repeat them and
fill in linkX and PagetextX with specfics each time but i'd prefer to be
able to run each with a simple bit of code calling the function.
Such as: verify.LinkX("Terms")or verify.LinkX(href/ID/etc, "Terms")
#which would validate a link called Terms.
and verify.PagetextX("Your info is important").

Thanks for any help or direction,
TJ

Code samples below.

#First process--------------------------
puts "\n"
puts "Verifying (linkX)."
ie.link:)id, "(linkX)").exist?
print "Actual Result:"
if ie.text.include? "(linkX)"
puts " Test Passed. Found: 'LinkX'. Actual Results match expected
Results."
else
puts " Test Failed! Could not find: 'LinkX'."
end
ie.link:)href, /(linkx)/).click
#Second process-------------------------------
puts "\n"
puts "Checking for Page text '(PagetextX)'."
ie.text.include? "Govern Your Use of Our Site"
print "Actual Result:"
if ie.text.include? "(PagetextX)"
puts " Test Passed. Found the test string: '(PagetextX)'. Actual
Results match expected Results."
else
puts " Test Failed! Could not find: '(PagetextX)'."
end
 
R

Robert Klemme

Hi I've been learning Ruby to help with scripting some tests, but the
books I have are only using mathematical method/class creations and my
attempts to make my own methods are failing. Looking for some help with
the creation of two time savers.

Note: Using ruby 1.8.7 and Watir-webdriver as well as IE 9.

I'm trying to set up some page validation scripts but every attempt has
failed, these run prefectly fine in the program if I repeat them and
fill in linkX and PagetextX with specfics each time but i'd prefer to be
able to run each with a simple bit of code calling the function.
Such as: verify.LinkX("Terms")or verify.LinkX(href/ID/etc, "Terms")
#which would validate a link called Terms.
and verify.PagetextX("Your info is important").

Thanks for any help or direction,

Sorry, I can't help you with Watir, but: do you really need to automate
a Browser for this? It seems for simple validation of page content you
could as well read pages (e.g. with net/http or more sophisticated
tools) and process the content (e.g. with Nokogiri).

If your server delivers different pages based on browser type you can
send the appropriate HTTP header to make it believe you are IE 9.

Of course, if you intend to also evaluate JavaScript in pages etc. then
your approach is better. In that case maybe you also want to look at
http://wiki.openqa.org/display/WTR/XPath

Kind regards

robert
 
7

7stud --

TJ Wilkes wrote in post #997808:
Hi I've been learning Ruby to help with scripting some tests, but the
books I have are only using mathematical method/class creations and my
attempts to make my own methods are failing. Looking for some help with
the creation of two time savers.

Note: Using ruby 1.8.7 and Watir-webdriver as well as IE 9.

I'm trying to set up some page validation scripts but every attempt has
failed, these run prefectly fine in the program if I repeat them and
fill in linkX and PagetextX with specfics each time

How about an example of that?

Your question is very hard to decipher. It appears that you have some
code with a string in it, e.g. linkX, and you don't understand what a
variable is or how to use one in a string. So here are some examples:


arr = [10, 30, 20]

arr.each do |num|
puts "The numbers is: #{num}"
end

--output:--
The numbers is: 10
The numbers is: 30
The numbers is: 20



Next, instead of using a variable inside a string, you can also call a
function:

def verify(x)
if x > 15
'valid'
else
'invalid'
end
end


arr = [10, 30, 20]

arr.each do |num|
puts "The page is: #{verify(num)}"
end

--output:--
The page is: invalid
The page is: valid
The page is: valid


But sometimes trying to cram to0 much code in a string can be confusing,
so you could do something like this as well:

def verify(x)
if x > 15
true
else
false
end
end


arr = [10, 30, 20]

arr.each do |num|
print 'The page is: '

if verify(num)
puts 'valid'
else
puts 'invalid'
end
end


--output:--
The page is: invalid
The page is: valid
The page is: valid
 
T

TJ Wilkes

7stud -- wrote in post #997840:
TJ Wilkes wrote in post #997808:

How about an example of that?


We need to see an example of that too.

I've attached my working code for a short test, when manually putting
pages/links in, so you can see what I'm trying to do.

Below I've put what my first attempt for simplifying before I decided to
post a question.

I want to be able to put a method that accepts a string variable and
then runs a process using that string.


Also, thanks to everyone for responding,
-TJ


def verify(x)
print "Verifying ", v, "."
ie.link:)id, x).exist?
print "Actual Result:"
if ie.text.include? x
puts " Test Passed. Found the test string: ", x, ". Actual Results
match expected Results."
ie.link:)href, x).click
else
puts " Test Failed! Could not find: ", x, "."
end
end

Attachments:
http://www.ruby-forum.com/attachment/6185/post.rb
 
7

7stud --

TJ Wilkes wrote in post #997849:
def verify(x)
print "Verifying ", v, "."
ie.link:)id, x).exist?
print "Actual Result:"
if ie.text.include? x
puts " Test Passed. Found the test string: ", x, ". Actual Results
match expected Results."
ie.link:)href, x).click
else
puts " Test Failed! Could not find: ", x, "."
end
end

Inside that method, there is no variable called 'ie'. You either need
to create ie inside the method, or you need to pass ie in as an
argument, like this:

def verify(x, obj)
obj.link:)id, x).exist?
...
...
end
 
B

Brian Candler

7stud -- wrote in post #997840:
Next, instead of using a variable inside a string, you can also call a
function:

def verify(x)
if x > 15
'valid'
else
'invalid'
end
end

And another step, say if the number 15 is a property which you want to
set, is to make a class.

class Verifier
def initialize(threshold)
@threshold = threshold
end

def verify(x)
if x > @threshold
'valid'
else
'invalid'
end
end
end

v1 = Verifier.new(15)
puts v1.verify(20)
v2 = Verifier.new(25)
puts v2.verify(20)

The Verifier instance objects, v1 and v2, wrap up the 'verify' method
with some state (instance variable @threshold) which can be used when
that method is called.

In your case, you might want to build a class with an @ie instance
variable, so that the methods can have access to @ie without having to
pass it in each time.
 
T

TJ Wilkes

Thanks, that was exactly what I needed.

I ended up with:
def tverify(str, ie)
puts "\n"
print "Checking for text '", str, "'. "
puts "\n"
print "Actual Result: "
if ie.text.include? str
print " Test Passed. Found the string: '", str, "'. Valid"
puts "\n"
else
print " !ERROR! Test Failed! Could not find: '", str, "'."
puts "\n"
end
end

and

def lifollow(str, ie)
puts "\n"
print "Verifying ", str, ". "
puts "Actual Result:"
if ie.li:)id, str).exist?
print " Found the link: '", str, "'. Following link."
puts "\n"
ie.li:)id, str).click
sleep(1)
else
print " Test Failed! Could not find: ", str, "."
puts "\n"
end
end

Seems to be working perfectly.
Thanks everyone.
-TJ
 
7

7stud --

TJ Wilkes wrote in post #998080:
Thanks, that was exactly what I needed.

I ended up with:
def tverify(str, ie)
puts "\n"
print "Checking for text '", str, "'. "
puts "\n"

You are doing way to much work with all your print's and puts
statements. The lines:

print "Checking for text '", str, "'. "
puts "\n"

are equivalent to:

puts "Checking for text '#{str}'."

Not only is that code easier to type, the code is easier to read, which
is even more important. Note that the outer quotes determine if
variables will be interpolated, not any inner quotes.

And if the quotes get too confusing, you can use %q{} for single quotes,
and %Q{} for double quotes, like this:

puts %Q{Checking for text '#{str}'.}
 
T

TJ Wilkes

Modified with your suggestion:

def tverify(str, ie)
puts "\n"
puts "Checking for text '#{str}'."
print "Actual Result: "
if ie.text.include? str
puts "Test Passed. Found the string: '#{str}'. Valid"
else
puts " !ERROR! Test Failed! Could not find: '#{str}'."
end
end

def lifollow(str, ie)
puts "\n"
puts "Verifying '#{str}'. "
print "Actual Result:"
if ie.li:)id, str).exist?
puts "Found the link: '#{str}'. Following link."
ie.li:)id, str).click
sleep(1)
else
puts " !ERROR! Test Failed! Could not find: '#{str}'."
end
end


Thanks for the tip.
 
7

7stud --

TJ Wilkes wrote in post #998101:
Modified with your suggestion:

def tverify(str, ie)
puts "\n"

Some more tips. puts() adds a newline to a string *only if it doesn't
end with a newline already*, so the line:

puts "\n"

is equivalent to just

puts

And these two lines:

puts "\n"
puts "Checking for text '#{str}'."

can also be written like this:

puts "\nChecking for text '#{str}'."

although you often see it written like this:

puts
puts "Checking for text '#{str}'."

...because newlines are the most favorite things to type.
 

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,979
Messages
2,570,185
Members
46,728
Latest member
FernMcmull

Latest Threads

Top