I am trying the write a groovy script which get the result from the response of the first testStep and use it into the next testStep.
My web service returns the following response after a POST:
<Response xmlns="http://xxxxxx.xxx.xxxxx.xxx/cal-service/v1/users/">
<individual_id>83ecf411-0e3b-4e6b-9bc4-d4b5f6efed54</individual_id>
</Response>
I am trying to grab the and pass it to my next test in the test suite.
I am new with groovy and soapUI but what I started with is:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def responseHolder = groovyUtils.getXmlHolder("messageExchange.responseContent")
def individualId = responseHolder.getNodeValue("individual_id")
log.info(individualId)
I am getting the following error when i run the test suite and it reaches the groovy script:
I don't see your error in you post, but to grab something from a request - here's how you do it. The groovy script will return the id from your request.
def id = context.expand( '${REST Test Request#ResponseAsXml#declare namespace ns1=\'http://lshlx082a.sys.cigna.com/cal-service/v1/users/'; //ns1:individual_id[1]}' )
return id;
Replace the "REST Test Request" part with the name of your REST test step.
NOTE: I tried this with my own namespace, so I might have cut-and-pasted your namespace and declarations incorrectly. But this is the general approach.
You can use the Property transfer step to transfer values between responces
Related
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.
Is there any way in which I can run a Property Transfer step from a groovy script? Both are in the same test case.
Test case contains the following test steps:
groovy script
soapUI request (GetAccountNumber)
property transfer step (transfers a response property from above to a request property in the below step)
soapUI request (DownloadURL)
I need to make sure that the flow is as follows:
Groovy runs and reads numbers from a file and passes them to GetAccountNumber.
GetAccountNumber runs with the passed values and generates a response.
This response is passed by the property transfer step to DownloadURL.
DownloadURL runs with this passed value and generates an output.
All I need to do is run the Property Transfer from the groovy because the other steps can be run from groovy.
It isn't running with the following code
def testStep_1 = testRunner.testCase.getTestStepByName("PropertyTransfer")
def tCase_1 = testRunner.testCase.testSuite.testCases["SubmitGenerateReport"]
def tStep_1 = tCase.testSteps["PropertyTransfer"]
tStep_1.run(testRunner, context)
Without more context I think that your problem is a simple typo, you get your testCase and assing to tCase_1:
def tCase_1 = testRunner.testCase.testSuite.testCases["SubmitGenerateReport"];
However then to get the tStep_1 you use tCase instead of tCase_1:
def tStep_1 = tCase.testSteps["PropertyTransfer"]; tStep_1.run(testRunner, context);
Additionally if the testStep you want to run from groovy are in the same testCase you're executing; you can run it simply using:
testRunner.runTestStepByName('some teststep name')
Which I think it's more convenient than get the testStep from the testCase and then run it.
Hope it helps,
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.
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,
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.