A Quick SoapUI Guide to Store Request and Response Data in a File – SoapUI Tutorial #15

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated February 25, 2024

This tutorial is a Quick SoapUI Guide to store request and response data in a file. We will learn how to store the responses in a file for future reference. 

This is the last tutorial in our SoapUI free training series. Click on this link for all the previous tutorials in this comprehensive SoapUI and SoapUI pro tutorials series. Also, since the steps are very detailed, we treat this as a quick recap of everything we do, end-to-end.

A Quick SoapUI Guide to Store Request and Response Data in a File

soapui tutorial 15

Storing Request and Response in a File

We will start by creating the SOAP project

  1. Open SoapUI Pro
  2. In the Navigator panel, right click on the Project
  3. From the context menu, click on the New SOAP Project option.(You can also use Ctrl+N)
  4. A New SOAP Project dialogue appears on the screen.
  5. In the new SOAP Project window, enter the project name, StoreResponseValueInFile.
  6. click OK
  7. The project has now been created successfully without any test steps as we did not include the WSDL document

Adding WSDL

Let us add the currency converter WSDL to this section.

1) In the project tree, right click on the project name that was created in the previous section. i.e. StoreResponseValueInFile

2) In the pop-up menu, click the Add WSDL option. Refer to the following screenshot.

exporting data in file 1

3) In the Add WSDL window, enter the currency converter URL.

4) Click OK

5) We can now see the StoreResponseValueInFile project under the project tree along with the interface steps. If you expand web services (i.e. ConversionRate), there will be a node known as Request1 by default.

6) Double click on the Request1 node to view the request XML content. You can rename it if needed.

Add Test Suites and Test Cases

  1. In the project tree, right-click on the CurrencyConvertorSoap interface.
  2. From the pop-up menu, click the Generate Test Suite option.
  3. Leave the default settings in and click OK.
  4. Enter the test suite name in the given text field, SampleTestSuite, and click OK.
  5. The test suite is now created along with the test case name ConversionRate TestCase.
  6. Rename the test case name as SampleTestCase by right-clicking on the test case name and clicking on the Rename option.

Add Groovy Steps

1) Under the sample test suite, right click on the sample test case node

2) From the pop-up menu, click on the Add Step -> Groovy Script test step

3) Enter the name of the groovy script as GetResponseValue and click OK

4) Groovy script test steps are added to the test suite and redirected to the script editor.

5) In the editor, add the following script.

def response = context.expand( ‘${ConversionRate – Request 1#Response}’ )
new File( “D:/Groovy/” + “_response.txt” ).write( response )

Note: You should have executed the ConversionRate request with the required input parameters.

6) Double click on the SampleTestSuite -> SampleTestCase -> TestSteps(2) -> ConversionRate – Request1

7) Click on the XML tab from the Vertical tab bar

8) Here, replace the following input data instead of a question mark (?) in the soap code.

  • FromCurrency = USD
  • ToCurrency = INR

9) Double click on SampleTestSuite -> SampleTestCase -> TestSteps(2) -> GetResponseValue

10) Now Run the TestSuite by clicking on this icon.

We are now done storing the response data in our local drive.

Verify the responses in the response tab and also in the hard drive to ensure both responses are identical.

We can also save the requested information on file. To do that, do the following:

1) Right-click on the script editor, and then click on the Get Data -> Step 1 [ConversionRate] -> Property [Request] option. Refer to the following screenshot.

exporting data in file 2

2) SoapUI Pro will give you an auto-generated code like this.

def request = context.expand( ‘${ConversionRate#Request}’ )

3) Then add the following script in the next line

new File( “D:/groovy/” + “_request.txt” ).write( request )

If we execute this script, we will get a file with the name of _request.txt in our local drive as mentioned in the script.

Here are some tips on storing raw request through groovy scripting. Take a look at the following sample scripts.

testCase.getTestStepByName(<teststep name>).getProperty(“Request”).getValue()
Or
context.testCase.getTestStepAt(<index>).getProperty(“Request”).getValue()
Or
testRunner.testCase.getTestStepAt(<index>).getProperty(“Request”).getValue()

The above scripts are used to get the raw web service request while executing the test step. Following the script, we can write another line to receive the request information in the variable as we have seen in the above samples. See the sample code given below to understand this better.

def request = testCase.getTestStepByName(“ConversionRate”).getProperty(“Request”).getValue()
new File( “D:/GroovyRequest/” + “RawRequestData.txt” ).write( request )

Conclusion

That brings us to an end, not only of this article, but also the entire series. We hope this series has been useful to you and has helped advance your SoapUI learning skills. As always, the key is to practice more. Patience and persistence are as important as the tutorials themselves for achieving success.

To recap everything you learned in this SoapUI training series, we will be having one SoapUI interview question and answer tutorial at the end.

We would love to hear your thoughts, experiences and of course, answer questions should you have any. Feel free to post your views in the comments section below.

Thank you for sticking around with us!

Was this helpful?

Thanks for your feedback!

Recommended Reading

8 thoughts on “A Quick SoapUI Guide to Store Request and Response Data in a File – SoapUI Tutorial #15”

  1. I get the following error when I run the TestSuite “GetResponseValue”. The script is as follows:

    ## groovy script ##

    def response = context.expand( ‘${ConversionRate – Request 1#Response}’ )
    new File( “C:\Users\cvasquez\Pictures\Groovy\Groovy” + “_response.txt” ).write( response )

    ## error ##

    org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script3.groovy: 1: unexpected char: ‘#’ @ line 1, column 61. ‘${ConversionRate – Request 1#Response} ^ org.codehaus.groovy.syntax.SyntaxException: unexpected char: ‘#’ @ line 1, column 61. at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:135) at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:108) at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236) at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:162) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:912) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:574) at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:550) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:527) at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:279) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:258) at groovy.lang.GroovyShell.parseClass(GroovyShell.java:613) at groovy.lang.GroovyShell.parse(GroovyShell.java:625) at groovy.lang.GroovyShell.parse(GroovyShell.java:652) at groovy.lang.GroovyShell.parse(GroovyShell.java:643) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.compile(SoapUIGroovyScriptEngine.java:136) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:87) at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:141) at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:250) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: Script3.groovy:1:61: unexpected char: ‘#’ at org.codehaus.groovy.antlr.parser.GroovyLexer.nextToken(GroovyLexer.java:709) at org.codehaus.groovy.antlr.parser.GroovyLexer$1.nextToken(GroovyLexer.java:258) at groovyjarjarantlr.TokenBuffer.fill(TokenBuffer.java:69) at groovyjarjarantlr.TokenBuffer.LA(TokenBuffer.java:80) at groovyjarjarantlr.LLkParser.LA(LLkParser.java:52) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.commandArgumentsGreedy(GroovyRecognizer.java:10284) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.expressionStatementNoCheck(GroovyRecognizer.java:8361) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.expressionStatement(GroovyRecognizer.java:8899) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.statement(GroovyRecognizer.java:1274) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.blockBody(GroovyRecognizer.java:1495) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.closableBlock(GroovyRecognizer.java:8778) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.appendedBlock(GroovyRecognizer.java:11554) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.pathElement(GroovyRecognizer.java:11506) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.pathExpression(GroovyRecognizer.java:11621) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.postfixExpression(GroovyRecognizer.java:13332) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.unaryExpressionNotPlusMinus(GroovyRecognizer.java:13301) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.powerExpressionNotPlusMinus(GroovyRecognizer.java:13005) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.multiplicativeExpression(GroovyRecognizer.java:12937) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.additiveExpression(GroovyRecognizer.java:12607) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.shiftExpression(GroovyRecognizer.java:9824) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.relationalExpression(GroovyRecognizer.java:12512) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.equalityExpression(GroovyRecognizer.java:12436) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.regexExpression(GroovyRecognizer.java:12384) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.andExpression(GroovyRecognizer.java:12352) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.exclusiveOrExpression(GroovyRecognizer.java:12320) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.inclusiveOrExpression(GroovyRecognizer.java:12288) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.logicalAndExpression(GroovyRecognizer.java:12256) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.logicalOrExpression(GroovyRecognizer.java:12224) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.conditionalExpression(GroovyRecognizer.java:4850) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.assignmentExpression(GroovyRecognizer.java:8027) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.expression(GroovyRecognizer.java:10001) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.strictContextExpression(GroovyRecognizer.java:9239) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.argument(GroovyRecognizer.java:13928) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.argList(GroovyRecognizer.java:6751) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.methodCallArgs(GroovyRecognizer.java:11910) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.pathElement(GroovyRecognizer.java:11491) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.pathExpression(GroovyRecognizer.java:11621) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.postfixExpression(GroovyRecognizer.java:13332) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.unaryExpressionNotPlusMinus(GroovyRecognizer.java:13301) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.powerExpressionNotPlusMinus(GroovyRecognizer.java:13005) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.multiplicativeExpression(GroovyRecognizer.java:12937) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.additiveExpression(GroovyRecognizer.java:12607) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.shiftExpression(GroovyRecognizer.java:9824) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.relationalExpression(GroovyRecognizer.java:12512) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.equalityExpression(GroovyRecognizer.java:12436) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.regexExpression(GroovyRecognizer.java:12384) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.andExpression(GroovyRecognizer.java:12352) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.exclusiveOrExpression(GroovyRecognizer.java:12320) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.inclusiveOrExpression(GroovyRecognizer.java:12288) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.logicalAndExpression(GroovyRecognizer.java:12256) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.logicalOrExpression(GroovyRecognizer.java:12224) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.conditionalExpression(GroovyRecognizer.java:4850) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.assignmentExpression(GroovyRecognizer.java:8027) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.expression(GroovyRecognizer.java:10001) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.expressionStatementNoCheck(GroovyRecognizer.java:8353) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.varInitializer(GroovyRecognizer.java:2682) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.variableDeclarator(GroovyRecognizer.java:7928) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.listOfVariables(GroovyRecognizer.java:7882) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.variableDefinitions(GroovyRecognizer.java:2278) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.declaration(GroovyRecognizer.java:2140) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.statement(GroovyRecognizer.java:1208) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.compilationUnit(GroovyRecognizer.java:666) at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:131) … 20 more 1 error

    Reply
  2. testCase.getTestStepByName(“ConversionRate”).getProperty(“Request”).getValue()
    new File( “D:/GroovyRequest/” + “RawRequestData.txt” ).write( request )

    in the above expression what is “Request” refering to?
    from where we want to get it…
    Pls…help me…it is very helpful to me…
    thanxs in advance…

    Reply
  3. This is one of the best tutorial where we can save data for all the request and response received..

    Now i can add SOAP to my resume and with practice it will be better. thanks STH for this series

    whats the difference between context and testrunner
    and for me
    testCase.getTestStepByName().getProperty(“Request”).getValue()
    did not worked btu testRunner and context worked, any reason ?

    Hoping for more tutorials for tips and tricks for SOAP,,

    Reply
  4. Those public webservices no longer exist. Check out YouTube – search on SoapUI (free version) Excel Data-Driven Framework using Assertions to test webservices

    Reply

Leave a Comment