Get Header from Request in SoapUI groovy script - groovy

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

Related

How to pass a variable to the body (not header/params) of a sampler

I have a request from where I am getting a token in response. I have captured that token into a variable using Regular Expression Extractor-Post processor.
Now, I want to pass this token to the body of the next sampler in two places, one as the contributionID and the second as the contrib_(pass the value).mp4
This is how the body looks:
enter image description here
The problem is, passing it via header manager is not working. And passing variable in body causes syntax error.
It seems that I would need to pass it using beanshell script or something else which I am unable to implement.
Any leads?
I have tried adding beanshell preprocessor in the sampler where I want to pass the value but I am unable to capture the body and the respective parameter.
It would be a huge help if I could get a hint towards a working solution.

how to replace blank values with null in http post body in Jmeter while reading data from csv for multiple iterations?

I am able to use below code to replace blank value with null in http post request body
def body = sampler.getArguments().getArgument(0).getValue().replaceAll('""','null')
sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('', body,'')
sampler.setPostBodyRaw(true)
But, I get an error for multiple iterations.
javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getValue() on null object
I suspect the removeAllArguments call is affecting subsequent calls to the first line. If it works the first time and fails all subsequent calls then it's probably that. Try commenting that line out and see if it continues to happen:
def body = sampler.getArguments().getArgument(0).getValue().replaceAll('""','null')
sampler.getArguments().removeAllArguments() // I bet this line affects all invocations of the script.
sampler.addNonEncodedArgument('', body,'')
sampler.setPostBodyRaw(true)
Wouldn't that be easier to go for __strReplace() function instead (can be installed as a part of Custom JMeter Functions bundle using JMeter Plugins Manager)?
Whatever, if you like Groovy and copy-pasting the code from Internet without understanding what it's doing I think you need to amend "your" code to look like:
def data = new org.apache.jmeter.config.Arguments()
def body = new org.apache.jmeter.protocol.http.util.HTTPArgument('',sampler.getArguments().getArgument(0).getValue().replaceAll('""','null'),'',false)
data.addArgument(body)
sampler.setArguments(data)

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.

Groovy Script and Property transfer in soapUI

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,

SOAP UI Reporting

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

Resources