# --------------------------------------------------------- # calctestsuite.rb # Tester Name: Tobias Mayer # Project Name: TSRAPI Example # Create Date: 25 August 2005 # © Quality Forge, 2004-2005 # --------------------------------------------------------- require 'wincalc' =begin ----------------------------------------------------------------------- A set of simple Windows Calculator Tests This example shows the use of a domain-specific class, WinCalc to create a higher level abstraction of the RubySmith calls. See the file wincalc.rb for the definition of WinCalc. ----------------------------------------------------------------------- =end class CalcTestSuite < WinCalc # TestSmith uses this variable name to display the report in the TestSmith report window @@REPORT_NAME = "reports\\rCalcTestSuite" # close the calculator at the end? @@CLOSE = false def initialize(winid) super(winid) end def run(data = nil) result = initSession(@@REPORT_NAME) return result unless (result == 0) begin result = openCalc() return result unless (result == 0) # run each test test1() test2() test3() test4() test5() # optionally close or clear the Calculator application if (@@CLOSE) closeCalc() # close it else funcKey('C') # clear it end exit_ok = exitSession() if (!exit_ok) # handle error, e.g. puts lastTSError() # last error generated by TSAPI end # full test ran without exceptions, so return self.exitCode rescue UninitializedException => ue # handle error, e.g. puts ue return -1 end end # run method protected # Calculate 1234 + 5678 and validate the result. def test1() @cmd = 1 remark("Test1: Calculate 1234 + 5678 and validate the result.") numKey(1); numKey(2); numKey(3); numKey(4) funcKey('+') numKey(5); numKey(6); numKey(7); numKey(8) funcKey('=') expectedResult = 6912.0 # 1234 + 5678 as a float # validate and print result printResult("Test1", expectedResult) end # Calculate 99 - 100 and validate the result. def test2() @cmd = 2 remark("Test2: Calculate 99 - 100 and validate the result.") numKey(9); numKey(9) funcKey('-') numKey(1); numKey(0); numKey(0); funcKey('=') expectedResult = -1.0 # 99 - 100 as a float # validate and print result printResult("Test2", expectedResult) end # Calculate 1359 * 2468 and validate the result. # Note: this test uses the 'WinCalc#input' method # to send in complete integer numbers with a single method call def test3() @cmd = 3 remark("Test3: Calculate 1359 * 2468 and validate the result.") input(1359) funcKey('*') input(2468) funcKey('=') expectedResult = 3354012.0 # 1359 * 2468 as a float # validate and print result printResult("Test3", expectedResult) end # Calculate 55.66 + 77.88 and validate the result. # Note: this test uses the 'WinCalc#input' method # to send in complete float numbers with a single method call def test4() @cmd = 4 remark("Test4: Calculate 55.66 + 77.88 and validate the result.") input(55.66) funcKey('+') input(77.88) funcKey('=') expectedResult = 133.54 # 55.66 + 77.88 # validate and print result printResult("Test4", expectedResult) end # Calculate 20 / 5 and validate the result. # Note: this test is intended to fail; # the 'expectedResult' value is incorrect def test5() @cmd = 5 remark("Test5: Calculate 20 / 5 and validate the result.") numKey(2); numKey(0) funcKey('/') numKey(5); funcKey('=') expectedResult = 5.0 # incorrect result # validate and print result printResult("Test5", expectedResult) end # Validate the result and write an appropriate message # to the TestSmith report. # This method is called by each test method. def printResult(testName, expectedResult) actualResult = getDisplayedValue() if (expectedResult == actualResult) printOk(testName + ": Validation Passed", @cmd) else printErr(testName + ": Validation Failed: expected = " + expectedResult.to_s + \ ", actual = " + actualResult.to_s, @cmd) end end # Write a remark to the TestSmith report # This method is called by each test method. def remark(text) noop("remark='" + text + "' suppress=all", @cmd) end end # class CalcTest1 # ==================================================================== # TEST begin objCalcTestSuite = CalcTestSuite.new(1) exitCode = objCalcTestSuite.run() puts "exitCode: " + exitCode.to_s # show the TestSmith report in a new browser window # objCalcTestSuite.displayReport() rescue Exception => ex puts ex end # ====================================================================