dva1946
13 years agoOccasional Contributor
Groovy Script Loop HOWTO
This will be a quick HOWTO with hopes of adding much more shorty.
1. Set variables at the TESTSUITE level. Double-click on testsuite, click on properties in the bottom of the larger window. Set things like DB login here.
2. Set testcase-related variables at the TESTCASE level. Use same process.
3. Test Steps (basic):
Datagen File
Groovy Script - Goto
Property Transfer
TestStepA
TestStepB
Groovy Script - Main Loop
4. Groovy Script - Goto (a single line is all that is necessary)
testRunner.gotoStepByName( "Groovy Script - Main Loop")
5. Groovy Script - Main Loop
Below is an actual script which has been modified to not show private information, but it is A FULLY FUNCTIONAL SCRIPT. You should be able to COPY THIS SCRIPT AS-IS and modify for your needs.
// Database connection:
// All these properties are defined at the TEST SUITE LEVEL
// Properties used:
// DenServer25
// Password
// sid
// Database
// DBport
// Extract global property value as local Variables
def scriptDenServer25 = context.expand( '${#TestSuite#DenServer25}' )
def scriptPassword = context.expand( '${#TestSuite#Password}' )
def scriptSid = context.expand( '${#TestSuite#sid}' )
def scriptDatabase = context.expand( '${#TestSuite#Database}' )
def scriptDBport = context.expand( '${#TestSuite#DBport}' )
//** Resolve db connection **//
import groovy.sql.Sql
import com.eviware.soapui.support.GroovyUtils.*
log.info "Connecting to DB here";
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver("oracle.jdbc.driver.OracleDriver")
//***************************************//
log.info "Running Query for TestCase - My R&D";
sql = Sql.newInstance("jdbc:oracle:thin:@${scriptDenServer25}:${scriptDBport}:${scriptSid}", "${scriptDatabase}", "${scriptPassword}", "oracle.jdbc.pool.OracleDataSource")
// **TESTCASE** //
def scriptMOD = context.expand( '${#TestCase#MOD}' )
def scriptLIMIT = context.expand( '${#TestCase#LIMIT}' )
// **MAIN QUERY ** //
def aa = 0
def bb = 0
sql.eachRow("select offering_id from h_offering where service_type = ${scriptMOD} and deleted_flag = 0 and ROWNUM <= ${scriptLIMIT} order by offering_id") { row ->
testRunner.testCase.setPropertyValue( "dbofferingID", "$row.OFFERING_ID" )
log.info "Set Property dbofferingID = $row.OFFERING_ID";
// Now run desired test steps. Must send each individually.
testRunner.runTestStepByName("Property Transfer to ABC");
testRunner.runTestStepByName("getMyStuff");
//** Add loop to only run a step one time **//
while (aa < 1) {
testRunner.runTestStepByName("getAuth");
testRunner.runTestStepByName("isAct");
testRunner.runTestStepByName("getAuthServ");
aa++
}
//******************************************//
testRunner.runTestStepByName("Delete It");
bb++
}
log.info "Records Processed = $bb";
sql.close()
// END OF GROOVY SCRIPT - MAIN LOOP //
6. DESIGN THEORY:
a. The objective is a compact Test Suite / Test Case which connects to a database for all it's looping data. This of course eliminated HARD-CODED most source data.
b. Before you can use this design, you must understand properties at the TESTSUITE & TESTCASE level.
c. TESTSUITE properties provide DATABASE CONNECTIONS.
d. TESTCASE properties allow setting and retrieval of values within the testcase. This provides the real power for the looping process.
e. First you build a teststep for Datagen static values.
f. Next you add a "Groovy Script-Goto" teststep.
g. Next add two simple (and known) teststeps.
h. Now add the "Groovy Script - Main Loop" teststep.
i. Now edit this script and make it run.
j. For now, I am not going to explain this script, as it already has lots of comments.
LEARNING LOOP DESIGN
This was not easy, in fact it took weeks of research, trial-n-error and failure before everything in it was mastered.
Have fun!
1. Set variables at the TESTSUITE level. Double-click on testsuite, click on properties in the bottom of the larger window. Set things like DB login here.
2. Set testcase-related variables at the TESTCASE level. Use same process.
3. Test Steps (basic):
Datagen File
Groovy Script - Goto
Property Transfer
TestStepA
TestStepB
Groovy Script - Main Loop
4. Groovy Script - Goto (a single line is all that is necessary)
testRunner.gotoStepByName( "Groovy Script - Main Loop")
5. Groovy Script - Main Loop
Below is an actual script which has been modified to not show private information, but it is A FULLY FUNCTIONAL SCRIPT. You should be able to COPY THIS SCRIPT AS-IS and modify for your needs.
// Database connection:
// All these properties are defined at the TEST SUITE LEVEL
// Properties used:
// DenServer25
// Password
// sid
// Database
// DBport
// Extract global property value as local Variables
def scriptDenServer25 = context.expand( '${#TestSuite#DenServer25}' )
def scriptPassword = context.expand( '${#TestSuite#Password}' )
def scriptSid = context.expand( '${#TestSuite#sid}' )
def scriptDatabase = context.expand( '${#TestSuite#Database}' )
def scriptDBport = context.expand( '${#TestSuite#DBport}' )
//** Resolve db connection **//
import groovy.sql.Sql
import com.eviware.soapui.support.GroovyUtils.*
log.info "Connecting to DB here";
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver("oracle.jdbc.driver.OracleDriver")
//***************************************//
log.info "Running Query for TestCase - My R&D";
sql = Sql.newInstance("jdbc:oracle:thin:@${scriptDenServer25}:${scriptDBport}:${scriptSid}", "${scriptDatabase}", "${scriptPassword}", "oracle.jdbc.pool.OracleDataSource")
// **TESTCASE** //
def scriptMOD = context.expand( '${#TestCase#MOD}' )
def scriptLIMIT = context.expand( '${#TestCase#LIMIT}' )
// **MAIN QUERY ** //
def aa = 0
def bb = 0
sql.eachRow("select offering_id from h_offering where service_type = ${scriptMOD} and deleted_flag = 0 and ROWNUM <= ${scriptLIMIT} order by offering_id") { row ->
testRunner.testCase.setPropertyValue( "dbofferingID", "$row.OFFERING_ID" )
log.info "Set Property dbofferingID = $row.OFFERING_ID";
// Now run desired test steps. Must send each individually.
testRunner.runTestStepByName("Property Transfer to ABC");
testRunner.runTestStepByName("getMyStuff");
//** Add loop to only run a step one time **//
while (aa < 1) {
testRunner.runTestStepByName("getAuth");
testRunner.runTestStepByName("isAct");
testRunner.runTestStepByName("getAuthServ");
aa++
}
//******************************************//
testRunner.runTestStepByName("Delete It");
bb++
}
log.info "Records Processed = $bb";
sql.close()
// END OF GROOVY SCRIPT - MAIN LOOP //
6. DESIGN THEORY:
a. The objective is a compact Test Suite / Test Case which connects to a database for all it's looping data. This of course eliminated HARD-CODED most source data.
b. Before you can use this design, you must understand properties at the TESTSUITE & TESTCASE level.
c. TESTSUITE properties provide DATABASE CONNECTIONS.
d. TESTCASE properties allow setting and retrieval of values within the testcase. This provides the real power for the looping process.
e. First you build a teststep for Datagen static values.
f. Next you add a "Groovy Script-Goto" teststep.
g. Next add two simple (and known) teststeps.
h. Now add the "Groovy Script - Main Loop" teststep.
i. Now edit this script and make it run.
j. For now, I am not going to explain this script, as it already has lots of comments.
LEARNING LOOP DESIGN
This was not easy, in fact it took weeks of research, trial-n-error and failure before everything in it was mastered.
Have fun!