SOAP UI Reporting - groovy

I am using open Source version of SOAPUI.
My requirement is to export SOAP Response XML into a file (e.f txt). I could not find any such option in open source for this purpose. Please suggest if there is any way to do it or Groovy scripting is the solution. If yes then how.

If you want to save a simple SOAP Test step response, you can add a groovy script with this code to do so:
// get your response
def soapResponse = context.expand('${YourSOAPRequest#Response}')
// create the file
def file = new File("C:/Temp/testSO/response.xml")
file.mkdirs()
// save the response
file.write(soapResponse)
Note that YourSOAPRequest is the name of your SOAP Test step.
Additionally if you want to save all the test step responses of the Test suite or the Test case this answer could help you: Unable to save TestSuite Response Result in SOAP UI

You could run your tests from the command line, with the -a or -A switch.

Thanks to #albciff for guiding me through the answer. I also figured out a way for situtaion when rather than a static SOAP request name we have a variable:
def soapResponse= testRunner.testCase.getTestStepByName(testStepName).getProperty("Response").getValue()
Now this response can be used to save

Related

Print the request step response in groovy script step using variable

I have a requirement in which I need to print XML response in Groovy Script test step
Name of SOAP Request test step is Licensed
When I write
log.info context.expand('${Licensed#Response}')
I get right response
But I have a requirement where user is not aware of the code
def requestname=Licensed //user will enter request name here
log.info context.expand($'{requestname"#Response}')
I don't get valid response
I want to declare a variable and use and print xml response
Here is what you need in order use the step name as parameter / variable. You have a trivial error in your code snippet.
//You missed quotes
def requestname='Licensed'
//Your were using quotes incorrectly
log.info context.expand('${'+requestname+'#Response}')
Hope this resolves your issue.

SoapUI Groovy Script’s JSON Responses Is Empty When Using Testrunner

Using Windows 7 with Soap 5.2.0 freeware.
I also asked about this in the Smart Bear community and was only given recommended posts to read. The posts didn’t relate to this problem.
I have a REST project that has one test suite with one test case containing two test steps. The first step is a groovy step with a groovy script that calls the second test step. The second test step is a REST GET request that sends a string to our API server and receives a response back in JSON format. The second test step has a script assertion that does "log.info Test Is Run", so I can see when the second test is run.
When the groovy script calls the second test step it reads the second test step’s JSON response in the groovy script like this:
def response = context.expand('${PingTest#Response}').toString() // read results
I can also use this for getting JSON response:
def response = testRunner.testCase.getTestStepByName(testStepForPing).getPropertyValue("response")
The project runs as expected when run through the Soap UI but when I run the project with test runner, the response from the groovy script call to get the JSON response is empty, using either of the methods shown above. When run from testrunner, I know the second test step is being called because I see the log.info result in the script log.
This is part of the DOS log that shows the second test step is running and it seems there are no errors for the second test step run.
SoapUI 5.2.0 TestCase Runner
12:09:01,612 INFO [WsdlProject] Loaded project from [file:/C:/LichPublic/_Soap/_EdPeterWorks/DemoPing.xml]
12:09:01,617 INFO [SoapUITestCaseRunner] Running SoapUI tests in project [demo-procurement-api]
12:09:01,619 INFO [SoapUITestCaseRunner] Running Project [demo-procurement-api], runType = SEQUENTIAL
12:09:01,628 INFO [SoapUITestCaseRunner] Running SoapUI testcase [PingTestCase]
12:09:01,633 INFO [SoapUITestCaseRunner] running step [GroovyScriptForPingtest]
12:09:01,932 INFO [WsdlProject] Loaded project from [file:/C:/LichPublic/_Soap/_EdPeterWorks/DemoPing.xml]
12:09:02,110 DEBUG [HttpClientSupport$SoapUIHttpClient] Attempt 1 to execute request
12:09:02,111 DEBUG [SoapUIMultiThreadedHttpConnectionManager$SoapUIDefaultClientConnection] Sending request: GET /SomeLocation/ABC/ping?echoText=PingOne HTTP/1.1
12:09:02,977 DEBUG [SoapUIMultiThreadedHttpConnectionManager$SoapUIDefaultClientConnection] Receiving response: HTTP/1.1 200
12:09:02,982 DEBUG [HttpClientSupport$SoapUIHttpClient] Connection can be kept alive indefinitely
12:09:03,061 INFO [log] **Test Is Run**
This is the testrunner call I use in DOS command line:
“C:\Program Files\SmartBear\SoapUI-5.2.0\bin\testrunner.bat" DemoPing.xml
When the groovy script is run through test runner I get the project using ProjectFactoryRegistry and WsdlProjectFactory. Any advice on why I can’t read JSON response when using testrunner would be appreciated.
I can provide more info/code if needed.
Thanks.
Please try the below script:
import groovy.json.JsonSlurper
//provide the correct rest test step name
def stepName='testStepForPing'
def step = context.testCase.getTestStepByName(stepName)
def response = new String(step.testRequest.messageExchange.response.responseContent)
log.info response
def json = new JsonSlurper().parseText(response)
Thank you Rao! Your suggestion worked when I used it as shown below. The DOS window showed the response text:
// setup stepName as variable for name of test step to run.
def stepName = "PingTest"
// use stepName to get the test step for calling the test step (testCase is a
string variable of the test case name).
def step = context.testCase.getTestStepByName(stepName)
// call the test step.
step.run(testRunner, context)
// show the results.
def response = new String(step.testRequest.messageExchange.response.responseContent)
log.info response // this response shows correctly in the DOS window
The json slurper also works. At your convenience, if you have any suggested links or books describing the technique(s) you used here please let me know of them.
Thanks.

Reading External XML in SOAP UI open Source and using it as Request

I am using SOAPUI FREE. My project requirement is to pick Request XML (as we may have in hundreds) from a location and use it as it is as a request. Is it possible using any feature or Groovy Scripting in Free version
If you have SOAP xml requests in some directory and you want to pick each file from there and create a new TestStep for each one, you can do the follow:
Create a new TestCase and add a new SOAP TestStep inside which will be used as a template to easily create the new ones, then add a a groovy TestStep an use the next code to create the new tests steps in the same TestCase (I put the comments in the code to explain how it works):
import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory
// get the current testCase to add testSteps later
def tc = testRunner.testCase;
// get the SOAP TestStep as template to create the other requests
def tsTemplate = tc.getTestStepByName("MyRequest");
// create the test step factory to use later
def testStepFactory = new WsdlTestRequestStepFactory();
// now get all the request from a specific location...
// your location
def directory = new File("C:/Temp/myRequests/")
// for each file in the directory
directory.eachFile{ file ->
// use file name as test step name
def testStepName = file.getName()
// create the config
def testStepConfig = testStepFactory.createConfig( tsTemplate.getTestRequest(), testStepName)
// add the new testStep to TestCase
def newTestStep = tc.insertTestStep( testStepConfig, -1 )
// set the request from the file content
newTestStep.getTestRequest().setRequestContent(file.getText())
};
I think that you're asking about the SOAP TestStep however note that this code is to create SOAP TestStep request, to create REST TestStep request or other kind of TestStep you must change the code related to the testStepFactory (WsdlTestRequestStepFactory).
Furthermore for me it's not clear in your question if you're asking about to create a test steps for each request or if you prefer to run all requests from a groovy script without create the test steps, if the second one is your intention you can use in groovy script the apache-http-client classes which are included in the SOAPUI to send the request from your directory.
Hope this helps,

Get Header from Request in SoapUI groovy script

I want to get the value of the "Accept" Header from a SoapUI request. Then I want to store it in a TestCase property.
That is what I'm trying to do from a Groovy script TestStep:
//Get Accept Header from request (if Accept Header does not exist default to empty string)
def acceptHeader = context.testCase.getTestStepAt(0).testRequest.requestHeaders.get("Accept", "")
//Set Accept Header Value to current TestCase properties
testRunner.testCase.setPropertyValue("acceptHeaderSet", acceptHeader)
The first TestStep of the TestCase is a REST Request and the second TestStep is the mentioned script.
Each time that I run the TestCase the default value is set (it seems that it does not find any header)
Any idea about what is happening? Is this a bug in the SoapUI tool?
Thanks in advance.
This works for me:
testRunner.getResults()[0].getRequestHeaders()["Accept"]
This will only work if you run the entire testcase, otherwise there are no results to get. Maybe your method ran into a similar issue.
I would try to settle such situation using properties. Just define property (e.g. on project level) and then change its value. Like described here Script access to properties

How to use XmlSlurper in soapUI

I have the below groovy script which I run in groovyconsole and it runs just fine. I'm finding the number of child nodes for a particular node in my xml response and printing out required values for each child node.
def path = new XmlSlurper().parse(new File('C://SoapUI//ResponseXML/Response.xml'))
NumberOfPositions = path.Body.GetPositionsAggregateResponse.GetPositionsAggregateResult.AccountPositions.Securities.Positions.children().size()
for(def i=0; i<NumberOfPositions; i++){
println i
println path.Body.GetPositionsAggregateResponse.GetPositionsAggregateResult.AccountPositions.Securities.Positions.PositionSummary[i].Legs[0].PositionAggregate[0].PositionID[0].text()
println path.Body.GetPositionsAggregateResponse.GetPositionsAggregateResult.AccountPositions.Securities.Positions.PositionSummary[i].Legs[0].PositionAggregate[0].AccountID[0].text()
}
I want to perform the same task in soapUI, but couldn't get it working using groovyutils as mentioned here : http://www.soapui.org/Scripting-Properties/tips-a-tricks.html
1) How do I parse the xml response from my request to xmlSlurper?
def path = new XmlSlurper().parse (?)
2) Would I be able to use the same code above in soapUI too?
Any help is appreciated. Thanks!
(1)
For parsing the response message you could try the following:
def response = context.expand( '${TestRequest#Response}' )
def xml = new XmlSlurper().parseText(response)
TestRequest represents the name of your test step which is sending the SOAP request message.
(2)
Yes, soapUI should be able to handle any Groovy code.
You can directly use normal groovy script in SoapUI. Check this link, it might help you. But, remember that instead of "println' You need to use "log.info" while scripting in SoapUI.

Resources