WsdlTestRunContext example for our sample project

Example with our own application

In TestSuite2 let us make a new test case Login and in it add a new Soap Request login. In TestSuite 2 let us make a new test case Get All Products and in it add a new Soap Request Get All Products. In TestSuite 2 let us make a new test case Get All Categories and in it add a new Soap Request Get All Categories.

Then to run login request from the test case in which Get All Categories request is lying, let us add a groovy test step in that test case and name the groovy test step as GetSessionId and in that groovy reuqest we add the following code

import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
import com.eviware.soapui.support.XmlHolder;
log.info "In the test step for Get All Products"
tSuite = testRunner.testCase.testSuite;//go to TestSuite Level
loginStep =  tSuite.testCases["Login"].testSteps["login"];//From Test Case Login ,go to test step login
def logintestStepContext  =  new WsdlTestRunContext(loginStep);//Then we get text context object

response = logintestStepContext.expand('${login#response}')
//Now the issue is if we put this groovy script step in various test cases like Get All Products ,Get All Categories
//etc it will run every time and we will get different session ids for all the various steps.
//To solve this issue we use the if statement that is execute the login request only  if response is empty
//And if the login has run at least once we simply use that session id.
//so in this way all of our test cases will have same session id.
if(response == ''){
    loginStep.run(testRunner,logintestStepContext);
    response = logintestStepContext.expand('${login#response}')
    }
 
def xml  =  new  XmlHolder(response);
sessionid = xml.getNodeValue('//sessionid');
log.info sessionid

Now if we run all the test cases in a batch i.e we click the test suite and run and check the SoapUi logs (in C:\Program Files (x86)\SmartBear\SoapUI-5.3.0\bin) the login will not run again for Get All Categories as it has already run for Get All Products.