I am working on a program in Ruby with Watir that has a series of modules.
An example module would look like:
demoMod1.rb
DEMOMOD1 = true #this module has been loaded
module demoMod1
def reload(browser)
browser.goto("demo1.html")
end
end
There are several files with very similarly named functions (example: reload() ), so demoMod2 and demoMod3 both have the same reload function (except pointing to different pages).
The modules are commented out of the require list when that area is not expected to be tested. (as per design specs)
The program then dynamically loads test case files (code to do that is below in main.rb). Each test case file contains a runTest() function, which calls the appropriate sections from the modules. Each test checks if the module is loaded, and if it has, the test executes.
A sample test case file would look like:
test1.rb
def runTest(browser) # test case 1
if(defined?(DEMOMOD1)).nil?
return nil #if the module is not there, dont run this test
end
demoMod1::reload(browser)
end
As per specs, a single file lists all of the modules to be required, so that a user can just comment out the sections to not be tested without having to look at the main file. The main file is below.
main.rb
require 'Watir'
SITE = #internal site - address removed =/ sorry
FOLDER = File.dirname(__FILE__) + '/lib'
SHORTNAME = 'Test' #for reporting
LONGNAME = 'In Development' #for reporting
#I know - globals.. yuck... avoid...
require FOLDER + '/lib/requires'
#includes the function to require each module from the module folder
require FOLDER + '/www/include/report.module'
#creates an html report to view the test results
# Get list of tests
def getTests()
Dir.chdir(FOLDER + '/Tests')
files = Dir.glob("*.rb")
return files
end #eofunc
# Run each test
def runTests(files, browser, report)
testSuite = String.new
files.each do |test|
suite = test.split(" - ") # name convention: suite - testname.rb
if !(suite[0]==testSuite)
testSuite = suite[0]
report.newTestSuiteName(testSuite)
end #eoif
load FOLDER + '/Tests/' + test #load the test case file
runTest(browser) #run the test case
end #eoeach
end #eofunc
begin
browser = Watir::Browser.new
browser.goto(SITE)
report = Report.new()
reportname = report.createReport(SHORTNAME, LONGNAME) #vars in Vars.rb
files = getTests()
runTests(files, browser, report)
report.finishReport(reportname, SITE)
browser.close
report.openReport(reportname) #launch report in a new browser
end #eofunc
I would expect in each of the runTest() to be able to access the demoMod1::reload() and demoMod2::reload(), but this is not the case. I get an error for undefined method 'reload' for demoMod1:Module. I can resolve the error by using include demoMod1 but then (if I have this correctly), the scope is not needed, and using just reload() would work. However this would require each module have entirely unique names, and that is not a great solution.
What am I missing?
By changing the function names in the modules I can get it to work.
example module:
Line with the <— arrow is the main change (also had to change the module name, did not have a capital first letter…oops)
example test case: