# --------------------------------------------------------- # wincalc.rb # Tester Name: Tobias Mayer # Project Name: TSRAPI Example # Create Date: 25 August 2005 # © Quality Forge, 2004-2005 # --------------------------------------------------------- require 'rubysmith' =begin ----------------------------------------------------------------------- Abstract class representing the Windows Calculator This must be extended and the RubySmith#run method implemented to create a test. ----------------------------------------------------------------------- =end class WinCalc < RubySmith # the TestSmith Window identifier @winid = "" def initialize(winid) super() # winid: the TestSmith Window identifier @winid = " id=" + winid.abs().to_s + " " # cmd: the TestSmith report CMD identifier @cmd = 0 end attr_writer :cmd # initialize the TestSmith TSRAPI session def initSession(report_name = nil) begin initialized = super(report_name) if (!initialized) puts("Session not initialized.") puts("TS Error: " + lastTSError()) unless lastTSError() == "" puts("RubySmith Error: " + lastRubyError()) unless lastRubyError() == "" return -1 end rescue InSessionException => ise # handle error, e.g. puts ise return -1 end return 0 # no error end # Open the Calculator application def openCalc() result = system("run=calc.exe suppress=oid") if (result == 0) result = wndInit(@winid + "wtitle=Calculator wclass=SciCalc suppress=oid") end return result end # Close the Calulator application def closeCalc() wndSetFocus(@winid) wndClose(@winid) end # Press a number key on the calculator. def numKey(num) wcid = 124 + num.to_i params = @winid + "wclass=Button wpclass=SciCalc title='' suppress=odi wnum=-1 wcid=" + wcid.to_s return ctrlButtonClick(params) end # numKey method # Specify a number to be sent to the Calculator as a set of key strokes # Both integer and float numbers are accepted # e.g. input(1.2) will create three keystrokes '1', '.' and '2' def input(num) num.to_s().each_byte {|c| (c.chr== '.')? funcKey(c.chr) : numKey(c.chr.to_i)} end # Press a function key or decimal point on the calculator. # Only . + - * / = C CE are supported in this implementation def funcKey(func) wcid = (func=='.')? 85 :(func=='/')? 90 :(func=='*')? 91 : (func=='+')? 92 :(func=='-')? 93 :(func=='=')? 112 : (func=='C')? 81 :(func=='CE')? 82 : 0 params = @winid + "wclass=Button wpclass=SciCalc title='' suppress=odi wnum=-1 wcid=" + wcid.to_s return ctrlButtonClick(params) end # funcKey method # Get the value currently shown in the calculator display window # and return as a floating point number. def getDisplayedValue() params = @winid + "wtitle~=, wclass=Static wpclass=SciCalc suppress=odi wnum=-1 wcid=403" ctrlStaticClick(params) wtitle = getString("wtitle") # remove any formatting comments wtitle.gsub!(",", "") return wtitle.to_f end # valResult method end # class WinCalc