Access test case property from test suite tear down script - groovy

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

Related

Removing the property values from all test cases of a test suite in SoapUI?

I have 108 test cases in a test suite.
In each test cases, it has certain properties starts with r_ (means it is a return variable from other test case). I want to remove all the property values which starts with r_.
I can do this by TearDown Script in each test case. But, it takes lot of time.
Is it possible from doing the same from Suite level TearDown Script ?
Yes, it is possible.
Below is the suite level TearDown Script.
//Define the pattern of property names which you want to clear the values
def pattern = 'r_'
//Loop thru each case of the suite and find those matching properties and clear them
testSuite.testCaseList.each { kase ->
def props = kase.propertyNames.findAll { it.startsWith(pattern) }
def msg = props ?: 'None'
log.info "Matching properties for ${kase.name} test are : ${msg}"
props?.each { prop -> kase.setPropertyValue(prop, '')}
}

Run a test step in Script assertion SOAP UI

I have 5 test steps in a test case and i want to write a script assertion for a test step
Like
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def httpResponseHeaders = context.testCase.testSteps["Step1"].testRequest.response.responseHeaders
def httpStatus = httpResponseHeaders["#status#"]
def httpStatusCode = (httpStatus =~ "[1-5]\\d\\d")[0]
if (httpscode == "500")
I want to re-run the test step named as step 1
I know that testRunner class is not present in Script assertion is there a way to do it with messageExchange variable class
I saw an answer on stack overflow
`messageExchange.modelItem.testStep.testCase.getTestStepByName("Step1").run(context.getTestRunner(),context)`
I tried the code but as soon as i click run SOAP UI hangs and I have to force close the SOAP UI application
To run a test step from script assertion you may use this
import com.eviware.soapui.support.types.StringToObjectMap
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner
def Runner = new WsdlTestCaseRunner(messageExchange.modelItem.testStep.testCase, new StringToObjectMap())
yourTestStep= messageExchange.modelItem.testStep.testCase.testSuite.project.testSuites["ScriptLibrary"].testCases["Library"].testSteps["Lib"]
yourTestStep.run(Runner,context)
Your code is fine, except your logic is flawed, because you encounter the error 500 for the first time and then you call the same step again and it fails again with 500 and so on. You would need to get different status, before your Java runs out of memory.
So if you want to do something like that, you have to implement some kind of counter (using TestCase, TestSuite, Project or Global property) to perform this loop only several times and then fail.
Anyway this worked for me:
def utilitiesSuite = messageExchange.modelItem.testStep.testCase
.testSuite.project.getPropertyValue('utilitiesTestSuiteName');
messageExchange.modelItem.testStep.testCase.testSuite.project
.testSuites[utilitiesSuite].testCases["Test Case utility name"]
.run(null, true);
In this case we have all "utilities" = test cases with some often needed functionality in a dedicated test suite and I wanted its name to be possible to set up on Project level, of coures the Test Suite name can be put there directly as for example "Utilities'.
Also i have tried with this:
context.getTestRunner()
As the first parameter in the methor run instead of the null, but it worked only when testing the one requirement, not when running whole test case or test suite.
I let here another example that worked for me.
It's an assertion script, which runs a testStep basing on a response node value.
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context );
def holder = groovyUtils.getXmlHolder( "yourTestStep#response" );
def myValue = holder.getNodeValue( "//theNameSpace:yourNode" );
if(myValue == 'whateverYouWant'){
//com.eviware.soapui.support.UISupport.showInfoMessage(myValue);
def Runner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(
messageExchange.modelItem.testStep.testCase, new com.eviware.soapui.support.types.StringToObjectMap());
def mySteps= messageExchange.modelItem.testStep.testCase.testSuite.project.testSuites["yourTestSuite"].testCases["yourTestCase"].testSteps["yourTestStep"];
mySteps.run(Runner,context);
}

SoapUI & Groovy - How to get properties from different TestCases using Run testCase

Sorry in advance. I am sure this is not a new question but I swear I've been looking for days and have tried several solutions but no one fits exactly what I need. I hope you guys can help me...
My problem:
I have a main script which sends bash commands to our server:
TestSuite: Tools;
TestCase: sendBashCommands;
TestStep: groovycript;
This test script is called by several test cases using "Run testCase". Each test case have a different bash command:
TestSuite: ServerInfo
TestCase: getServerVersion
Property: BashCommand="cat smt | grep Version"
TestStep: Run sendBashCommands
TestCase: getServerMessage
Property: BashCommand="cat smt | grep Message"
TestStep: Run sendBashCommands
...
On my sendBashCommands.groovyScript I have already tried the following:
//def bashCmd= context.expand( '${#BashCommand}' );
//it returns empty;
//def bashCmd= testRunner.testCase.getPropertyValue( "BashCommand" );
//it returns null;
//def bashCmd= context.getTestCase().getPropertyValue( "BashCommand" );
//it returns null;
def bashCmd = context.expand('${#TestCase#BashCommand}');
//it also returns empty;
Currently I use a solution which works with project properties but what I actually need is working with these properties on the level of the test case which is calling the sendBashCommand script. Is that possible? How could I do it?
I think you are doing it for maintenance purpose...
As per your approach, the result has to come as null or empty. Because your groovyscript cannot get the properties of other testcases directly. If you call a getProperty() Method directly, then it will refer to the current testStep's property. So the following approach could help you.
put the following code in your individual test cases, "getServerVersion" etc.
def currentProject = testRunner.testCase.testSuite.project
// the following is to store the names of the test suite and the test cases from which you want to call your main script
currentProject.setPropertyValue("TestSuiteName",testRunner.testCase.getTestSuite().getLabel().toString())
currentProject.setPropertyValue("TestCaseName", testRunner.getTestCase().getLabel().toString())
// call sendBashCommands here -> run sendBashCommands
put this code in your main groovy script (sendBashCommands.groovyscript)
def currentProject = testRunner.testCase.testSuite.project
def callingTestCase = currentProject.testSuites[currentProject.getPropertyValue("TestSuiteName")].testCases[currentProject.getPropertyValue("TestCaseName")]
def bashCmd = callingTestCase.getPropertyValue( "BashCommand" )
// to verify
log.info bashCmd
The project is common to all the test suites, so you have to use project property in any case, i.e., for your requirement :)
Enjoy :)

Context missing from called test case

In SoapUI I’m running a test case from a groovy script with this code:
def contextMap = new StringToObjectMap(context)
def myTestCase_1 = myTestSuite.getTestCaseByName("TestcaseName")
myTestCase_1.run(contextMap, false)
In the called test case I set some context properties in a groovy script in this way: context.setProperty(“ProperyName”,”Value”)
After the called test case has finished the created property values are missing in the context of the groovy script that has called this test case.
How can I pass back property values to a test case that called another test case?
Instead of using context if you want to share properties through different testCases in the same testSuite try to set the properties on the testSuite level which will be shared for all testCases in this testSuite. For example if you have a testSuite with two testCases: testCase 1 and testCase 2, on a groovy script from testCase 1 do:
testRunner.testCase.testSuite.setPropertyValue( "sharedVar", "someValue" )
Then to use this property from another groovy script in testCase 2 use:
def shared = testRunner.testCase.testSuite.getPropertyValue("sharedVar")
log.info shared
You can also use this testSuite property saved in the testCase 1 in the testCase 2 for example in testStep SOAP Request using: ${#TestSuite#sharedVar}
EDIT BASED ON OP COMMENT:
Since seems that the OP problem is how to get the properties from a testCase which is called from another testCase using groovy script; here is a possible work around, which consists in through the run result get the invoked testCase and then get his properties:
The first testCase which invokes the other testCase using groovy looks like:
import com.eviware.soapui.support.types.StringToObjectMap
def contextMap = new StringToObjectMap(context)
def testCase2 = testRunner.testCase.testSuite.getTestCaseByName("TestCase 2")
// get the result since is running sync
def result = testCase2.run(contextMap, false)
// from the result access the testCase and then the properties setted there
log.info result.getTestCase().getPropertyValue("varSettedOnTestCase2")
In the second testCase simply perform your logic and set the properties which will be get back from the first testCase:
testRunner.testCase.setPropertyValue("varSettedOnTestCase2","test")
Hope this helps,

Clone TestStep from a Test Case to another Test Case using groovy

I use groovy script in my test suite to fill gaps in Automation. Here I need to clone a Property from one Test case to another Test Case. For Example.. Clone the Property test step from TestCase1 to TestCase2, In order to get values from that property.
I tried to get values in the property from one TC to another, But SOAPUI will not allow to do that action. we can't transfer a property value from one TC to another TC. So I go for cloning the Test step using groovy.
Your help is much appreciated.. Awaiting response from anyone..
Thanks,
Madhan
You can run TestCase2 from TestCase1 using test step "Run TestCase". Create properties that you need directly in TestCase2 so you can set them by "Property transfer" test step in TestCase1. More information about run test case here and about property transfer here.
Another way is to set properties and run TestCase programmatically. Something like this:
// get properties from testCase, testSuite or project if you need
def testCaseProperty = testRunner.testCase.getPropertyValue( "MyProp" )
def testSuiteProperty = testRunner.testCase.testSuite.getPropertyValue( "MyProp" )
def projectProperty = testRunner.testCase.testSuite.project.getPropertyValue( "MyProp" )
def globalProperty = com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "MyProp" )
//get test case from other project or from the same one
project = testRunner.getTestCase().getTestSuite().getProject().getWorkspace().getProjectByName(project_name)
testSuite = project.getTestSuiteByName(suite_name);
testCase = testSuite.getTestCaseByName(testcase_name);
//set properties if you need
testRunner.testCase.setPropertyValue(property_name, property_value);
testRunner.testCase.setPropertyValue(another_property_name, another_property_value);
// run test case
runner = testCase.run(new com.eviware.soapui.support.types.StringToObjectMap(), false);
The following example copies all of the properties from a test suite to the project that the test suite belongs in.
A similar mechanism could be used for test case properties, etc.
testSuite.getProperties().each {
testSuite.getProject().setPropertyValue(it.getKey(), testSuite.getPropertyValue(it.getKey()))
}
To copy from one test case to another (I will leave defining the test cases)
def sourceTestCase = youdefinethis
def destTestCase = youdefinethis
sourceTestCase.getProperties().each {
destTestCase.setPropertyValue(it.getKey(), sourceTestCase.getPropertyValue(it.getKey())
}

Resources