I have one groovy script named Check in testcase 1, which contains the follow code:
log.info "Running from different test case script"
I am trying to get this message in a script written in testcase 2:
package com.eviware.soapui.impl.wsdl.testcase;
Test_script= testRunner.testCase.testSuite.project.testSuites["TestSuite"].testCases["TestCase"].testSteps["Check"]
def myCont= new WsdlTestRunContext(Test_script)
log.info Test_script.run(testRunner,myCont)
It gives me output as:
Wed May 18 17:39:57 IST 2016:INFO:com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStepResult#131bc813
What to do here to see proper message in output
TestStep.run method doesn't return directly a desired object from other testStep or so, It returns a generic WsdlTestStepResult object to see the status, possible errors etc.. of the testStep execution, due this log.info Test_script.run(testRunner,myCont) it's printing the result of toString() method on WsdlTestStepResult object.
If you want to pass objects from one groovy script testStep to another, you've to use the context variable which is available in each groovy script testStep.
For your case since you're running the first groovy script using TestStep.run(TestCaseRunner testRunner,TestCaseRunContext testRunContext) from the second one script, you can get back all objects added in the context of your first script in the second one with testRunContext object passed to run method. Let me show it with an example:
In the first groovy script add your text as a property of the context:
// instead of log put the text in a context property
context.setProperty('somePropToGetBack','Running from different test case script')
// you can put all the properties you want...
context.setProperty('anotherOne','more props')
Then in your second script you only have to get back these properties:
package com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext
def Test_script= testRunner.testCase.testSuite.project.testSuites["TestSuite"].testCases["TestCase"].testSteps["Check"]
def myCont= new WsdlTestRunContext(Test_script)
def result = Test_script.run(testRunner,myCont)
// after execution in myCont variable you've all properties you set
// in context variable of first script
log.info myCont.getProperty('somePropToGetBack') // prints Wed May 18 14:56:36 CEST 2016:INFO:Running from different test case script
log.info myCont.getProperty('anotherOne') // prints Wed May 18 14:56:36 CEST 2016:INFO:more props
Hope it helps,
Related
i have to run the testStep multiple time with different data loading from external file(using file object looping to run the testSteps for each record) and validate the data from response for each value.
i'm able to do that and its executing and works fine. but when ever assert fails execution is not stoping and it will continue and shows testStep is passed.
i can see the failed in "script log" windows whenever it fails because i'm using log.info with run.status
and i check checkbox for "abort if test if any error occurs" and "failed tes cases if testStep fails" on test case options.
still it continue executing the test cases
Important Note: above scenario happens when assert fails in middle of the records, if it is fails for last record then its stopping the execution.
Code which i used
This Groovy script code: file iterates the testStep as per the no of rows in file
// Get tha path to an input file from a custom property
def inputFilePath = testRunner.testCase.testSuite.getPropertyValue( "inputFile" )
// Get the test case we want to Run
def tc=testRunner.testCase.testSuite.testCases["DetectRules"].testSteps["Rules_DENF_224"]
// Iterate through the input file
File file = new File(inputFilePath.toString()).eachLine{
// Set Test Suite custom properties
testRunner.testCase.testSuite.setPropertyValue( "InValue", it.split(",")[0] )
testRunner.testCase.testSuite.setPropertyValue( "OutValue", it.split(",")[1] )
testRunner.testCase.testSuite.setPropertyValue( "outDesc", it.split(",")[2] )
def runner = tc.run(testRunner,context)
//Get the Json response from execution
def FraudReq = testRunner.testCase.testSuite.testCases["DetectRules"].testSteps["Rules_DENF_224"].testRequest.response.responseContent
// Log info to a output
log.info "Execution of test case with Input value: ${it.split(",")[0]} and expected returned value: ${it.split(",")[1]} and Expected Description is: ${it.split(",")[2]} ${runner.status}"
// Sleep for a second since were using public service
sleep 500
}
this is testStep --> Script Assertion: section
import groovy.json.JsonSlurper
def response= messageExchange.response.responseContent
def jsonsl= new JsonSlurper().parseText(response)
def expected=context.getTestCase().getPropertyValue(“rule number”)
assert expected==jsonsl.results[4].id
if above assert fails then testStep should stop ,but testStep failing but execution is not stopped.
any help appreciated
I have one test case which is called (started and finished) before every run of other test cases. It is something like 'test data preparation' test case. Output from this test case is list with some elements, list looks like this:
def list = ['Login', 'Get Messages', 'Logout', etc.]
List is different on every run. I need to transfer this list from 'test data preparation' test case to other test cases. Transfer will be between two Groovy scripts.
How to transfer list between two Groovy test steps in SoapUI?
As I understand it:
You have one TestCase, which you call from every other TestCase.
I assume this is done using a "Run TestCase" teststep?
You would like to be able to pass a list of strings
As I read it, parameters go one way. From the "external testcase" and back to the calling testcase. There is no "input" from each testcase to this "external testcase"?
The Groovy Script in your "external testcase" may then generate a String result, which in turn can be converted to something like an Array or ArrayList of Strings.
This could be a string with values separated by ;
def result = ""
result += "Entry1;"
result += "Entry2;"
result += "Entry3;"
// You may want to add a line of code that removes the last ;
return result
This result will then be easily retrieved from Groovy Scripts elsewhere, by adding a few lines of code.
If the Groovy Script is placed in another TestCase, but in the same TestSuite, you may retrieve the result using:
def input = testRunner.testCase.testSuite.getTestCaseByName("Name of TestCase").getTestStepByName("Groovy Script Name").getPropertyValue("result")
If it is placed in a TestCase in a different TestSuite, you may use:
def input = testRunner.testCase.testSuite.project.getTestSuiteByName("Test Suite Name").getTestCaseByName("Test Case Name").getTestStepByName("Groovy Script Name").getPropertyValue("result")
and then loop over the input doing something like:
for (def s : input.split(";")) {
log.info s
// Do your stuff here
}
I hope that makes sense...? :)
from groovy step 1 you shall return the list:
def list = ['Login', 'Get Messages', 'Logout']
return list
from groovy step 2 you can get this returned list
def result = context.expand( '${Groovy Script 1#result}' )
list = result.tokenize('[,] ')
list.each{
log.info it
}
note that you get a string that you have to convert back to a list (tokenize).
I did this with SOAPUI pro.
Another way (ugly) would be to store the list in a custom property in groovy script 1 (using testRunner.testCase.setPropertyValue("myList",list.toString())
and to recover it in groovy step 2 (testRunner.testCase.getPropertyValue("myList")
I hope that will help
EDIT : if list elements contain spaces
this is not very clean and I hope someone will help to provide something better but you can do the following :
list = "['Login - v1', 'Get Messages - v2', 'Logout - v1']"
list = list.replace('\'','\"')
def jsonSlurper = new groovy.json.JsonSlurper()
list = jsonSlurper.parseText(list)
list.each{
log.info it
}
Alex
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 :)
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,
I am having 2 test cases in my suite.
First test case contains 1 test step with xml request.
Second test case contains 1 test step with groovy script. I want to run the 1st test case from this groovy script a number of times. Every time I want to change the input XML. I am unable to update the input XML in TestCase 1.
I have the following the code for the groovy script:
import com.eviware.soapui.support.XmlHolder
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def tsuite = testRunner.testCase.testSuite
def acctInq_tstepName = tsuite.getTestCaseAt(1).getTestStepAt(0).getName()
def acctInq_requestHolder = tsuite.getTestCaseAt(1).testSteps[acctInq_tstepName].testRequest.getRequestContent()
def acctInq_req = groovyUtils.getXmlHolder("$acctInq_requestHolder")
acctInq_req["//soapenv:Envelope[1]/soapenv:Body[1]/v2:AcctInqRq[1]/ifx:DepAcctId[1]/ifx:AcctId[1]"] = "0009917812344"
acctInq_req.updateProperty()
I also tried using
tstep.setPropertyValue("request",cStr(acctInq_req))
In either case the XML is not getting updated. Please help.
Try set node value like below and check if it fixed the issue:
acctInq_req.setNodeValue("//*:ifx:AcctId[1]", "0009917812344")