Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Testsuitename dynamically

Options
daruna1
daruna1 Posts: 8

Hello All

Is there a way, one could retrieve the testSuiteName, thru a variable, dynamically, which can be leveraged in all the scripts for logging purpose.

Anon

Comments

  • jakubiak
    jakubiak Posts: 804 admin
    edited March 2020
    Options

    There is no variable that has the name. However, you can retrieve it in a script. Here is an example script that allows you to get the test name or test suite name in an Extension Tool using groovy:

    import com.parasoft.api.*
    
    void test(Object input, ScriptingContext context) {
        Application.showMessage(input.toString())
        Application.showMessage("test: " + context.getParentContext().getContextName())
        Application.showMessage("test suite: " + context.getParentContext().getParentContext().getContextName())
    }
    

    You could add additional "getParentContext()" calls to get parent test suites if you have a lot of nested test suites.

  • daruna1
    daruna1 Posts: 8
    Options

    Yes, created an Extension Tool, with the above Groovy Script but unfortunately not getting the Name of TestSuite like (GetGeoLocationAPI).

    ---------- Test 1: Extension Tool ----------

    test: Extension Tool
    test suite: Test Suite

  • jakubiak
    jakubiak Posts: 804 admin
    Options

    Your script actually did get the name of the test suite - your test suite still has the default name of "Test Suite". Instead of the name of the test suite, are you looking for the name of the .tst file?

  • daruna1
    daruna1 Posts: 8
    Options

    Thanks that worked. Sorry about that. But if I were to do a Jython equivalent of that, and with a Data Generator Tool with a string will it not work.

    from java.text import *

    from java.util import *

    def getmyID():

    # Get an instance of the calendar
    calendar = Calendar.getInstance()
    
    # Set the hour in the calendar
    hour = calendar.get(Calendar.HOUR_OF_DAY)
    calendar.set(Calendar.HOUR_OF_DAY, hour)
    
    # Retrieve the date from the calendar
    date = calendar.getTime()
    
    # Create the date formatter
    myFormat ="yyyyMMddHHmmssSSS"
    
    formatter = SimpleDateFormat(myFormat)
    
    # Create the date and time stamp, and print and return the name value
    timestamp = formatter.format(date)
    
    name ="{Auditing}{" + context.getParentContext().getContextName() +"}{" + timestamp + "}"  
    
    print name
    return name
    
  • jakubiak
    jakubiak Posts: 804 admin
    Options

    Currently your method declaration looks like this:

    def getmyID():

    Instead it should look like this:

    def getmyID(input, context):

    I'm not sure what error you get when you run, but I suspect the code is failing when trying to reference the context variable since you didn't declare it as input to your script.

  • daruna1
    daruna1 Posts: 8
    Options

    from java.text import *

    from java.util import *

    from com.parasoft.api import *

    def getmyID(input, context):

    # Get an instance of the calendar
    calendar = Calendar.getInstance()
    
    # Set the hour in the calendar
    hour = calendar.get(Calendar.HOUR_OF_DAY)
    calendar.set(Calendar.HOUR_OF_DAY, hour)
    
    # Retrieve the date from the calendar
    date = calendar.getTime()
    
    # Create the date formatter
    myFormat ="yyyyMMddHHmmssSSS"
    formatter = SimpleDateFormat(myFormat)
    
    # Create the date and time stamp, and print and return the name value
    timestamp = formatter.format(date)
    apiname = context.getParentContext().getParentContext().getContextName()
    
    name ="{Auditing}{" + apiname + "}{" +  timestamp + "}"  
    

    print name
    return name

    returns an Error Message "Unable to invoke user method getmyID(). Argument count should be 0 or 1 instead of 2."

  • jakubiak
    jakubiak Posts: 804 admin
    Options

    Then change the signature to def getmyID(context) and try again.

  • daruna1
    daruna1 Posts: 8
    Options

    from java.text import *
    from java.util import *
    from com.parasoft.api import *

    def getMyID(context):

    # Get an instance of the calendar
    calendar = Calendar.getInstance()
    
    # Set the hour in the calendar
    hour = calendar.get(Calendar.HOUR_OF_DAY)
    calendar.set(Calendar.HOUR_OF_DAY, hour)
    
    # Retrieve the date from the calendar
    date = calendar.getTime()
    
    # Create the date formatter
    myFormat ="yyyyMMddHHmmssSSS"
    formatter = SimpleDateFormat(myFormat)    
    
    # Create the date and time stamp, and print and return the name value
    timestamp = formatter.format(date)
    
    apiname = context.getParentContext().getParentContext().getContextName()
    name = apiname
    
    print name
    return name
    

    File "", line 43, in getMyID
    AttributeError: 'NoneType' object has no attribute 'getContextName'

  • jakubiak
    jakubiak Posts: 804 admin
    edited March 2020
    Options

    It appears that you are not running this script from an Extension Tool. Where are you inputting this script? It may need to be adjusted based on where it is running.