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?
my attempts to make my own methods are failing.
We need to see an example of that too.
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 number is: #{num}”
end
–output:–
The number is: 10
The number is: 30
The number 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 too 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
You can also ‘interpolate’ variables, or the result of a function call,
into a regex:
def do_stuff
‘hello’
end
if ‘hello world’ =~ /#{do_stuff}/
puts ‘yes’
end
–output:–
yes