Groovy script to read test case name in parasoft

SrivasS5SrivasS5 Posts: 6

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

  • jakubiakjakubiak Posts: 314 ✭✭✭

    Are you doing this from an Extension Tool?

  • jakubiakjakubiak Posts: 314 ✭✭✭
    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());
    }
    
  • SrivasS5SrivasS5 Posts: 6

    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;

    }

  • jakubiakjakubiak Posts: 314 ✭✭✭
    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.

  • bprabhbprabh Posts: 5

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

  • jakubiakjakubiak Posts: 314 ✭✭✭

    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();
    }
    
Sign In or Register to comment.