/* * DataDriver.java * TSJAPI v1.1 * 15 March 2004 * © Quality Forge, 2004 */ /***************************************************************************** DataDriver.java This Java test demonstrates how to use data stored in a .csv file. ---------------------------------------------------------------------------- Description of the System on which the script was recorded: Windows 2000, Screen Size: 1024 x 768, Color: High Color (16 bit) *****************************************************************************/ //package <...> import java.util.Hashtable; import qualityforge.testsmith.tsjapi.JSmith; import qualityforge.testsmith.tsjapi.InSessionException; import qualityforge.testsmith.tsjapi.UninitializedException; public class DataDriver extends JSmith { /** * @param data data should contain one key named "count" with an Integer value */ public int run(Hashtable data) { //initialize try { //initialize test //note: the \Reports directory must already exist in the // same directory where this java class file exists int initialized = initSession("Reports\\jDataDriver"); //to run without generating any TestSmith reports use: //int initialized = initSession(NO_REPORT); if (initialized == FALSE) { //handle error... //use getLastTSError() for errors generated by TSAPI //use getLastJError() for errors generated by JSmith return -1; } } catch (InSessionException e) { //handle error, e.g. System.err.println(e.toString()); return -1; } try { //set environment int envSet = setEnv("LAUNCH=Notepad.exe"); if (envSet == TRUE) { envSet = setEnv("DATA=C:\\QualityForge\\TestSmith\\Data\\smith1.csv"); } if (envSet == TRUE) { envSet = setEnv("DATA=C:\\QualityForge\\TestSmith\\Data\\smith2.csv"); } //if any of the above calls failed, deal with it here if (envSet == FALSE ) { //handle error... //use getLastTSError() for errors generated by TSAPI //use getLastJError() for errors generated by JSmith return -1; } //run test int lasterr = wndInit("wndInit id=1 wtitle~=Notepad wclass=Notepad " + "zsync=s6 zinterval=s2"); if (lasterr == NO_ERR) { //the description of the window is the same for all following calls String windowDescription = "id=1 wnum=1 wcid=15 wclass=Edit wpclass=Notepad "; int count = 1; //default if (data != null) { //get the "count" value from the Hashtable count = ((Integer)data.get("count")).intValue(); } ctrlEditSetText(windowDescription + "ctext='Java Test\n'"); for (int i=1; i<=count; i++) { //increment the data index setDataIndex(i); String line1 = "'-----\n" + "{_TIMESTAMPNOW1_}\n" + i + ":{index}\n" + "Hello {firstname} {lastname}. You are {age}.\n'"; String line2 = "' You said: {comment}\n'"; String line3 = "' You randomly shout: {x1}... {x2}... !!!\n'"; //enter some text into Notepad, using replacement vars ctrlEditSetText(windowDescription + "cedit=end " + "ctext=" + line1); ctrlEditSetText(windowDescription + "cedit=end " + "ctext=" + line2); ctrlEditSetText(windowDescription + "cedit=end " + "ctext=" + line3); try { Thread.sleep(500); } catch (java.lang.InterruptedException e) {} } //check that the text contains the name "JAMES BOND" (ignoring case) //if the loop count is less than 4 this validation will fail lasterr = valEditText(windowDescription + "vtext~='JAMES BOND'`i"); if (lasterr > NO_ERR) { //handle error, e.g. System.err.println("Error: Unable to find James Bond."); } //uncomment next command to close Notepad //wndClose("id=1"); } //end test int exit_ok = exitSession(); if (exit_ok == 0) { //handle error... //use getLastTSError() for errors generated by TSAPI //use getLastJError() for errors generated by JSmith } //full test ran without exceptions, so return getExitCode(); } catch (UninitializedException e) { //handle error, e.g. System.err.println(e.toString()); return -1; } } //============================================================================= //TEST public static void main(String[] args) { int count = 1; //default if (args.length > 0) { try { count = Integer.parseInt(args[0]); } catch (NumberFormatException e) { System.err.println("Error: " + e + ", using default value 1"); } } Hashtable data = new Hashtable(); data.put("count", new Integer(count)); DataDriver dataDriver = new DataDriver(); int exitCode = dataDriver.run(data); System.out.println("exitCode: " + exitCode); //show the TestSmith report in a browser window JSmith.displayLastReport(); } //============================================================================= }