C# Microsoft Visual Studio 2005

  1. Start Microsoft Visual Studio 2005 and select Create Project from the start page or File->New->Project.
  2. Select C# windows, class library project, give it a suitable name and press OK.
    screenshot
  3. Right click on the project and select properties, Build, All Configurations in the Configuration box and in the Build options select the Register for COM Interop
    screenshot
  4. Open the AssemblyInfo.cs file (located in the Properties folder of the project) and set ComVisible to true.
    screenshot
  5. Right click on the Class1cs file and select rename and change the name to <TesterName>TesterCreator.cs replacing <TesterName> with whatever makes sense in your case. In this example we use the name DemoTesterCreator.cs
  6. Right click on the project and select Add Reference.
  7. Select the COM tab and then TestScriptRunner 5.0 Type Library then press OK.
    screenshot
  8. Open your tester creator source file and a using statement for TestScriptRunnerLib and make the class inherit the ITesterCreator1 interface.
          using System;
          using System.Collections.Generic;
          using System.Text;
          using TestScriptRunnerLib;
          namespace Demo_CSharp_2005
          {
              class DemoTesterCreator : ITesterCreator1
              {
              }
          }
         
  9. Right click on ITesterCreator1 and select Implement Interfaced->Implement Interface. This should add the properties NumberOfTypes, Version and the method create to your class.
  10. In the project class view right click on the project, select Add->Class then enter a name for your tester object and press Add.
    screenshot
  11. Open the tester object source file and add a using statement for TestScriptRunnerLib, make your tester class inherit the interface ITester2, add a bool attribute called isAborted and an ITrace attribute called myTrace.
          using System;
          using System.Collections.Generic;
          using System.Text;
          using TestScriptRunnerLib;
          namespace Demo_CSharp_2005
          {
              class DemoTester : ITester2
              {
                  private bool isAborted = false;
                  private ITrace myTrace = null;
              }
          }
         
  12. Right click on ITester2 and select Implement Interface->Implement Interface. This should add a number of methods/properties to your class.
  13. In the Abort method remove the throw statement and replace it with isAborted = true; the flag can be used in your action method to know when you should abort due to the user pressing the stop button. If you do not have any actions that take a long time you do not need this variable and can leave the method blank but you must remove the throw statement.
          public void Abort()
          {
              isAborted = true;
          }
         
  14. In the get section of ActionCount property change the throw statement with return <number of actions>. In this case we have 4 actions.
          public uint ActionCount
          {
              get { return 4; }
          }
         
  15. In the ActionDocumentation method add a switch for each of your actions to return the description, in and out parameters. This provides on line documentation for your tester in the help menu describing each action along with the in and out parameters. In this case this method is as follows:-
          public void ActionDocumentation(uint ActionNumber, out string Description, out Array inParameters, out Array outParameters)
          {
              switch (ActionNumber)
              {
                  case 0: // pass
                      Description = "Always passes";
                      inParameters = null;
                      outParameters = null;
                      break;
                  case 1: // fail
                      Description = "Always fails";
                      inParameters = null;
                      outParameters = null;
                      break;
                  case 2: // random
                      Description = "Random pass/fail"
                      inParameters = new string[] { "int ratio" };
                      outParameters = null;
                      break;
                  case 3: // randomValue
                      Description = "Random value"
                      inParameters = new string[] { "int ratio" };
                      outParameters = new string[] { "int value" };
                      break;
                  default:
                      throw new Exception("Invalid action number " + ActionNumber);
              }
          }
         
  16. In the ConstructorDocumentation method you should add code to document the parameters and use of the new command for this tester. In this case there are no parameters in the call to new the tester object.
          public void ConstructorDocumentation(out string Description, out Array inParameters)
          {
              Description = "Creates a demo tester";
              inParameters = null;
          }
         
  17. In the Description method you should add code to document the tester.
          public string Description
          {
              get { return "This is a demo tester"; }
          }
         
  18. In the Close method you should add any code that removes any use of hardware resources etc. In the case of demo tester there are no hardware resources so we simply need to return true.
          public bool Close()
          {
              return true;
          }
         
  19. In the Execute method you need to add a switch statement for each action and add code to perform the behaviour you expect for each action. If you need parameters then read them from the parameters array and if you wish to return values add them to the values. Please note when returning values in the values array you MUST create an object array as this is the C# equivalent to a VARIANT array.
          public Result Execute(uint ActionNumber, Array parameters, out Array values)
          {
              values = null;
              switch (ActionNumber)
              {
                  case 0: //pass  
                      return Result.TestPassed;
                  case 1: // fail
                      return Result.TestFailed;
                  case 2: // random
                      if (parameters.Length == 1)
                      {
                          int i = (int)parameters.GetValue(0);
                          if (i > 0)
                          {
                              Random r = new Random();
                              if (r.Next(i) != 0)
                              {
                                  return Result.TestPassed;
                              }
                          }
                          Result.TestFailed;
                      }
                      throw new Exception("Invalid parameters to random action");
                  case 3: // randomValue
                      if (parameters.Length == 1)
                      {
                          int i = (int)parameters.GetValue(0);
                          if (i > 0)
                          {
                              Random r = new Random();
                              values = new object[] { r.Next(i) };
                              return Result.TestPassed;
                          }
                      }
                      throw new Exception("Invalid parameters to random action");
              }
              throw new Exception("Invalid ActionId");
          }
         
  20. In the Open method you need to add any initialisation that cannot be performed in the constructor. In the demo case there is nothing to do so we just return true
          public bool Open(Array parameters)
          {
              return true;
          }
         
  21. In the SetTrace method simple assign the trace object to your myTrace object. If you wish to add logging to your tester you can call the Log method of this object. If you do not wish to add any trace you can leave this method blank and remove the myTrace variable.
          public void SetTrace(ITrace trace)
          {
              myTrace = trace;
          }
         
  22. In the Status method you should return a string that represents the current status of your object. Please note this will be called for a different thread from the Execute method and therefore should you add a string to hold the status it must be protected via a mutex. As the demo tester does not do much the status returned is simply a blank string.
          public string Status
          {
              get { return ""; }
          }
         
  23. In the TesterType method simply return the string that you wish to use in the new statement in the script. In this case this is “demo”.
          public string TesterType
          {
              get { return "demo"; }
          }
         
  24. In the scriptDirectory method save the directory if you wish to perform file operations and need to know where the script was loaded. If you do not need this information then simply remove the exception.
          public void scriptDirectory(script dir)
          {
          }
         
  25. In the Version method you should add a string that represents the version of the tester.
          public string Version
          {
              get { return "2.0"; }
          }
         
  26. In the Actions method add a switch that returns the name of each action.
          public string get_Actions(uint ActionNumber)
          {
              switch (ActionNumber)
              {
                  case 0:
                      return "pass";
                  case 1:
                      return "fail";
                  case 2:
                      return "random";
                  case 3:
                      return "randomValue";
              }
              throw new Exception("Invalidaction number " + ActionNumber);
          }
         
  27. Open the tester creator source and in the NumberOfTypes method return the number of testers you have implemented. In this case we have implemented 1.
          public uint NumberOfTypes
          {
              get { return 1; }
          }
         
  28. The version method should return a string representing the version of the Tester Creator.
          public string Version
          {
              get { "2.0"; }
          }
         
  29. In the create method add a switch for each tester and allocate a new tester of the correct type.
          public ITester create(uint TesterNumber)
          {
              switch (TesterNumber)
              {
                  case 0: 
                      return new DemoTester();
              }
              throw new Exception("Invalid tester number " + TesterNumber);
          }
         
  30. Compile your code and run TestScriptRunner.exe
  31. Open the Admin->Add Testers menu command and add your tester to the system. The text to add is <namespace>.<testercreatorclassname> and in this case it is Demo_CSharp_2005.DemoTesterCreator.

If you wish to debug your code add PluginRunner.exe as the external program in the Debug tab of the project properties and run it. Run a script that calls your tester and you should be able to add break points in the execute method.

If you wish to add logging information to your tester object you can call the log method of the ITrace object provided to your tester via the SetTrace method at any point. All logging entries from configuration to sever will be displayed in the logger view window and in the log file. If you wish to add debug log entries then use levels finest, finer and fine levels and change the logging level via the Logging->Set Logging Level option.

You can download the demo plug-in at DemoTester C# 2005.