Property transfer in SOAPUI using groovy script - groovy

Hi i am new to soapui and have this situation.I have two service memberservice1 where the response has "Region" property.I need to check this property and see if its value is "SCR" i need to modify it to "SCA" and pass it to another WS memberservice2.
I tried this way but couldnt get it.Can anyone please suggest.
def smlholder = groovyUtils.getXMLholder("Webservice#request");
def node = smlholder.getnodevalue("//region");
if(node == 'SCA')
testRunner.testcase.testSteps("anotherwebservicename").setProperty('Region','SCR');

Your example code does not match you statement. Below I will follow your statement, and ignore the broken code example.
There are several different ways this cane be done. The easiest would probably be:
def region = context.expand("${Webservice#Response#//*:region}")
if (region == "SCR")
testRunner.testCase.testSteps["anotherwebservicename"].setProperty("Region", "SCA");
else
testRunner.testCase.testSteps["anotherwebservicename"].setProperty("Region", region);

Related

How do I mock a changing url in a loop?

I'm trying to mock a set of urls that change through a loop and prove out that a bad output format (not json) throws up an error. I currently have
#unittest.mock.patch("ORIG_URL", "MOCK_URL")
def test_url(self)
mock_response = "some string"
self.adapter.register_uri("GET", "MOCK_URL", json=mock_response, status_code=200)
but the original GET command is taking something of the form
let = [/path1, /path2]
for entry in let:
session.get("MOCK_URL" + entry...
so I basically need to mock something different each time it loops through but I'm unclear on how to do this. I understand where the problem is but I haven't yet found a solution. Any help would be appreciated!

How to return a variable from a python function with a single parameter

I have the following function:
def test(crew):
crew1 = crew_data['CrewEquipType1']
crew2 = crew_data['CrewEquipType2']
crew3 = crew_data['CrewEquipType3']
return
test('crew1')
I would like to be able to use any one of the 3 variables as an argument and return the output accordingly to use as a reference later in my code. FYI, each of the variables above is a Pandas series from a DataFrame.
I can create functions without a parameter, but for reason I can't quite get the concept of how to use parameters effectively such as that above, instead I find myself writing individual functions rather then writing a single one and adding a parameter.
If someone could provide a solution to the above that would be greatly appreciated.
Assumption: You problem seems to be that you want to return the corresponding variable crew1, crew2 or crew3 based on your input to the function test.
Some test cases based on my understanding of your problem
test('crew1') should return crew_data['CrewEquipType1']
test('crew2') should return crew_data['CrewEquipType2']
test('crew3') should return crew_data['CrewEquipType3']
To accomplish this you can implement a function like this
def test(crew):
if crew=='crew1':
return crew_data['CrewEquipType1']
elif crew=='crew2':
return crew_data['CrewEquipType2']
elif crew=='crew3':
return crew_data['CrewEquipType3']
...
... # add as many cases you would like
...
else:
# You could handle incorrect value for `crew` parameter here
Hope this helps!
Drop a comment if not

Access test case property from test suite tear down script

I am trying to access the test case property from test suite tear down script.
i am not able to use test runner properties.
def testSuiteProperty = testRunner.testCase.testSuite.getPropertyValue( "MyProp" )
Need to access the test case property using test case name.
It will be very helpful if some one can answer it.
To access the test Suite property, it is simply a case of this in your tear down script....
def someProp = context.expand( '${#TestSuite#someProp}' )
Now, I use the Pro version and I don't know what you're using, so I'm not sure if the next part of my answer will help.
In Setup Script, Tear Down script, Script assertions, you can 'right-click' in the code window where you type your script and there is a 'Get Data' menu item int he context menu. This allows you to select a piece of data of interest. In fact, the line of code above was generate by using the 'Get Data' context menu option.
To access a custom property for a given test case within your suite tear-down script, you need to do this...
def testCase = testSuite.testCaseList.find { p -> p.name == 'TestCase 2' }
def testProp = testCase.getProperty('testCaseProp');
log.info(testProp.value);
The syntax you are using is not correct
Try using this
def testCs = testRunner.testCase.testSuite.project.testSuites["name of testsuite"].getTestCaseByName(name)
def tcprop = testCs.getPropertyValue("NameOftestCaseProp")
We are first getting refernce of the test case and then accessing its property
or below should also work
def testcaseProp= testRunner.testCase.testSuite.project.testSuites["name of testsuite"].getTestCaseByName(name).getPropertyValue("name of property)
try whichever looks simple to you, though both are same

groovy returning different node count

I am trying to get the count of result nodes in soapUI using groovy and the below code gave me the correct count
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder("StepName#ResponseAsXml")
def cnt = holder["count(//Results/ResultSet/Row)"]
but when i tried the below i got a count of 1. How are the two different?
def cnt = holder["count('//Results/ResultSet/Row')"]
Though I've never used SoapUI, in the second one, you are passing a String (wrapped in '...') to count.
The first passes a path which I guess gets evaluated into a list of nodes.
All the examples I can find do not wrap the path in a String, so my guess is the first example is the way to do it ;-)
EDIT
Refer Tips and Tricks for most of the SoapUI and Groovy related questions. And count in xpath.

Second map value is always null, even if it prints out 1

I have some code getting data and then selecting it in order. For this I use simple maps that I may later access with ease (I thought..).
I use the following code within a loop to insert maps to another map named "companies":
def x = [:]
x.put(it.category[i], it.amount[i])
companies.put(it.company, x)
And I can surely write the result out: [Microsoft:[Food:1], Apple:[Food:1]]
But then, when I am about to get the food value of each company it always is null. This is the code I use to get the values:
def val = companies.get(it.company).get(key.toString())
def val = companies[it.company][key] // doesn't make a difference
Val is always null. Can someone help and / or explain why I have this error. What am I doing wrong? I mean, I can clearly see the 1 when I print it out..
My guess is that it.category[i] and key are completely different types...
One thing you could try is:
x.put(it.category[i].toString(), it.amount[i])
and then
def val = companies[it.company][key.toString()] // doesn't make a difference
The solution was simple to make the category as a string:
x.put(it.category[i].toString(), it.amount[i])
And after that little fix it all works as expected.

Resources