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.

Groovy script to read test case name in parasoft

SrivasS5
SrivasS5 Posts: 10

Hi,
I am using groovy script for reading of parameter from the data source and creating of folder at the same location. I am able to do this much but now I want my folder name to be same as my test case name. Therefore can you please tell me what should be the groovy script **to read the **test case name from my test suite so that the folder could be created with the same name.

Answers

  • jakubiak
    jakubiak Posts: 759 admin

    Are you doing this from an Extension Tool?

  • jakubiak
    jakubiak Posts: 759 admin
    edited November 2017

    If you are doing it from an Extension Tool, you can use a script like the following:

    import com.parasoft.api.*;
    
    public void printName(Object input, Context context) {
        Application.showMessage("test name: " + context.getContextName());
    }
    
  • SrivasS5
    SrivasS5 Posts: 10

    hi,
    Thanks for the script.
    I used the same in my groovy script , but it is still not reading the test name from parasoft.
    Please find my groovy script and please tell me what should I add to read the test name and create folder with the same name at the location provided in my data source.

    import static java.util.Calendar.*;
    import java.text.SimpleDateFormat;
    import javax.xml.datatype.XMLGregorianCalendar;
    import java.text.DateFormat;
    import java.lang.String;
    import java.util.regex.Pattern;
    import java.sql.Timestamp;
    import com.parasoft.api.*;

    String path(input, context){
    def sdf = new java.text.SimpleDateFormat("dd-MM-yyyy hh-mm-ss aa");
    def SysDate = sdf.format( new Date()) ;
    value = context.getValue("HeaderValues", "FolderPath")
    def Name1=Application.showMessage("test name: " + context.getContextName());
    String runFolder = value+Name1+SysDate;
    def runFolderDir= new File(runFolder);
    runFolderDir.mkdir();

    return runFolder;

    }

  • jakubiak
    jakubiak Posts: 759 admin
    edited November 2017

    Your script is not quite correct - you are setting the value of "Name1" to the output of Application.showMessage() - which is a void function that doesn't return anything. Change that line to the following two lines:

    def Name1=context.getContextName();
    Application.showMessage("test name: " + Name1);
    

    context.getContextName() will give you the name of the test when running from an Extension Tool, but may not if you are running it from a script in a different tool. So if you're not using an Extension Tool, please let me know where you're inputting the script and I can help you modify it to get the test name.

  • bprabh
    bprabh Posts: 5

    is there a way to read the name of the scenario thats executing?

  • jakubiak
    jakubiak Posts: 759 admin

    SOAtest scenarios are structured in a hierarchy of test suites (scenarios). You can use the Context objects to get information about them. Here is some code that will print out information that helps you to see how the contexts are structured, as well as methods that help you get the name for the parent test suite and the name for the top-level test suite. Put this code into an Extension Tool:

    import com.parasoft.api.*;
    import java.lang.*;
    
    public void printTestNames(Object obj, Context context) {
        Application.showMessage("top level scenario: " + getTopLevelScenarioName(context));
        Application.showMessage("parent scenario: " + getParentScenarioName(context));
        printContexts(context);
    }
    
    public void printContexts(Context context) {
        Application.showMessage("print all contexts");
        while (context != null) {
            Application.showMessage("    name: " + context.getContextName());
            Application.showMessage("    type: " + context.getContextType());
            context = context.getParentContext();
        }
    }
    
    public String getTopLevelScenarioName(Context context) {
        Context previous = null;
        while (context != null && !context.getContextType().equals("Application")) {
            previous = context;
            context = context.getParentContext();
        }
        return previous != null ? previous.getContextName() : "";
    }
    
    public String getParentScenarioName(Context context) {
        return context.getContext("TestSuite").getContextName();
    }
    
  • goofy78270
    goofy78270 Posts: 128

    Sorry for the addition here, but I thought it fell in the same context.

    With the above, is it also possible to get the location of the testsuite (ie...${test_suite_loc})

  • benken_parasoft
    benken_parasoft Posts: 1,062 ✭✭✭

    With the above, is it also possible to get the location of the testsuite

    I'm not sure if there's public (non-internal) scripting API to get this. The closest thing I found is this:
    com.parasoft.api.ScriptingContext.getAbsolutePathFile(String fileName).

    Passing "" or "." for the "fileName" should get you the folder containing the tst file but not the tst file itself. Otherwise, you can try putting ${test_suite_loc} in some field where it is likely get resolved in a way that you can read it later, like maybe on the Input tab of an Extension Tool.

  • dgoedh
    dgoedh Posts: 50

    Hi, the script above from Jakuibiak is written in java. Is there also a Jython version available, for e.g. the getParentScenarioName method, among others?

    Thanks in advance.

    Regards,

    Daniel

  • benken_parasoft
    benken_parasoft Posts: 1,062 ✭✭✭

    the script above from Jakuibiak is written in java

    It's actually written for the Groovy script engine. Can you use Groovy?

    Is there also a Jython version available, for e.g. the getParentScenarioName method, among others?

    No, but it shouldn't be hard for someone to translate. If you don't want to translate it yourself then you may just want to run it as Groovy instead.