i wanto run 5 times a testcase in soapui using groovy.
i found the following code in Stackoverflow and added the loop part but it executes randomly.
Sometimes it executes 2 times and sometimes 4 times.
here is the code:
def a = 5;
while (a>0) {
a--;
def testCase = testRunner.testCase.testSuite.testCases["TestCase"];
def properties = new com.eviware.soapui.support.types.StringToObjectMap();
def async = false;
testCase.run(properties, async);
}
Thank you
def doSomething() {
println 'Foo Bar'
}
5.times {
doSomething()
}
First thing, you do not want to def testCase in your script, as testCase is usually defined for you.
def myTestCase = testSuite.getTestCaseByName("some TestCase")
def myContext = (com.eviware.soapui.support.types.StringToObjectMap)context
5.times {
myTestCase.run(myContext, false)
}
You also did not specify where you are doing this from. The above code will work from test suite Setup Script. If you are doing it elsewhere, you will probably need to adjust def myTestCase.
I have following script in Setup Script of a test Suite and it is running indefinite loop:
def count=context.expand('${#Global#run}')
log.info count
def project1 = runner.testCase.testSuite.project
def properties = new com.eviware.soapui.support.types.StringToObjectMap()
def testcase = project1.getTestSuiteByName("TestSuite 1").getTestCaseByName("Login");
def testcase1 = project1.getTestSuiteByName("TestSuite 1").getTestCaseByName("Logout");
// This will run everything in the selected project
for(int i=1;i<=count;i++){
// testRunner = testcase.run(new com.eviware.soapui.support.types.StringToObjectMap(), false)
def myContext = (com.eviware.soapui.support.types.StringToObjectMap)context
testcase.run(myContext, false)
sleep(70000)
//testRunner = testcase1.run(new com.eviware.soapui.support.types.StringToObjectMap(), false)
testcase1.run(myContext, false)
sleep(5000)
log.info( "Finished running "+i+" cycle" )
if(i==count){
testRunner.cancel('Test Execution is completed')
break;
}
}
Related
Need to access the variable defined in the groovy script file
Before executing the script using GroovyShell
However, I am getting error:
groovy.lang.MissingPropertyException: No such property: _THREADS for class: Test1
//Test1.groovy
_THREADS=10;
println("Hello from the script test ")
//scriptRunner.groovy
File scriptFile = new File("Test1.groovy");
def sharedData = new Binding()
def shell = new GroovyShell(sharedData)
def script = shell.parse (scriptFile);
def thread = script.getProperty('_THREADS'); // getting ERROR --
// No such property: _THREADS for class: Test1
//-------
//now run the threads
script.run()
you have to run script before getting property/binding
def sharedData = new Binding()
def shell = new GroovyShell(sharedData)
def script = shell.parse (''' _THREADS=10 ''')
script.run()
println script.getProperty('_THREADS')
try to open AST Browser (Ctrl+T) in groovy console for this code:
_THREADS=10
and you'll see approximately following generated class:
public class script1663101205410 extends groovy.lang.Script {
public java.lang.Object run() {
_THREADS = 10
}
}
where _THREADS = 10 assigns property into binding
however it's possible to define _THREADS as a function then it will be accessible without running script.
def sharedData = new Binding()
def shell = new GroovyShell(sharedData)
def script = shell.parse (''' def get_THREADS(){ 11 } ''')
println script.getProperty('_THREADS')
got the solution using #Field modifier value can be accessed after parsing
import groovy.transform.Field
#Field _THREADS=10
File scriptFile = new File("Test1.groovy");
def sharedData = new Binding()
def shell = new GroovyShell(sharedData)
def script = shell.parse (scriptFile);
def thread = script.getProperty('_THREADS'); //value is accessible
I have a Java app through which I'm executing a Groovy Script, the problem occurs when my Script again has code to use the groovy shell to execute the Inner script.
something like this
import com.xx.WrappedGroovyShell
import java.lang.*
def scriptString = """
def test(str) {
str.toLowerCase() // This doesn't work as GString can't seem to be treated as a String
}
"""
try {
def script = WrappedGroovyShell.getInstance().getScript("Test1", scriptString)
def script2 = new GroovyShell().parse(scriptString)
def example = 1
def gstring = "OUR VALUE IS ${example}";
println script instanceof GroovyObject // Statement returning false
println script.test(gstring) // This throws an exception groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.runtime.GStringImpl.toLowerCase() is applicable for argument types: () values: []
println script2 instanceof GroovyObject // Statement returns true
println script2.test(gstring) //this one works
println "Success"
} catch (ex) {
println ex
}
return 0
The getScript method in WrappedGroovyShell.java is as simple as
public Script getScript(String scriptName, String scriptBody){
return new GroovyShell().parse(scriptBody);
}
I will be thankful if someone let me know why two different behavior.
new GroovyShell() creates a new groovy classloader with parent = GroovyShell.class.getClassLoader()
and if GString.class is unknown for parent classloader - then it will be loaded again.
two GString classes loaded by different classloaders are not equal that's why you could have unexpected exceptions like "method not found for parameter GString" or "can't cast GString to GString"
this code should work because it uses classloader of current groovy class
def scriptText='''
def test(str) {
str.toLowerCase()
}
'''
def gs = new GroovyShell(this.getClass().getClassLoader())
def script = gs.parse(scriptText)
def arg = "GSTRING ${123}"
println script.test(arg)
Below is the tear down script to get the all test case counts and status from suite tear down but i need to get the overall test suite counts and status from project tear down but unfortunately it will not support the object of test runner class.
def failedTestCases = 0
runner.results.each { testCaseResult ->
def name = testCaseResult.testCase.name
if(testCaseResult.status.toString() == 'FAILED') {
failedTestCases++
log.info "$name has failed"
testCaseResult.results.each { testStepResults ->
testStepResults.messages.each { msg -> log.info msg }
}
} else {
log.info "$name works correctly"
}
}
On your project, create a Setup Script:
project.setPropertyValue("testcasesRun","0")
project.setPropertyValue("testcasesFailed","0")
On each of your TestSuites, create a TearDown Script something along this:
def failedTestCases = 0
def testCasesRun = 0
runner.results.each { testCaseResult ->
testCasesRun++
if(testCaseResult.status == com.eviware.soapui.model.testsuite.TestRunner.Status.FAILED) {
failedTestCases++
}
}
def existingFailedTestCases = new Integer( runner.testSuite.project.getPropertyValue("testcasesFailed") )
def newFailedValue = existingFailedTestCases + failedTestCases
runner.testSuite.project.setPropertyValue("testcasesFailed",newFailedValue.toString())
def existingRunTestCases = new Integer( runner.testSuite.project.getPropertyValue("testcasesRun") )
def newRunTestCases = existingRunTestCases + testCasesRun
runner.testSuite.project.setPropertyValue("testcasesRun",newRunTestCases.toString())
After running the project, you should have two Project Properties, holding number of counted testcases and failed testcases.
SoapUI has options to run your test-suites and test-cases in parallel but no such thing for doing it with test-steps.
How can I achieve such a thing with a Groovy teststep inside my testcase?
This is my current code:
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner
//Get all Soap type test steps within the testCases
for ( testStep in testRunner.testCase.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep.class)){
//Print out the name for the testStep
tsname = testStep.getName();
log.info tsname;
//thread = new Thread("$tsname")
def th = new Thread("$tsname") {
#Override
public void run(){
//Set the TestRunner to the respective TestCase
TestRunner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(testRunner.testCase, null);
//Run them all and then rejoin the threads
log.info("thread: " + tsname)
TestRunner.runTestStepByName(tsname);
def soapResponse = TestRunner.getTestCase().getTestStepByName(tsname).getProperty("Response");
log.info "soapResponse: " + soapResponse;
th.join();
}
}
th.start();
th.join();
}
Managed to fix it myself and posted the answer here for all who stumble into the same problem:
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
List<String> steps = testRunner.testCase.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep.class)
def map = [:]
//define the threads list, this will hold all threads
def threads = []
def kickEm = steps.each{ step ->
def th = new Thread({
stepName = step.getName();
log.info "Thread in start: " + step.getName();
//Set the TestRunner to the respective TestCase
TestRunner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(testRunner.testCase, null);
//Run the corresponding teststep
TestRunner.runTestStepByName(step.getName());
//Get the response of the current step
def soapResponse = context.expand(TestRunner.getTestCase().getTestStepByName(step.getName()).getPropertyValue('Response')) as String;
log.info "In thread "+step.getName()+" soapResponse: " + soapResponse
//Put this into a map, the key is the stepname and the value is its response, so we can retrieve it all outside the each loop
map.put(step.getName(), soapResponse);
})
log.info "Putting new Thread({..}) th back into the threads list";
threads << th;
}
threads.each { it.start(); }
threads.each { it.join(); }
log.info "Map: "
map.each { step, response ->
log.info "Step: ${step} has the response ${response}"
};
log.info "Done!"
I have generate a report based on an execution of a test suite where it creates a folder directory and insert a file displaying the report. This is compiled within a TearDown Script at Test Suite level. Below is the code:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def dataFolder = groovyUtils.projectPath
def failedTestCases = 0
def succeedTestCases = 0
def totalTestCases = 0
def testCaseFailed = ""
def testCaseSucceed = ""
def date = new Date()
def folderTime = date.format("yyyy-MM-dd HH-mm-ss")
def hotelId = context.getProperty('hotelid')
def hotelname = context.getProperty('hotelname')
def hoteltype = context.getProperty('hoteltype')
//def propertyValues = ""
//def correlationid = messageExchange.modelItem.testStep.testCase.testSuite.Project.namegetPropertyValue("correlationid")
//Create a folder directory for the responses
RootResultFolder = dataFolder + "\\Test Reports" + "\\xxx_WebAPI - " + folderTime + "\\"
CreateResultFolder = new File(RootResultFolder)
CreateResultFolder.mkdir()
//context.setProperty("RootResultFolder", RootResultFolder)
def fileName = "WebAPI Test Report.txt"
def rootFolder = RootResultFolder + fileName
def logFile = new File(rootFolder)
if(logFile.exists())
{
log.info("Error a file named " + fileName + "already exisits")
}
else
{
runner.results.each { testCaseResult ->
def name = testCaseResult.testCase.name
totalTestCases++
if(testCaseResult.status.toString() == 'FAILED'){
failedTestCases ++
testCaseFailed += "- $name - HAS FAILED \n\n"
//propertyValues += "hotelid - $hotelid, hotelname - $hotelname, hoteltype - $hoteltype \n\n"
testCaseResult.results.each{ testStepResults ->
testStepResults.messages.each() { msg -> log.info msg }
}
}else{
succeedTestCases ++
testCaseSucceed += "- $name - SUCCEED \n\n"
testCaseResult.results.each{ testStepResults ->
testStepResults.messages.each() { msg -> log.info msg }
}
}
}
}
logFile.write "TOTAL TEST CASES SUCCEED: $succeedTestCases of $totalTestCases" + "\n\n" +
testCaseSucceed + "---\n\n" +
"TOTAL TEST CASES FAILED: $failedTestCases of $totalTestCases" + "\n\n" +
testCaseFailed + "\n\n"
What I actually want to do is move the code from Test Suite level and place it in the tear down script at Project level. Now when I run the code from there, it does not generate the file, I'm assuming I need to place the correct paths in as I am not moving to test suite to test case but from project to test suite to testcase to test steps.
My question is really on syntax, I want to develop a report when the whole project is run, it outputs the following results:
Project Name - is it success or failed. If one suite failed then project fails else it passes
Test Suite - Take name of each test suite in project and if passes then place 'Succeed' next to name of test suite else place 'Failed' next to name of test suite
Name of all test cases within test suite. Like the one in screenshot really, 'succeed' next to test cases that have passed and 'failed' next to those that haven't.
Finally the property values. If a test case has failed, capture the property values for that failed test case so we can track which values were entered that caused the failure of the test.
Can somebody help me with the relevant syntax to perform these so then I can peice it into my code and manipulate?
UPDATE:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def dataFolder = groovyUtils.projectPath
def date = new Date()
def folderTime = date.format("yyyy-MM-dd HH-mm-ss")
//Create a folder directory for the responses
RootResultFolder = dataFolder + "\\Test Reports" + "\\xxx - " + folderTime + "\\"
CreateResultFolder = new File(RootResultFolder)
CreateResultFolder.mkdir()*/
//context.setProperty("RootResultFolder", RootResultFolder)
def reportFileName = "WebAPI Test Report.txt"
def rootFolder = RootResultFolder + reportFileName
def logFile = new File(rootFolder)
If you look at the TearDown Script of the project, it shows as below i.e., the variables already initialized by soapui.
Issue with your script
So if you look at it, there is runner variable. Also the same variable is available at TearDown script of test suite level. But, these are instances of different Objects. The script used in OP was of suite level which you aware and that is why you are not seeing in the result.
Here is the project level TearDown Script and following in line comments.
/**
*
* Below is the TearDown script for SoapUI Project level
* Which create a custom report in a given file
* Modify the variable "reportFileName" below
*
**/
//Modify the file as needed for report file
//def reportFileName = '/tmp/abctestreport.txt'
//Adding the below as user wants specific directory
//Get the project path
def dataFolder = new com.eviware.soapui.support.GroovyUtils(context).projectPath
//Create today's date for storing response
def today = new Date().format("yyyy-MM-dd")
def filePrefix = "${dataFolder}/TestReports/xxx_WebAPI_${today}" as String
def fileNamePart = new Date().format("yyyy-MM-dd'T'HH.mm.ss")
//creating filename dynamically.
def reportFileName = "${filePrefix}/xxx_WebAPI_TestReport_${fileNamePart}.txt" as String
//NOTE: Not required to edit beyond this point
/**
* This class holds the test case details
**/
class TestCaseResultHolder {
def log
Map<String, String> properties = [:]
boolean status
def createProperties(testCase) {
testCase.getPropertyNames().each { key ->
properties[key] = testCase.getPropertyValue(key)
}
}
def getCaseResult(caseRunner, caseName) {
log.info "Checking test case status ${caseName}"
if ( caseRunner.status.toString() == 'FAILED' ){
log.error "Test case $caseName has failed"
for ( stepResult in caseRunner?.results ){
stepResult.messages.each() { msg -> log.info msg }
}
return false
} else {
log.info "${caseName} is passed"
}
true
}
def buildCaseResult(caseRunner, caseName) {
status = getCaseResult(caseRunner, caseName)
if (!status) {
createProperties(caseRunner.testCase)
}
}
}
/**
* This class holds the test suite details
**/
class SuiteResultsHolder {
def log
Map<String, TestCaseResultHolder> casaeResults = [:]
int testCaseCount = 0
int passedCasesCount = 0
int failedCasesCount = 0
def buildSuiteResults(suiteRunner, suiteName){
log.info "Building results of test suite ${suiteName}"
for ( caseRunner in suiteRunner?.results ) {
def caseName = caseRunner.testCase.name
testCaseCount++
def tcHolder = new TestCaseResultHolder(log: log)
tcHolder.buildCaseResult(caseRunner, caseName)
casaeResults[caseName] = tcHolder
if (tcHolder.status) {
passedCasesCount++
} else {
failedCasesCount++
}
}
}
def getStatus() {
(0 < failedCasesCount) ? false : true
}
}
/**
* This class holds the project details
**/
class ProjectResultsHolder {
def log
Map<String, SuiteResultsHolder> suiteResults = [:]
int suiteCount = 0
int passedSuitecount = 0
int failedSuiteCount = 0
def buildProjectResults(projectRunner, projectName) {
log.info "Building results of test project ${projectName}"
for(suiteRunner in projectRunner?.results) {
def suiteName = suiteRunner.testSuite.name
suiteCount++
def suiteResultsHolder = new SuiteResultsHolder(log: log)
suiteResultsHolder.buildSuiteResults(suiteRunner, suiteName)
suiteResults[suiteName] = suiteResultsHolder
if (suiteResultsHolder.status) {
passedSuitecount++
} else {
failedSuiteCount++
}
}
}
def getStatus() {
(0 < failedSuiteCount) ? false : true
}
}
//Get the status string based on boolean
def getResult(status){ status == true ? 'SUCCEED' : 'FAILED'}
//Draws a line
def drawLine(def letter = '=', def count = 70) { letter.multiply(count)}
//Gets the summary report
def getSummaryReport(project, projectResultHolder) {
def report = new StringBuffer()
report.append(drawLine()).append('\n')
report.append("\t\t\tTest Execution Summary\n")
report.append(drawLine('-', 60)).append('\n')
report.append("Project : ${project.name}\n")
report.append("Result : ${getResult(projectResultHolder.status)}\n")
report.append("Total test suites executed: ${projectResultHolder.suiteCount}\n")
report.append("Test suites passed: ${projectResultHolder.passedSuitecount}\n")
report.append("Test suites failed: ${projectResultHolder.failedSuiteCount}\n")
report.append(drawLine()).append('\n')
report
}
//Gets the test case report
def getTestCaseReport(testCaseReport) {
def report = new StringBuffer()
report.append(drawLine('-', 60)).append('\n')
report.append("\t\tTest Case Details:\n")
report.append(drawLine('-', 60)).append('\n')
testCaseReport.each { kase, tcReport ->
report.append("Name : ${kase}\n")
report.append("Status : ${getResult(tcReport.status)}\n")
if (!tcReport.status) {
report.append("Properties : ${tcReport.properties.toString()}\n")
}
}
report
}
//Get the detailed report
def getDetailedReport(projectResultHolder) {
def report = new StringBuffer()
report.append(drawLine()).append('\n')
report.append("\t\t\tTest Execution Detailed Report\n")
report.append(drawLine()).append('\n')
projectResultHolder.suiteResults.each { suite, details ->
report.append("Test Suite : ${suite}\n")
report.append("Result : ${getResult(details.status)}\n")
report.append("Total Cases : ${details.testCaseCount}\n")
report.append("Cases Passed : ${details.passedCasesCount}\n")
report.append("Cases Failed: ${details.failedCasesCount}\n")
report.append(getTestCaseReport(details.casaeResults))
report.append(drawLine()).append('\n')
report.append(drawLine()).append('\n')
}
report
}
//Save the contents to a file
def saveToFile(file, content) {
if (!file.parentFile.exists()) {
file.parentFile.mkdirs()
log.info "Directory did not exist, created"
}
file.write(content)
assert file.exists(), "${file.name} not created"
}
def holder = new ProjectResultsHolder(log: log)
holder.buildProjectResults(runner, project.name)
def finalReport = new StringBuffer()
finalReport.append(getSummaryReport(project, holder))
finalReport.append(getDetailedReport(holder))
def reportFile = new File(reportFileName)
saveToFile(reportFile, finalReport.toString())
And here is the generated output: