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

In this SoapUI advance tutorial, let’s learn about how to store the response in a file for future reference. Also, the steps are very detailed, so we treat this as a quick recap of everything we do, end-to-end.

This is the last tutorial in our SoapUI free training series. Head over to this page for all the tutorials in this comprehensive SoapUI and SoapUI pro tutorials series.

soapui tutorial 15

Storing Request and Response in a File:

We will start from creating SOAP project:

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

Adding WSDL

Let us add currency convertor WSDL in 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 popup menu, click Add WSDL option. Refer the following screenshot.

exporting data in file 1

3) In the Add WSDL window, enter the currency convertor URL. http://www.webservicex.net/CurrencyConvertor.asmx?WSDL

4) Click OK

5) We can now see the StoreResponseValueInFile project under the project tree along with the interface steps. If you expand a web service (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 could rename it if needed.

Add Test Suites and Test Cases

1) In the project tree, right click on the CurrencyConvertorSoap interface

2) From the popup menu, click 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) Now test suite is 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 click Rename option

Add Groovy steps:

1) Under SampleTestSuite, right click on the SampleTestCase node

2) From the popup menu, click Add Step -> Groovy Script test step

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

4) Groovy script test step is added under 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 ConversionRate request with required input parameters.

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

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

8) Here, replace the following input data instead of 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.

Now we are done storing response data in our local drive.

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

We can also save the request information in a file. For that, do the following.

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

exporting data in file 2

2) SoapUI Pro will give you 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 about 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 request information in the variable as we have seen in the above samples. See the below sample code 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 the entire series. We hope this has been useful to you and brought you closer to your SoapUI learning skills. As always, practice, patience and persistence are the most as important as the tutorials themselves for best results.

As a recap of everything you learned in this SoapUI training series, we will be having one SoapUI interview questions and answers tutorial in the end.

We are here to hear your thoughts, experiences and of course, answer questions should you have any.

Thank you for sticking around with us!




Recommended reading

5 comments ↓

#1 Sheetal

hi I didnt get emails for 14 and 15 tutorial?

#2 Gaurav Khurana

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,,

#3 Santhosh

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…

#4 sujit

i did not understand ,it would be better if you can explain each of test steps by providing snap

#5 carlos vasquez

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

Leave a Comment