groovypropertiessoapuigetpropertygetproperties

SoapUI & Groovy - How to get properties from different TestCases using Run testCase


Sorry in advance. I am sure this is not a new question but I swear I've been looking for days and have tried several solutions but no one fits exactly what I need. I hope you guys can help me...

My problem:

  1. I have a main script which sends bash commands to our server:
TestSuite: Tools;
   TestCase: sendBashCommands;
      TestStep: groovycript;
  1. This test script is called by several test cases using "Run testCase". Each test case have a different bash command:
TestSuite: ServerInfo
   TestCase: getServerVersion
      Property: BashCommand="cat smt | grep Version"
      TestStep: Run sendBashCommands

   TestCase: getServerMessage
      Property: BashCommand="cat smt | grep Message"
      TestStep: Run sendBashCommands
   ...

On my sendBashCommands.groovyScript I have already tried the following:

//def bashCmd= context.expand( '${#BashCommand}' );
     //it returns empty;
//def bashCmd= testRunner.testCase.getPropertyValue( "BashCommand" );
     //it returns null;
//def bashCmd= context.getTestCase().getPropertyValue( "BashCommand" );
     //it returns null;
def bashCmd = context.expand('${#TestCase#BashCommand}');
     //it also returns empty;

Currently I use a solution which works with project properties but what I actually need is working with these properties on the level of the test case which is calling the sendBashCommand script. Is that possible? How could I do it?


Solution

  • I think you are doing it for maintenance purpose... As per your approach, the result has to come as null or empty. Because your groovyscript cannot get the properties of other testcases directly. If you call a getProperty() Method directly, then it will refer to the current testStep's property. So the following approach could help you.

    put the following code in your individual test cases, "getServerVersion" etc.

    def currentProject = testRunner.testCase.testSuite.project
    // the following is to store the names of the test suite and the test cases from which you want to call your main script
    
    currentProject.setPropertyValue("TestSuiteName",testRunner.testCase.getTestSuite().getLabel().toString())
    currentProject.setPropertyValue("TestCaseName", testRunner.getTestCase().getLabel().toString())
    // call sendBashCommands here -> run sendBashCommands
    

    put this code in your main groovy script (sendBashCommands.groovyscript)

    def currentProject = testRunner.testCase.testSuite.project
    def callingTestCase = currentProject.testSuites[currentProject.getPropertyValue("TestSuiteName")].testCases[currentProject.getPropertyValue("TestCaseName")]
    def bashCmd = callingTestCase.getPropertyValue( "BashCommand" )
    // to verify
    log.info bashCmd 
    

    The project is common to all the test suites, so you have to use project property in any case, i.e., for your requirement :) Enjoy :)