Forum Discussion

Novari-QA's avatar
Novari-QA
Frequent Contributor
8 years ago

Get Keyword Test name via script

In a keyword test, you are able to call a script routine, in this script routine I want to be able to capture the name of the keyword test that ran the script routine.  is this possible?

  • Would be nice... but currently not available.  The parameter method you're using is the best bet that I know of.

     

    The only thing you could do differently would be to set some global variable at the start of the keyword test that indicates the name of the keyword test.  Then reference that variable internally in your script.  You could get rid of the parameter, but it's 6 one way, half a dozen the other in the amount of code you need to write.

     

    Feel free to make a feature request... it would be cool to implement some sort of "caller" property internally in TestComplete so that, if you're not using something like JavaScript or you're using keyword test, you can build your current call stack.

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      If you're working with PURE JavaScript and no keyword tests, you can do this (just found this myself with a quick Google search):


      function getCaller(){
          if (getCaller.caller != null) {
          Log.Message('My caller was ' + getCaller.caller.name)
          }
      }
      
      function theCaller(){
          getCaller();
      }

      It's really pretty cool.

       

      However, a KeywordTest is not a JavaScript function... so it doesn't have all those nifty properties that will tell you who called the script function.  The above "getCaller" function, if called from within a keyword test, won't return the keyword test name... in fact, the log message won't even execute because "caller" is null.

       

      This is where something like what we have in our framework works well.  The data structures we have in place that do the test execution actually store the names of each keyword test to be executed along with several other properties in an object within a larger array.  When we get to a test to run, we put that individual element from the array into a "CurrentTestCase" globally scoped variable.  Then, at any point when that test case is running, we can go to that variable and retrieve any information we want from the test case including the name, what parameters are used, what it's current status (PASS/FAIL) is, etc.

       

      This is what Project Items do for you by giving you a built in feature of TestComplete that allows you to get some information about the test item.  So, if each of your keyword tests has a corresponding test item that does the execution, then the Project.TestItems.Current.Name will help you to determine what is currently being executed.  

       

      As for the feature request that I submitted... that's kind of the reverse... more on the lines of, within a script function, be able to use just a string to trigger what keyword test to execute... and our dear friend HKosova gave us a fabulous workaround for that.

       

      function runKeywordTest(testName){
          KeywordTests[testName].Run()
      }
      • Novari-QA's avatar
        Novari-QA
        Frequent Contributor

        I have something similar, the only issue is that:

        in my keyword test i am running a script routine, this script routine takes in a parameter, the parameter is going to be the name of the keyword test. 

        The script will then use that param and do a database call based off the name.  If i could have a script that fetches the name of the current keyword test that is being run, then i could take away that param. Very small improvement, but still would be nice.