Get all test cases and test steps in SoapUI-project - groovy

I'm looking for a way to get all the test cases with belonging test steps in SoapUI Pro.
I've managed to get all the test steps under one test case (Groovy in Setup Script)
def projectName= testRunner.testCase.testSuite.project.getName()
def testcaseName = testRunner.getTestCase().getName()
File myTestFile = new File("C:/temp/" + projectName + ".txt")
myTestFile .withWriterAppend{ out ->
out.println("TestCase: " + testcaseName)
testRunner.testCase.getTestStepList().each(){
out.println("TestStep: " + it.getName())
}
}
How can I get all test cases and test steps?

This will help you:
for ( testSuite in testRunner.testCase.testSuite.project.getTestSuiteList() ) {
for ( testCase in testRunner.testCase.testSuite.getTestCaseList() ) {
for ( testStep in testCase.getTestStepList() ) {
log.info "${testSuite.getName()} : ${testCase.getName()} : ${testStep.getName()} is a test step of type: ${testStep.getClass().toString().tokenize('.')[-1]}"
}
}
}

I can't comment (<50 reputation) or Edit (edit queue is full) on ou_ryperd's answer, but I put here a little change to anyone else that will use his code.
If the code is executed within a case context, the 2nd for called always the current TestSuite. Call the TestSuite from the 1st for instead, this way:
for ( testSuite in testRunner.testCase.testSuite.project.getTestSuiteList() ) {
for ( testCase in testSuite.getTestCaseList() ) {
for ( testStep in testCase.getTestStepList() ) {
log.info "${testSuite.getName()} : ${testCase.getName()} : ${testStep.getName()} is a test step of type: ${testStep.getClass().toString().tokenize('.')[-1]}"
}
}
}
All credits to ou_ryperd.

Related

Groovy closure return value independent of code structure

I have some code in build.gradle
test {
doFirst {
def profile = System.getenv("...")
if (profile == "dev") {
println "1: if start"
// ...
println "2: if end"
}
}
}
and last line ("2: if end") executing anyway, even if profile not "dev"
Looks like groovy don't care about code structure: it simple return last line as result of closure
because if I modify code to:
test {
doFirst {
def profile = System.getenv("...")
if (profile == "dev") {
println "1: if start"
// ...
println "2: if end"
}
println "3: after if"
}
}
Then, this way, if profile not "dev", then all ok - after checking statement groovy execute line with "3: after if"
Is this bug or feature? :)
Yes, as commented, this only in the debugger - groovy works fine

runner.results do not provide results in sequence of test run

I want to get status of testcases run in a testsuite using teardown scripts.
I am able to get the status but not in the sequence of the run of testcases.
I am getting results in random order. The names are in random order everytime.
for ( testCaseResult in runner.results )
{
log.info "$testCaseName"
}
Whenever I do that, I do get them in the correct order...
You may want to try something like this then:
// In this manner, I would expect you to get the testcases in correct order
for (def tc in runner.testSuite.testCaseList) {
// Now loop through the results in order to get the result for the current tc
for (def tcRunner in runner.results) {
def matchFound = false
if (tcRunner.testCase.name.equals(tc.name)) {
matchFound = true
// do your thing
}
if (!matchFound) {
// Do whatever you want to do, if the specific testresult was not found.
}
}
}

How to identify that the step is RunTestCase in SoapUI?

I have following Structure:
Each single functionality is broken down in Reusable script and reusing all in the Main Suite.
TestCase 1:
1. Login as Normal Customer (This is calling login test case from Reusable script)
2. Extract Session from STEP 1
3. Add diner card (This is calling add card test case from Reusable script)
4. View Added Card (This is calling view test case from Reusable script)
5. etc..
Now Each test case in Reusable script returns a property that r_result (Passed or Failed)
Now I wanted to check each run test case and see the property r_result is Passed or Failed. If It is failed, I need to check where the First Failed occurs (in RunTestCase) and report that error.
Is It possible to isolate ONLY RunTestCase steps in each Test case and use it in closure to get the results of each RunTestCase results?
Here is the script which can fetch the list of matching test steps across the soapui project.
Please follow the in-line comments.
result variable has all the list of test steps of required type. The you can leverage and do the needful using this data.
Script:
import com.eviware.soapui.impl.wsdl.teststeps.WsdlRunTestCaseTestStep
//To identify lookup test step is not this step
def currentStepMap = [ suite : context.testCase.testSuite.name, case : context.testCase.name, step : context.currentStep.name ]
//Type of step to look for
def stepTypes = [WsdlRunTestCaseTestStep]
//To hold the final result
def result = []
//Find the test step details of matching step
def getMatchingMap = { suite, kase, step ->
def tempMap = [suite : suite.name, case : kase.name, step: step.name]
def isNotMatching = currentStepMap != tempMap ? true : false
if (isNotMatching &&(stepTypes.any{step in it}) ) {
tempMap
} else { [:] }
}
def project = context.testCase.testSuite.project
//Loop thru the project and find the matching maps and list them
project.testSuiteList.each { suite ->
suite.testCaseList.each { kase ->
kase.testStepList.each { step ->
def tempResult = getMatchingMap(suite, kase, step)
if (tempResult) {
result << tempResult
}
}
}
}
if (result) {
log.info "Matching details: ${result} "
} else {
log.info "No matching steps"
}

How can I retrieve the build parameters from a queued job?

I would like to write a system groovy script which inspects the queued jobs in Jenkins, and extracts the build parameters (and build cause as a bonus) supplied as the job was scheduled. Ideas?
Specifically:
def q = Jenkins.instance.queue
q.items.each { println it.task.name }
retrieves the queued items. I can't for the life of me figure out where the build parameters live.
The closest I am getting is this:
def q = Jenkins.instance.queue
q.items.each {
println("${it.task.name}:")
it.task.properties.each { key, val ->
println(" ${key}=${val}")
}
}
This gets me this:
4.1.next-build-launcher:
com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty$ScannerJobPropertyDescriptor#b299407=com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty#5e04bfd7
com.chikli.hudson.plugin.naginator.NaginatorOptOutProperty$DescriptorImpl#40d04eaa=com.chikli.hudson.plugin.naginator.NaginatorOptOutProperty#16b308db
hudson.model.ParametersDefinitionProperty$DescriptorImpl#b744c43=hudson.mod el.ParametersDefinitionProperty#440a6d81
...
The params property of the queue element itself contains a string with the parameters in a property file format -- key=value with multiple parameters separated by newlines.
def q = Jenkins.instance.queue
q.items.each {
println("${it.task.name}:")
println("Parameters: ${it.params}")
}
yields:
dbacher params:
Parameters:
MyParameter=Hello world
BoolParameter=true
I'm no Groovy expert, but when exploring the Jenkins scripting interface, I've found the following functions to be very helpful:
def showProps(inst, prefix="Properties:") {
println prefix
for (prop in inst.properties) {
def pc = ""
if (prop.value != null) {
pc = prop.value.class
}
println(" $prop.key : $prop.value ($pc)")
}
}
def showMethods(inst, prefix="Methods:") {
println prefix
inst.metaClass.methods.name.unique().each {
println " $it"
}
}
The showProps function reveals that the queue element has another property named causes that you'll need to do some more decoding on:
causes : [hudson.model.Cause$UserIdCause#56af8f1c] (class java.util.Collections$UnmodifiableRandomAccessList)

How to generate requests and responses for all operations of a WSDL in soapUI using a groovy script?

I have a WSDL which has multiple operations. For each op i want a template .xml with its response and request.
I know how to do this manually in soapUI but I would like to generate them using a groovy script.
I googled a lot already, but seems I'm the only one who is looking for this.
My service has 16 Operations, so to do this manual would be too much time. Since the service gets updates every 2 months, an automation using a test step would be perfect.
I managed to do it for the requests already:
right-click on ´services´ in left tree, ´Generate Test Suite´, ´Single Test Case with one Request for each Operation´
then I loop through those Test Step Requests and store them on my disk.
import com.eviware.soapui.impl.wsdl.teststeps.*
for( testCase in testRunner.testCase.testSuite.getTestCaseList() )
{
for( testStep in testCase.getTestStepList() )
{
if( testStep instanceof WsdlTestRequestStep )
{
log.info "operation name: " +testStep.getName()
// create file name
Date startTime = new Date();
def cur_Time = startTime.getMonth() + "_" + startTime.getDate();
cur_Time = cur_Time + "_" + startTime.getHours() + startTime.getMinutes() +startTime.getSeconds()
def fileName = testStep.getName() + "_" + cur_Time
def inputFileRequest = new File("T:\\"+ "Request_" + fileName+".txt")
def inputFileResponse = new File("T:\\"+ "Response_" + fileName+".txt")
// write request to file
inputFileRequest.write(testStep.getProperty("request").value)
}
}
}
But I havent figured out a way to do this also for the resposes.
If i use getProperty("reponse") it's null of course.
Any hint? :)
and the winner is, I figured it out myself:
map = context.testCase.testSuite.project.interfaces["services"].operations
for (entry in map)
{
opName = entry.getKey()
inputFileRequest = new File("T:\\" + opName + "Request.xml")
inputFileResponse = new File("T:\\" + opName + "Response.xml")
inputFileRequest.write(entry.getValue().createRequest(true))
inputFileResponse.write(entry.getValue().createResponse(true))
}
This is great , even I am also working on the same. As of now I am taking xml request from a folder but I just want to get the Request from WSDL itself and want to get it's parameter.
try{
//Hitting the WSDLs one by one
wsdlList.each
{
wsdl ->
wsdlToHit=wsdl
log.info("WSDL To Hit :" + wsdlToHit)
// Creating an interface
log.info("Before Interface Creation")
iface= WsdlInterfaceFactory.importWsdl( project,wsdl, false )[0]
//iface= WsdlInterfaceFactory.importWsdl( project,WSDLFile, false )[0]
log.info("After Interface Creation")
if(Operation == "xyz")
{
requestXML= requestXML1
responseActual= responseActual1
expectedActual=expectedActual1
}
if(Operation == "abc")
{
requestXML= requestXML2
responseActual= responseActual2
expectedActual=expectedActual2
}
requestXML.each
{
request1 ->
def wsdlReqDir=request1
log.info("RequestLocation : " + wsdlReqDir)
File fl = new File(wsdlReqDir)
File[] wsdlDirFiles = fl.listFiles()
log.info("XML Files in Request Folder : " + wsdlDirFiles)
if(wsdlDirFiles.size()>0)
{
wsdlDirFiles.each
{
wsdlFile->
log.info("Request XML file to Send :" + wsdlFile)
//Calling the function to hit the service
sendRequest(wsdlFile,iface,Operation,Report_File_LOC,requestXML,responseActual,propData)
reportFilewriter.flush()
}
}
}
//removing Interface created
removeInterface(wsdl)
log.info("Removed iface : " + wsdl)
reportFilewriter.flush()
}
Thanks,
Hanumant

Resources