Transfer Groovy script response to properties in SOAP UI 5.21 - groovy

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())

Related

Associate existing shared steps with a test case programmatically

I am trying to add a shared step to a test case that is being created using
CreateWorkItemAsync()
It is no problem to create test steps and add them to the test case
ITestStep testStep1 = testBase.CreateTestStep();
but I am trying to add an existing shared step to the test case. I cannot find a way in the Azure Devops SDK to do so.
There is a ISharedStepReference Interface which used to call a shared step set from a test case.
You should be able to add a shared step into a specific test case. A code snippet for your reference:
ITestManagementService testService = tfsCollection.GetService<ITestManagementService>();
ITestManagementTeamProject teamProject = testService.GetTeamProject(teamProjectName);
//find test case by testcase id
ITestCase testcase1 = teamProject.TestCases.Find(192); //192 is the testcase id
//find share steps by sharedstep id
ISharedStep sharedStep1 = teamProject.SharedSteps.Find(140); //140 is the shared step id
//Add shareSteps to the specific test case
ISharedStepReference sharedStepReference = testcase1.CreateSharedStepReference();
sharedStepReference.SharedStepId = sharedStep1.Id;
testcase1.Actions.Add(sharedStepReference);
testcase1.Save();
What I ended up doing is creating the test case using
CreateWorkItemAsync()
and then getting that work item using
GetWorkItemAsync()
and then editing the field
Microsoft.VSTS.TCM.Steps
as xml and inserting my own nodes into the proper places to add shared steps to the test case. You can see the format of the shared steps by getting a test case with an API GET request and looking at the format of Microsoft.VSTS.TCM.Steps.

SOAPUI Groovy - How do I access a Property added to MessageExchange, using testStepResult object?

Is it possible to add a property to messageExchange object in an "Assertion Script", and retrieve the value of this property later in "TestSuite TearDown Script"?
Basically, I want to get how many records REST API retrieved, in "TestSuite TearDown Script" window. I want to do this, without needing to process the responses of all steps in the "TestSuite TearDown Script".
This is how I am adding a property in "Assertion Script"
import groovy.json.JsonSlurper;
def resp = messageExchange.response.responseContent;
def jslurp = new JsonSlurper().parseText(resp);
messageExchange.addProperty("recordCount",Integer.toString(jslurp.size()));
I want to retrieve "recordCount" in "TestSuite TearDown Script", for all the 40+ REST API results that are executed as part of my TestSuite.
Somehow, if I am able to obtain a reference to the messageExchange object of each step from testStepResult, I should be able to achieve what I am trying to do.
Any pointers or your thoughts would be appreciated.
Thank you
I've never had to retrieve a property from an object set in one part of soapUI and used in another. I have a feeling soapUI doesn't share that way.
So, what about setting a custom property at, say the test-suite level? In your assertion script, you can use messageExchange to set a property like this:
def recordCounter = messageExchange.modelItem.testStep.testCase.testSuite.
setPropertyValue('recordCounter', 'your number');
Then, in your tear-down script you could retrieve it with:
def recordCounter = context.expand('${#TestSuite#recordCounter}')

Adding loadTestsFromTestCase from a string

I am creating an application where I pass in one or more test cases to the TestLoader that then get run.
I can run and add the testcase to the suite like this
suite1 = unittest.TestLoader().loadTestsFromTestCase(mymodule.Testcase01)
suite = unittest.TestSuite([suite1])
However, what I actually want to do is something like this
myTestcaseAsString="mymodule.Testcase01"
suite1 = unittest.TestLoader().loadTestsFromTestCase(myTestcaseAsaString)
suite = unittest.TestSuite([suite1])
What I want to happen is that the value of myTestcaseAsaString is passed into loadTestsFromTestCase as if it was hard coded like the first example
Is that possible? (my long term goal is to be able to add multiple testcases to teh same suite - if that makes a difference)
Thanks
Grant
This is what I ended up doing
suite2 = unittest.TestLoader().loadTestsFromTestCase(eval(testcasemodule))
I have full control over the testcasemodule value
Thanks
Grant

How to use nested paramter in SoapUI context.expand expression?

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 }

How to get the result for test steps other than test request using groovy

How to capture a test result details for test steps other than test request(Say Groovy script step) using groovy for soap ui?
For test request we can capture the result based on assertion result but for other test steps how can i get the result?
You must create assertions in the test than you want export the date. You must parse your json, xml, ... and later save the field than you want in a property of this test suite, also you can save in the properties of your project. Example of assertions:
**import groovy.json.JsonSlurper
def ResponseMessage = messageExchange.response.responseContent
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
testRunner.testCase.setPropertyValue("correlationID", jsonSlurper.uuid)**
Also you can use:
**testRunner.testCase.setPropertyValue( "correlationID", jsonSlurper.uuid)
testRunner.testCase.testSuite.setPropertyValue("correlationID", jsonSlurper.uuid)**
**testRunner.testCase.testSuite.project.setPropertyValue("correlationID", jsonSlurper.uuid)**
Then you have correlationID in the properties, the properties in this case name properties, of your test case, suite, project, ...
Finally only have than write in the header
Name Value
**correlationID '${Properties#correlationID}'**

Resources