Call another test step using groovy script test step in SoapUI - groovy

I have the Groovy Script test step named Copying_Bulk_File_To_BulkPayment_Folder.
def rawRequest = context.expand('${SOAP#request}')
def file1 = new File('C:/Actimize/4.23.0.30/Instances/actimize_server_1/QA/BulkPayment/NACHA/Input/RegularNachaPPD.ACH')
file1.write (rawRequest)
After that I have other groovy that should called it 10 times, but it is not doing as expected, below is the respective code.
if( context.loopIndex2 == null )
context.loopIndex2 = 0
if( ++context.loopIndex2 < 10 )
testRunner.gotoStepByName( "Copying_Bulk_File_To_BulkPayment_Folder" )

I did have similar experience. Either gotoStepByName does not seem to work or do not know usage of it correctly.
In order get it to work, please do the following change.
From:
testRunner.gotoStepByName("Copying_Bulk_File_To_BulkPayment_Folder")
To:
testRunner.runTestStepByName('Copying_Bulk_File_To_BulkPayment_Folder')
EDIT: OP mentioned that he still has issue without providing the details. Adding another approach to run the test step.
or try below code instead of gotoStepByName statement.
def step = context.testCase.testSteps['Copying_Bulk_File_To_BulkPayment_Folder']
step.run(testRunner, context)

There are 3 problems in your script.
1) gotoStepByName,
change this to runTestStepByName
2) if( ++context.loopIndex2 < 10 )
Since you want a loop better use a for loop instead of an if condition
for(int i=0; i< 10 ; i++ )
3) File name is same all the time i.e. RegularNachaPPD.ACH , even if you copy 1000 times you will not see this since the file name is same, So you should bring a logic by which every time the name of the file is different
int ok= (new Random().nextInt(100000))
print ok
def FileName='C:/Actimize/4.23.0.30/Instances/actimize_server_1/QA/BulkPayment/NACHA/Input/RegularNachaPPD_' + ok + '.ACH'
Below is the code i used and i was able to create 10 files
TestStepName :- test
if( context.loopIndex2 == null )
context.loopIndex2 = 0
for(int i=0; i< 10 ; i++ )
{
testRunner.runTestStepByName( "Copying_Bulk_File_To_BulkPayment_Folder" )
}
TestStepName :- Copying_Bulk_File_To_BulkPayment_Folder
int ok= (new Random().nextInt(100000))
print ok
def FileName='C:/Actimize/4.23.0.30/Instances/actimize_server_1/QA/BulkPayment/NACHA/Input/RegularNachaPPD_' + ok + '.ACH'
def rawRequest = context.expand('${First Step#request}')
def file1 = new File(FileName)
file1.write (rawRequest)
where SOAP is the name of another test step.
The above logic creates a random number which is appended in the name RegularNachaPPD
Hope it helps. I was able to create 10 files with above code

Related

How to call only 1 Test Step from TestSuite using for loop or how to add assertion only in 2 test suite out of 3 test suit

I have to add assertion in 2 test suit out of 3, written below groovy script for adding assertion in all step, but it is considering "Groovy Script" Test Step also, and hence script is getting failed
def testCases = context.testCase.testSuite.getTestCaseList()
testCases.each
{
log.info "~~~Test Case:" + it.name
for(testSteps in it.testStepList)
{
log.info "~~~Test Step:" + testSteps.name
def Assertion5 = testSteps.getAssertionList()
for( a in Assertion5)
{
testSteps.removeAssertion(a)
}
def Assertion6 = testSteps.addAssertion("Contains")
Assertion6.setToken("REST Countries")
}
}
Your help will be appreciated. Thanks in advance.
Of course, groovy scripts steps are considered as testSteps, but testStep contains information about its type that you can filter on.
In your 'for' loop you should add a filter like :
if (testStep.config.type != "groovy")
and then do your processing.
That should work

How to use Groovy script in soapUi to loop multiple time

I am new to SoapUi. I am exploring on how multiple request in soapUi is done using groovy script.
below is the example that im trying to do, based on example that i found through "googling"
import com.eviware.soapui.SoapUI;
import com.eviware.soapui.model.testsuite.*;
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner
import java.util.Random
import com.eviware.soapui.model.testsuite.TestRunner.Status
// Define your testCase pointer
//def testcase = testRunner.testCase.testSuite.project.testSuites["TestSuite - User Management REST API"].getTestCaseByName ("Authenticate User")
def counterUser = testRunner.testCase.testSuite.getPropertyValue( "counter" )
int value = counterUser.toInteger()
String tester = ""
30.times {
value = value + 1
tester = "tester " + value.toString()
testRunner.testCase.testSuite.setPropertyValue( "userName", tester )
testRunner.runTestStepByName("POST - createUser - Create a User")
}
testRunner.testCase.testSuite.setPropertyValue( "counter", value.toString() )
I want to create a 30 users which start from Tester1...tester2.....tester30.
Is it possible to do this way? I keep getting an error such as NullPointerException at this line
int value = counterUser.toInteger()
I got what you say.
That is because, initially there is no value for counter which results to null and you are applying toInteger() over it.
Just change:
From:
int value = counterUser.toInteger()
To:
int value = counterUser?.toInteger() ?: 0

Loop test steps in multiple test cases using SOAPUI

I'm having some issue in automating web services.
Actually I have an excel sheet containing all the input and outputs needed.
I wrote a groovy script that retrieve inputs, save them in properties, execute query, retrieve outputs and compare them with excel outputs.
My problem is that all the process is execute as one test case.
I want to "pimp" my process so that every line of my Excel sheet is dealt as a test case.
Here's the groovy code :
import jxl.*
import jxl.write.*
Workbook workbook1 = Workbook.getWorkbook(new File("C:\\Users\\****\\Desktop\\GroovyPSSheet.xls"))
Sheet sheet1 = workbook1.getSheet(0)
for (int i=6; i<sheet1.getRows(); i++)
{
sleep 1000
if (sheet1.getCell(0,i).getContents()=="")
{
i++
}
Cell clairance = sheet1.getCell(3,i)
Cell etatpatho = sheet1.getCell(2,i)
Cell idlReq = sheet1.getCell(1,i)
Cell idprod = sheet1.getCell(0,i)
Cell typeprod = sheet1.getCell(4,i)
testRunner.testCase.setPropertyValue( "clairance", clairance.getContents() )
testRunner.testCase.setPropertyValue( "etatpatho", etatpatho.getContents() )
testRunner.testCase.setPropertyValue( "idlReq", idlReq.getContents() )
testRunner.testCase.setPropertyValue( "idprod", idprod.getContents() )
testRunner.testCase.setPropertyValue( "typeprod", typeprod.getContents() )
sleep 500
def ExecuteQuery = testRunner.testCase.testSteps['ExecuteQuery']
ExecuteQuery.run( testRunner, context )
sleep 1000
groovyUtils = new com.eviware.soapui.support.GroovyUtils(context )
holder = groovyUtils.getXmlHolder ("ExecuteQuery#Response")
id_type_alerte = holder.getNodeValue("//id_type_alerte")
testRunner.testCase.setPropertyValue( "id_type_alerte", sheet1.getCell(5,i).getContents() )
idproduit = holder.getNodeValue("//idproduit")
testRunner.testCase.setPropertyValue( "idproduit", sheet1.getCell(6,i).getContents() )
typeproduit = holder.getNodeValue("//typeproduit")
testRunner.testCase.setPropertyValue( "typeproduit", sheet1.getCell(7,i).getContents() )
id_ter_per = holder.getNodeValue("//id_ter_per")
testRunner.testCase.setPropertyValue( "id_ter_per", sheet1.getCell(8,i).getContents() )
lib_ter_per = holder.getNodeValue("//lib_ter_per")
testRunner.testCase.setPropertyValue( "lib_ter_per", sheet1.getCell(9,i).getContents() )
id_ter_com = holder.getNodeValue("//id_ter_com")
testRunner.testCase.setPropertyValue( "id_ter_com",sheet1.getCell(10,i).getContents() )
id_typ_ter = holder.getNodeValue("//id_typ_ter")
testRunner.testCase.setPropertyValue( "id_typ_ter", sheet1.getCell(11,i).getContents() )
lib_ter = holder.getNodeValue("//lib_ter")
testRunner.testCase.setPropertyValue( "lib_ter", sheet1.getCell(12,i).getContents() )
id_nature_ci = holder.getNodeValue("//id_nature_ci")
testRunner.testCase.setPropertyValue( "id_nature_ci", sheet1.getCell(13,i).getContents() )
id_ter = holder.getNodeValue("//id_ter")
testRunner.testCase.setPropertyValue( "id_ter", sheet1.getCell(14,i).getContents() )
id_sequence_ter = holder.getNodeValue("//id_sequence_ter")
testRunner.testCase.setPropertyValue( "id_sequence_ter", sheet1.getCell(15,i).getContents() )
id_fic_ci = holder.getNodeValue("//id_fic_ci")
testRunner.testCase.setPropertyValue( "id_fic_ci", sheet1.getCell(16,i).getContents() )
sleep 1000
}
workbook1.close()
Thanks !
Seems that you want to get a next testCase from the current testSuite for each row in the your datasheet instead of performing the operation in the current testCase each time. I'm not sure that this is your goal, but you can collect all the testCases from the current testSuite and select the next one for each row, something like this can do the trick:
// get all testCases from the current testSuite as List
def allTestCases = testRunner.testCase.testSuite.testCases.collect{ name, testCase ->
return testCase
}
// row iteration
for (int i=6; i<sheet1.getRows(); i++)
{
// get a testCase
def testCase = allTestCases.take(1);
// drop the element from the list to change next iteration
allTestCases = allTestCases.drop(1);
...
...
// in the rest of your code use the testCase variable from the list
// instead of using testRunner.testCase which always take the current one
testCase.setPropertyValue("clairance", clairance.getContents())
testCase.setPropertyValue("etatpatho", etatpatho.getContents())
...
def ExecuteQuery = testCase.testSteps['ExecuteQuery']
ExecuteQuery.run( testRunner, context )
...
}
Hope it helps,

SoapUI Load test groovy sequentially reading txt file

I am using free version of soapui. In my load test, I want to read request field value from a text file. The file looks like following
0401108937
0401109140
0401109505
0401110330
0401111204
0401111468
0401111589
0401111729
0401111768
In load test, for each request I want to read this file sequentially. I am using the code mentioned in Unique property per SoapUI request using groovy to read the file. How can I use the values from the file in a sequential manner?
I have following test setup script to read the file
def projectDir = context.expand('${projectDir}') + File.separator
def dataFile = "usernames.txt"
try
{
File file = new File(projectDir + dataFile)
context.data = file.readLines()
context.dataCount = context.data.size
log.info " data count" + context.dataCount
context.index = 0; //index to read data array in sequence
}
catch (Exception e)
{
testRunner.fail("Failed to load " + dataFile + " from project directory.")
return
}
In my test, I have following script as test step. I want to read the current index record from array and then increment the index value
def randUserAccount = context.data.get(context.index);
context.setProperty("randUserAccount", randUserAccount)
context.index = ((int)context.index) + 1;
But with this script, I always get 2nd record of the array. The index value is not incrementing.
You defined the variable context.index to 0 and just do +1
You maybe need a loop to read all values.
something like this :
for(int i=0; i <context.data.size; i++){
context.setProperty("randUserAccount", i);
//your code
}
You can add this setup script to the setup script section for load test and access the values in the groovy script test step using:
context.LoadTestContext.index =((int)context.LoadTestContext.index)+1
This might be a late reply but I was facing the same problem for my load testing for some time. Using index as global property solved the issue for me.
Index is set as -1 initially. The below code would increment the index by 1, set the incremented value as global property and then pick the context data for that index.
<confirmationNumber>${=com.eviware.soapui.SoapUI.globalProperties.setPropertyValue( "index", (com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "index" ).toLong()+1 ).toString()); return (context.data.get( (com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "index" )).toInteger())) }</confirmationNumber>

Jenkins Build Test Case pass fail count using groovy script

I want to fetch Total TestCase PASS and FAIL count for a build using groovy script. I am using Junit test results. I am using Multiconfiguration project , so is there any way to find this information on a per configuration basis?
If you use the plugin https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin, you can access the Jenkins TestResultAction directly from the build ie.
import hudson.model.*
def build = manager.build
int total = build.getTestResultAction().getTotalCount()
int failed = build.getTestResultAction().getFailCount()
int skipped = build.getTestResultAction().getSkipCount()
// can also be accessed like build.testResultAction.failCount
manager.listener.logger.println('Total: ' + total)
manager.listener.logger.println('Failed: ' + failed)
manager.listener.logger.println('Skipped: ' + skipped)
manager.listener.logger.println('Passed: ' + (total - failed - skipped))
API for additional TestResultAction methods/properties http://javadoc.jenkins-ci.org/hudson/tasks/test/AbstractTestResultAction.html
If you want to access a matrix build from another job, you can do something like:
def job = Jenkins.instance.getItemByFullName('MyJobName/MyAxisName=MyAxisValue');
def build = job.getLastBuild()
...
For Pipeline (Workflow) Job Type, the logic is slightly different from AlexS' answer that works for most other job types:
build.getActions(hudson.tasks.junit.TestResultAction).each {action ->
action.getTotalCount()
action.getFailCount()
action.getSkipCount()
}
(See http://hudson-ci.org/javadoc/hudson/tasks/junit/TestResultAction.html)
Pipeline jobs don't have a getTestResultAction() method.
I use this logic to disambiguate:
if (build.respondsTo('getTestResultAction')) {
//normal logic, see answer by AlexS
} else {
// pipeline logic above
}
I think you might be able to do it with something like this:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
File fXmlFile = new File("junit-tests.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
println("Total : " + doc.getDocumentElement().getAttribute("tests"))
println("Failed : " +doc.getDocumentElement().getAttribute("failures"))
println("Errors : " +doc.getDocumentElement().getAttribute("errors"))
I also harvest junit xml test results and use Groovy post-build plugin https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin to check pass/fail/skip counts. Make sure the order of your post-build actions has harvest junit xml before the groovy post-build script.
This example script shows how to get test result, set build description with result counts and also version info from a VERSION.txt file AND how to change overall build result to UNSTABLE in case all tests were skipped.
def currentBuild = Thread.currentThread().executable
// must be run groovy post-build action AFTER harvest junit xml
testResult1 = currentBuild.testResultAction
currentBuild.setDescription(currentBuild.getDescription() + "\n pass:"+testResult1.result.passCount.toString()+", fail:"+testResult1.result.failCount.toString()+", skip:"+testResult1.result.skipCount.toString())
// if no pass, no fail all skip then set result to unstable
if (testResult1.result.passCount == 0 && testResult1.result.failCount == 0 && testResult1.result.skipCount > 0) {
currentBuild.result = hudson.model.Result.UNSTABLE
}
currentBuild.setDescription(currentBuild.getDescription() + "\n" + currentBuild.result.toString())
def ws = manager.build.workspace.getRemote()
myFile = new File(ws + "/VERSION.txt")
desc = myFile.readLines()
currentBuild.setDescription(currentBuild.getDescription() + "\n" + desc)

Resources