Forum Discussion

aldebaran's avatar
aldebaran
Occasional Contributor
14 years ago

get the currently tested Item Name

How can I retrieve the Name of the current test?



My Project is a mix of keyword and scripttests, using vb-script. I handle the OnStart and OnStop Events and would like to access the Project.TestItems.Current.Name Property!! I have found plenty of code-examples to do this, e.g.:  call Log.Message(Project.TestItems.Current.Name)



Trying to do this result in an error: Object required "Project.TestItems.Current". Evaluating the expression Project.TestItems.Current returns a value of type Object however.



Any suggestions are welcome.



thanx. C

  • Hi Carsten,





    It seems you're trying to run just a script or a keyword test. The approach with Project.TestItems.Current.Name will work if you run a test item (or a project containing a list of test items).


  • aldebaran's avatar
    aldebaran
    Occasional Contributor
    Hi Allen,



    when I posted my question, I was sure I had tried out both options: run keywordTest (single), and run same test by clicking run focused item.



    However, it worx now, thanx for Your reply.



    Is it in any way possible to have the currentItem.Name always as part of the indicator text??

    It would be helpful to be able to see how many tests of a test-suite the testexecuter still has to run!!



    greets



    Carsten

  • Hi Carsten,





    Yes, it is. You can do it in the following way:

    1. Create one project temporary variable (type: Integer) - TestRunCount. Set its default value to 0. See the 'Project and Project Suite Editor - Variables Page' help topic for more information.

    2. Add the following code into a script unit:

    function getTestItemsCount(testItem)

    {

      if (null == testItem) {

        testItem = Project.TestItems; 

      }

      else {

        if (false == testItem.Enabled) {

          return 0;

        }

      }

        

      var count = 0;

      for (var i = 0; i < testItem.ItemCount; i++) {

        count += getTestItemsCount(testItem.TestItem(i));

      }

      

      if (IsSupported(testItem, "Count")) {

        count = (count + 1) * testItem.Count;

      }

      

      return count;  

    }






    3. Create an event handler (it should reside in the same unit where getTestItemsCount does) for the OnStartTest event as follows:

    function GeneralEvents_OnStartTest(Sender)

    {

      if(0 == Project.Variables.TestRunCount) {

        count = getTestItemsCount();

      }

      Project.Variables.TestRunCount++;

      Indicator.PushText(Project.TestItems.Current.Name + ", progress: " + Project.Variables.TestRunCount + " of " + count);                

    }






    See the 'Creating Event Handlers in TestComplete' help topic for more information. Information about the Indicator object is available here.