Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4191 > unrolled thread

Starting Method/Class issues

Started byTJ Wilkes <tjwilkes@live.com>
First post2011-05-10 12:32 -0500
Last post2011-05-11 17:39 -0500
Articles 10 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  Starting Method/Class issues TJ Wilkes <tjwilkes@live.com> - 2011-05-10 12:32 -0500
    Re: Starting Method/Class issues Robert Klemme <shortcutter@googlemail.com> - 2011-05-10 22:18 +0200
    Re: Starting Method/Class issues 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-10 16:07 -0500
      Re: Starting Method/Class issues TJ Wilkes <tjwilkes@live.com> - 2011-05-10 16:34 -0500
        Re: Starting Method/Class issues 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-10 19:26 -0500
      Re: Starting Method/Class issues Brian Candler <b.candler@pobox.com> - 2011-05-11 03:39 -0500
    Re: Starting Method/Class issues TJ Wilkes <tjwilkes@live.com> - 2011-05-11 14:28 -0500
      Re: Starting Method/Class issues 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 14:56 -0500
    Re: Starting Method/Class issues TJ Wilkes <tjwilkes@live.com> - 2011-05-11 16:04 -0500
      Re: Starting Method/Class issues 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 17:39 -0500

#4191 — Starting Method/Class issues

FromTJ Wilkes <tjwilkes@live.com>
Date2011-05-10 12:32 -0500
SubjectStarting Method/Class issues
Message-ID<db27adb124f75fd3cd3e99b520e711c7@ruby-forum.com>
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
-----------------------------------------------

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#4199

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-05-10 22:18 +0200
Message-ID<92tkt2FnfaU1@mid.individual.net>
In reply to#4191
On 10.05.2011 19:32, TJ Wilkes wrote:
> 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

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#4204

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-10 16:07 -0500
Message-ID<71429885376e6338580943ad50c8eb2e@ruby-forum.com>
In reply to#4191
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

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4211

FromTJ Wilkes <tjwilkes@live.com>
Date2011-05-10 16:34 -0500
Message-ID<3caa79ffaf43a52149b9003788552318@ruby-forum.com>
In reply to#4204
7stud -- wrote in post #997840:
> 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.

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


-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4218

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-10 19:26 -0500
Message-ID<95ea7c810f1fbc89f31db3970ec7a3de@ruby-forum.com>
In reply to#4211
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

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4243

FromBrian Candler <b.candler@pobox.com>
Date2011-05-11 03:39 -0500
Message-ID<4ed6be5ceda1893dbae4a87329421793@ruby-forum.com>
In reply to#4204
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.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4285

FromTJ Wilkes <tjwilkes@live.com>
Date2011-05-11 14:28 -0500
Message-ID<d66cc98751bf995f7fa71bc7f3edc2f3@ruby-forum.com>
In reply to#4191
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

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4289

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-11 14:56 -0500
Message-ID<928cfbb68d8fb163331d8e830c0e0217@ruby-forum.com>
In reply to#4285
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}'.}

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4299

FromTJ Wilkes <tjwilkes@live.com>
Date2011-05-11 16:04 -0500
Message-ID<0f840f1d86026efc96147df31fc30b8a@ruby-forum.com>
In reply to#4191
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.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4305

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-11 17:39 -0500
Message-ID<dafa08854ab6f291fcbf37654ac6205d@ruby-forum.com>
In reply to#4299
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.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web