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.
Related
I have isolated all the request alone in One Test Suite. I need to collect each api call time Taken..
Every Functional Test Case will call Request Test Suite using Run Test Case option.
I have following code in the script assertion of each request.
def endpoint = messageExchange.getEndpoint()
uri = endpoint.replace("baseurl","")
log.info("Current URI: "+uri)
uri = "t_"+uri
def headers = messageExchange.getResponseHeaders()
bytes= headers["Content-Length"].get(0)
log.info("Number of Bytes: "+ bytes)
timeTaken = messageExchange.getTimeTaken().toString()
values = timeTaken + ";"+ bytes
context.testCase.setPropertyValue(uri, values)
I expected that the uri would be created as the key and timeTaken and bytes are values.
When run the request (from Request Test Suite) it creates the property.
But Functional Test Case calls this request from other Test suite it does not create such property.. How to resolve this? I see the work around run the each request and get the property created so that on next run it would be updated.. But the problem is when starting test suite I am deleting all the properties and values. So again it is a mess up.. Can someone let me know how to resolve this issue?
you can add a result to a csv file directly after execution like this:
new File('out.csv').append( "$uri; $timeTaken; $bytes\n" )
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,
I am calling a REST based service from SoapUI. I have created a load test for the service and the test works. I wrote the following code in my setup script for the load test.
log.info("This is from the setup script")
def request = context.expand('${#Request}')
log.info(request)
def response = context.expand('${#Response}')
log.info(response);
The only item I am getting in my log is the "This is from the setup script".
I also added the following lines of code in my Teardown script.
log.info("Teardown script")
def response = context.expand('${#Response}')
log.info(response);
I am not seeing the "Teardown script" text in the log. At this point I am a bit puzzled as to the behavior.
Load Test:
Test Suite
Test case options.
I have unchecked the Discard OK results test box.
What changes do I need to do to my scripts in order to log the requests and the responses?
When you create a setup and/or teardown script, remember those are run only once per run, not per test! What you intended is not going to work.
In your setup, since no tests have run yet, the context is going to be empty ... as you can see from your log message.
In your teardown, I suspect there is a bug in SoapUI and the log is not getting sent to the log tab. If you intentionally create an error (I used logg.info "Hello world!" - note the intentional double g), I still got a an error in the error log tab.
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
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