I suggest that you put your library methods in a Module. Thus:
# library.rb
module MyLibrary
def my_library_method(arg)
# code...
end
end
# tests.rb
require 'test/unit'
require 'library.rb'
class TC1 << Test::Unit::TestCase
include MyLibrary
def test_method
my_library_method arg
# more code
end
end
At 04:39 PM 8/19/2005, Joe Yakich wrote:
Gurus,
I m testing a fairly complicated web application. We have been using Canoo
WebTest for GUI testing, but we decided to try out Watir and we ve been
very pleased with it so far.
Because of the complexity, I wanted to have a layer (a library or API type
level) containing Watir calls and then have the test scripts themselves
require the library classes. (Note that I am not wedded to classes if they
are inappropriate; modules would be just fine.) I had hoped to insulate
most of the Watir calls in the library, partly to localize changes if the
application under test changes, partly to make writing the actual scripts
easier for others, and partly to make the logic of the actual test case
more apparent.
Anyway, my primary problem is this: I thought it would be convenient to
put assert() calls in the library class (Login.rb, below), but when I do
so and run the test script (test_login.rb, below), those assertions are
not counted up when the test script runs, although if I introduce
a failure, the failure count does include the error. That is, I see this:
1 tests, 0 assertions, 0 failures, 0 errors [ when the test script passes]
1 tests, 0 assertions, 1 failures, 0 errors [ when the test script fails ]
when I was expecting this:
1 tests, 1 assertions, 0 failures, 0 errors [ when the test script passes]
1 tests, 1 assertions, 1 failures, 0 errors [ when the test script fails ]
Directly inheriting Test::Unit::TestCase in my library didn t seem to
work, either, as then I get messages like No tests were specified.
Secondarily, if anyone has suggestions on how to better set up a similar
library/script hierarchy, I would love to hear them. I m new to Ruby and
Watir, so suggestions about coding style, naming, etc. are welcomed as well.
Code follows (note that I edited the code for brevity and that it s
possible that it won t actually run, but I thought it was sufficient for
illustration):
##################################################################
# test_login.rb
# The actual test script (edited for brevity)
##################################################################
$:.push("../../../lib")
require 'Login/Login'
require 'watir'
require 'test/unit'
class SimpleLogin < Test::Unit::TestCase
include Watir
@username = 'username'
@password = 'password'
@url = 'http://www.fake_url.com/login_page.html'
def test_login
ie = IE.new
l = Login.new()
l.login(ie, @url, @username, @password)
end
end
##################################################################
# Login.rb
# The Login class (edited for brevity)
##################################################################
class Login
require 'test/unit'
require 'test/unit/assertions'
require 'watir'
include Watir
include Test::Unit::Assertions
def login(ie, url, username, password)
ie.goto(url)
ie.link(:text, "site login").click
ie.text_field(:name, "username").set(username)
ie.text_field(:name, "password").set(password)
ie.button(:name, 'submitBtn').click
assert(ie.contains_text("Welcome #{username}"))
end
end
######################################################################################################
# End of code
######################################################################################################
Thank you!
Joe
_______________________________________________
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general
_____________________
Bret Pettichord
www.pettichord.com
_______________________________________________
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general