My use case is that I want to do a bulk update of request bodies in multiple SoapUI projects.
Example of request body.
{
"name": "${#TestSuite#NameProperty}"
"id": "${#TestSuite#IdProperty}"
}
I want to expand the property ${#TestSuite#NameProperty} through Groovy, and get the value stored at TestSuite level then modify it as necessary.
Suppose I have 50 test steps in my test case and I want to expand the request for each one from Groovy script. To expand a specific test steps, I would pass the test Steps's name. For example:
expandedProperty = context.expand('${testStep1#Request}')
But, how can I achieve the same, if I wanted to iterate over all 50 test steps? I tried to used a nested parameter inside the context.expand expression, but it did not work. For Example:
currentTestStepName = "TestStep1"
expandedProperty = context.expand('${${currentTestStepName}#Request}')
This only returned me the expanded request from the test step right above it (where I am running the groovy script from) rather than the "TestStep1" step. ( Which is madness!)
Also, context.expand only seems to work while executing via Groovy script from the SoapUI workspace project. Is there and other way, or method similar to context.expand which can expand the properties like "${#TestSuite#NameProperty}" during headless execution? Eg: A groovy compiled jar file imported in SoapUI.
Thanks for any help in advance!
You can use context.expand('${${currentTestStepName}#Request}') way to get it.
There are other approaches as well, which does not use context.expand.
In order to get single test step request of any given test step:
Here user passes step name to the variable stepName.
log.info context.testCase.testSteps[stepName].getPropertyValue('Request')
If you want to get all the requests of the test case, here is the simple way using the below script.
/**
* This script loops thru the tests steps of SOAP Request steps,
* Adds the step name, and request to a map.
* So, that one can query the map to get the request using step name any time later.
*/
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
def requestsMap = [:]
context.testCase.testStepList.each { step ->
log.info "Looking into soap request step: ${step.name}"
if (step instanceof WsdlTestRequestStep) {
log.info "Found a request step of required type "
requestsMap[step.name] = context.expand(step.getPropertyValue('Request'))
}
}
log.info requestsMap['TestStep1']
Update :
If the step that you are interested is REST step, use below condition instead of WsdlTestRequestStep in the above.
if (step instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep) { //do the stuff }
Related
When a script is created in a TestCase in SoapUI, you get access to the following variables:
log, context, and testRunner
From testRunner I grab the TestCase Step of interest with:
def testStep = testRunner.getTestCase().getTestStepByName('TestCase Step 1')
After the test case is run with testRunner.runTestStep(testStep), you get a response. I would like to modify that response, but when I try:
testStep.setPropertyValue('Response', "Sample response content: Lorem Ipsum")
I get: Trying to set read-only property [Response]
testStep is of type RestTestRequestStep. I know that one can use MockRequests and set MockResponses, but for this one, I need data from an actual server with different queries, combine the data, and then give it as a response.
Is there a way to modify the response returned to a TestStep?
Based on the info, I can suggest that you can take the output in a property( Global Property, Project level property) and pass/get the property in the following steps to combine it.
Can any one know how to transfer the groovyscript response into the properties step of SOAP UI. I am trying to generate the random numbers using the groovy script, and when i am gettign the random generated numbers how do i transfer that value to properties in soap ui which can be used for the TCs as a parametered value.
TIA
To make it simple,
Use below code to store any value on,
test case level custom properties:
testRunner.testCase.setPropertyValue("propertyName","value");
test suite level custom properties:
testRunner.testCase.testSuite.setPropertyValue("propertyName","value");
project level custom properties:
testRunner.testCase.testSuite.project.setPropertyValue("propertyName","value");
Use below code to check whether value stored successfully on runtime:
test case level:
log.info testRunner.testCase.getPropertyValue("propertyName");
test suite level:
log.info testRunner.testCase.testSuite.getPropertyValue("propertyName");
project level:
log.info testRunner.testCase.testSuite.project.getPropertyValue("propertyName");
Use below code to use the property value inside anywhere on,
test case level:
${#TestCase#propertyName}
test suite level:
${#TestSuite#propertyName}
project level:
${#Project#propertyName}
global level:
${#Global#propertyName}
Here you go:
The below groovy script code snippet will generate a random number and set the value into to a test case level custom property, say PROPERTY_NAME.
Groovy Script
context.testCase.setPropertyValue('PROPERTY_NAME', (Math.abs(new Random().nextInt()) + 1).toString())
In the same test case, it can be accessed in any test requests as ${#TestCase#PROPERTY_NAME}
EDIT: Based on the change you wanted while above original code works though
def a = 9
def AccountName = ''
(0..a).each { AccountName = AccountName + new Random().nextInt(a) }
context.testCase.setPropertyValue('Property_Name', AccountName.toString())
Even you achieve the same thing using below (just updated value in nextInt() to the first answer)
context.testCase.setPropertyValue('PROPERTY_NAME', (Math.abs(new Random().nextInt(999999998)) + 1).toString())
At the moment we are developing an application and deploying it in my local machine. And now I'm doing a load test using jmeter and it runs without a issue. The problem I'm getting is when I try to run the same test on a different machine with a different server it doesn't work.
And I observed that html element id's are different on different servers with JSF.(Issue is form submissions are not working). Is there a way to overcome this issue with jmeter since I want to run the same test scripts on different servers.
The process of extracting dynamic values from previous response and adding them to next request is called correlation. JMeter provides variety of Pre Processors and Post Processors to allow modification of requests on-the-fly.
Example use case:
Send a GET request a JSP page which contains a number of inputs
Extract all the inputs names and populate values where required
Populate dynamic fields of next request with values extracted in point 2
Send a POST request
Point 1: No changes to your script required
Point 2:
Add XPath Extractor as a child of GET Request and populate it as follows:
Reference Name: INPUT
XPath Query: //input/#name | //input[#type='hidden']
This extractor will fetch all <input> HTML elements and store it to JMeter Variables as
INPUT_1=javax.faces.ViewState
INPUT_2=loginForm_SUBMIT
INPUT3=...
...
Add a Beanshell Pre Processor as a child of next request with the following code
import java.util.Iterator;
import java.util.Map;
Iterator iter = vars.getIterator();
while (iter.hasNext())
{
Map.Entry e = (Map.Entry)iter.next();
if (e.getValue() != null)
{
if (e.getKey().toString().startsWith("INPUT_") && e.getValue().toString().length() >0)
{
sampler.addArgument(e.getValue().toString(),"VALUE OF YOUR INPUT");
}
}
}
The code above will fetch all variables prefixed with INPUT_ and add it as parameters to your next request.
See How to use BeanShell: JMeter's favorite built-in component guide for more information on Beanshell scripting in Apache JMeter to see what else can be done using Beanshell scripting and using of JMeter objects exposed to Beanshell test elements.
I have Test case starts with Groovy Test step and followed with Property and 4 SOAP request test steps. In groovy Test step I performing execution of those SOAP requests, accessing data from property Test step.
Here I just want to execute those SOAP request from groovy test step alone. When I ran it as Test Case in SOAPUI after Executing groovy Test step, those 4 SOAP requests also get executed and my Test case got failed.
I use testRunner.cancel("Skip the Teststep") it could skip those test step, But it results as failure in Execution report and I cant find any method to skip test step using groovy.
Please help me somebody on this.
Regards,
Madhan
Try this in the Groovy Script step.
testRunner.testCase.testSteps.each{k, v ->
if(k in ['step1', 'step2'])
v.cancel()
}
where step1 and step2 are the steps you want to skip.
If you want to cancel all the test steps then use
testRunner.testCase.testSteps.each{k, v ->
testRunner.cancel(k)
if want to disable test steps
def testSuite = context.testCase.testSuite;
def totalTestCases = testSuite.getTestCases().size();
for(n in (0..totalTestCases-1))
{
testSuite.getTestCaseAt(n).setDisabled(true)
}
I may be going about this in the completely wrong way, but how do I pass a dynamic variable to a bunch of requests within the same testsuite in SoapUI?
My first test step is a Groovy script. I need to generate a random account name, and then use it in all my other requests. There are about 20 other requests. I initially thought I could just loop the testsuite, but it is not working.
This is my groovy script at the beginning:
Random random = new Random()
def randUserAccount = "testAccount"
int max = 100000
randnum = random.nextInt(max+10000)
randUserAccount += randnum
log.info " Creating account: $randUserAccount"
Then in each request step, I have things like this:
<ns:CreateAccountRequest>
<accountID>${randUserAccount}</accountID>
...
or
<ns:PurchaseRequest>
<accountID>${randUserAccount}</accountID>
...
The account is null when I actually send it, and of course that gives errors on the server side. How do I really get the variable to persist across all the requests in the testsuite?
Thanks in advance for any hints!
You can use the context, I believe. You can definitely use it between requests in a test, but I also think it will work between tests in a suite.
context.setProperty("randUserAccount", randUserAccount)
Then use the syntax you specified in the actual requests.
Let me know if this doesn't work. You can also use 'properties' to do this, but it is a little more work.
or you can create a variable in property then set the value through set property as mentioned above..
for every tag jus right click and check the your project varaible it will automatically insert the code..
Hope it help