Using a main Function in the Script

Although most of the script files you create contain only test cases, in some instances you need to add a function named main to your script. You can use the main function to pass data to test cases as well as control the order in which the test cases in the script are executed.

When you run a script file by clicking Run > Run:


The following template shows the structure of a script that contains a main function that passes data to a data-driven test case:

main ()
// 1. Declare a variable to hold current record
// 2. Store all data for test case in a list of records
// 3. Call the test case once for each record in the list

Using this structure, the following example shows how to create a script that defines data records and then calls the sample test case once for each record in the list:

type SEARCHINFO is record
   STRING  sText      // Text to type in document window
   STRING  sPos       // Starting position of search
   STRING  sPattern   // String to look for
   BOOLEAN bCase      // Case-sensitive or not
   STRING  sDirection // Direction of search
   STRING  sExpected  // The expected match
main ()   
   list of SEARCHINFO lsData = {...}
      {"Test Case", "<END>", "C", TRUE, "Up", "C"}
      {"Test Case", "<END>", "Ca", TRUE, "Up", "Ca"}
      // additional data records can be added here
   for each Data in lsData
      FindTest (Data)
testcase FindTest (SEARCHINFO Data)
   TextEditor.File.New.Pick ()
   DocumentWindow.Document.TypeKeys (Data.sText + Data.sPos)
   TextEditor.Search.Find.Pick ()
   Find.FindWhat.SetText (Data.sPattern)
   Find.CaseSensitive.SetState (Data.bCase)
   Find.Direction.Select (Data.sDirection)
   Find.FindNext.Click ()
   Find.Cancel.Click ()
   DocumentWindow.Document.VerifySelText ({Data.sExpected})
   TextEditor.File.Close.Pick ()
     MessageBox.No.Click ()

When you click Run > Run, the main function is called and the FindTest test case will be executed once for every instance of Data in lsData (the list of SEARCHINFO records). In the script shown above, the test case will be run twice. Here is the results file that is produced:

Script findtest.t - Passed
Passed:  2 tests (100%)
Failed:  0 tests (0%)
Totals:  2 tests, 0 errors, 0 warnings

Testcase FindTest ({"Test Case", "<END>", "C", TRUE, "Up", "C"}) - Passed
Testcase FindTest ({"Test Case", "<END>", "Ca", TRUE, "Up", "Ca"}) - Passed
Note: With data-driven test cases, Silk Test Classic records the parameters that are passed in, in the results file.

In this sample data-driven test case, the test case data is stored in a list within the script itself. It is also possible to store the data externally and read records into a list using the FileReadValue function.