Groovy Script Technical solutions:
Groovy script plays vital role in automating the web services using the SOAPUI tool. As tester with my exp, no need to be expert in the grovey to build the regression suite in web service, however as tester you should be knowing the basic syntax, conditions and some of the groovy functions.
To make the job easy, I've listed out some of the functions which are very useful in the real execution.
lets talk about the groovy.
Groovy is an Object Oriented Scripting Language, Groovy and Java are really close cousins, and their syntaxes are very similar, hence why Groovy is so easy to learn for Java developers.
SoapUI provides extensive options for scripting, using either Groovy or Javascript (since SoapUI 3.0) as its scripting language, which to use is set a the project level in the project details tab at the bottom left.
good site to refer groovey script
https://www.soapui.org/scripting-properties/tips-tricks.html
1. Defining a variable called 'name' in Groovy and assigning it with a value.
def name = "John"
log.info name
def number = 10
log.info number
2. How to print results in Console
def status ="Testcase pass"
log.info status
3. IF Condition Syntax
//IF CONDITION
if (number == 10)
{
log.info "Pass"
}
elseh
{
log.infor "fail"
}
4. FOR Condition Syntax
//FOR CONDITION'
for(def i=1;i<10 font="" i="">10>
{
log.info "Value of I is "+i
}
5. While Condition Syntax
def i = 1
while (i<5 font="">5>
{
log.info "I value is :" + i
i=i+1
}
6. How to define Arrays in Grovey script
def number =[10,2,3]
log.info number[0]
log.info number[1]
log.info number[3]
********************************************************************************* Some of the very useful Grovy functions
7. How to retrieve the current project path - this would be very helpful in retriving the test data path and also helps in storing the results.
import com.eviware.soapui.support.GroovyUtils
import groovy.xml.XmlUtil
def groovyUtils = new GroovyUtils(context)
def projectdir = groovyUtils.projectPath
8. How to control the environments.
def String pEnv = testRunner.testCase.getPropertyValue("sEnv") // Get the value from the configuration file
// log.info(pEnv)
if (pEnv == "Local")
{
sEndpoint = "http://www.w3schools.com/webservices/tempconvert.asmx. " // Local environment
}
else if (pEnv == "Test")
{
sEndpoint = "http://www.w3schools.com/test/webservices/tempconvert.asmx" //Test environment
}
else
{
sEndpoint = "http://www.w3schools.com/prod/webservices/tempconvert.asmx" // Prod environment
}
//set the end point details
testRunner.testCase.setPropertyValue("pEndpoint",sEndpoint)
// Use the pEndpoint property variable in the test request - end point.
${#TestCase#pEndpoint}
4. How to get the SOAPUI project name in groovy script
def projectname= testRunner.testCase.testSuite.project.name
log.info(projectname)
or
def project = context.testCase.testSuite.project.name
log.info(project)
Project Path
def projectpath = testRunner.testCase.testSuite.project.path
log.info projectpath
5. How to get the Test case name in grovey script
def testcase = testRunner.getTestCase().name
log.info(testcase)
or
def testcase= testRunner.testCase.name
log.info(testcase)
6. How to get the Test suite name in grovey script
def suitename= testRunner.testCase.testSuite.name
log.info(suitename)
7. How to call the test step from the grovey script
def result = testRunner.runTestStep(testRunner.getTestCase().getTestStepByName("Step name"))
// Capture result in the string format
String status = result.getStatus().toString()
//display result
log.info status
8. How to get test step name
def teststep = testRunner.getTestCase().getTestStepByName("CelsiusToFahrenheit - Request 1")
log.info(teststep)
or
def teststep = testRunner.testCase.getTestStepByName("CelsiusToFahrenheit - Request 1")
log.info(teststep)
or
def step = context.testCase.getTestStepAt(0) //0 means first step,1 means second step e.t.c
log.info step.name
9. How to capture the SOAP response using the grovey script.
// groovyUtils.getXmlHolder("Request name# Response)
sResponse = groovyUtils.getXmlHolder("CelsiusToFahrenheit - Request 1"+"#Response")
sActualValue = sResponse.getNodeValue("//*:CelsiusToFahrenheitResult")
if (responseval == "50")
{
log.info "Test case pass"
}
else
{
log.info "test case fail"
}
10. How to get the date in groovy script
import java.lang.*
import java.util.*
import java.io.*
def startDate = new Date()
def datform= startDate.format("yyyy-MM-dd-HH-mm")
11. How to pass the XML from an external source to SOAP ui tool?
//Get the Test data path
def testdatapath=projectdir+"\\TestData\\"+testRunner.testCase.getPropertyValue("sTestcasename")+".xml"
def xmlParser = new XmlParser()
responsecontent = xmlParser.parse(testdatapath)
def finaltestdata =XmlUtil.serialize( responsecontent )
//log.info(finaltestdata)
//Assign xml data to property values
testRunner.testCase.setPropertyValue("pfinaltestdata",finaltestdata)
// pfinaltestdata - property variable contains the XML information.
12. Data driven in SOAPUI tool - no built in feature avaible in SOAP ui for data driven, ready api tool has a built in feature to support the data driven.
To make the data driven in soap ui tool either use the Apache POI library to write and Read data from the Excel.
or
define input data in CSV and retrieve the data from the CSV, write a function to retrive the data from csv.
Below code is to retrieve the data from csv and run for each iteration.
import java.lang.*
import java.util.*
import java.io.*
def sTestdatafile ="inputdata.csv"
//get the path of testdata file
def controlfilepath=projectdir+"\\TestData\\+sTestdatafile
//log.info(controlfilepath)
// projectdir = how to get the project dir, pls refer the question no 7
//Read file
File file = new File(controlfilepath)
BufferedReader reader = new BufferedReader(new FileReader(file))
String line=null
row = 1
while ((line = reader.readLine()) != null)
{
//Avoid first line, as those are columns
if (row >1)
{
// log.info "print number value in if condition:"+number
String[] dataval = line.split(",")
// Assign values to the property
testRunner.testCase.setPropertyValue("sEnv",dataval[1])
testRunner.testCase.setPropertyValue("sTestcasename",dataval[2])
testRunner.testCase.setPropertyValue("sCustomerNo",dataval[3])
testRunner.testCase.setPropertyValue("sCustomerName",dataval[3])
testRunner.testCase.setPropertyValue("sExpected",dataval[3])
//### Calling the submit request - Pass the data for each iteration to the submit request ##########
result = testRunner.runTestStep(testRunner.getTestCase().getTestStepByName("Submit Request"))
sleep 2000
}
row = row+1
}
13. How to get the custom properies?
Custom properties from project level:
def endpoint = context.expand('${#Project#custome property name}')
log.info endpoint
or
def test =testRunner.testCase.testSuite.project.getPropertyValue( "custom property name")
log.info test
Test suite level
def testsuite = context.expand( '${#TestSuite#custom property}' )
log.info testsuite
or
- def sTestSuite = testRunner.testCase.testSuite.getPropertyValue( "custom property name")
- log.info sTestSuite
Test case level
def testcase = context.expand('${#TestCase#custom property}' )
log.info testcase
4. How to get the SOAPUI project name in groovy script
def projectname= testRunner.testCase.testSuite.project.name
log.info(projectname)
or
def project = context.testCase.testSuite.project.name
log.info(project)
Project Path
def projectpath = testRunner.testCase.testSuite.project.path
log.info projectpath
5. How to get the Test case name in grovey script
def testcase = testRunner.getTestCase().name
log.info(testcase)
or
def testcase= testRunner.testCase.name
log.info(testcase)
6. How to get the Test suite name in grovey script
def suitename= testRunner.testCase.testSuite.name
log.info(suitename)
7. How to call the test step from the grovey script
def result = testRunner.runTestStep(testRunner.getTestCase().getTestStepByName("Step name"))
// Capture result in the string format
String status = result.getStatus().toString()
//display result
log.info status
def result = testRunner.runTestStep(testRunner.getTestCase().getTestStepByName("Step name"))
// Capture result in the string format
String status = result.getStatus().toString()
//display result
log.info status
8. How to get test step name
def teststep = testRunner.getTestCase().getTestStepByName("CelsiusToFahrenheit - Request 1")
log.info(teststep)
or
def teststep = testRunner.testCase.getTestStepByName("CelsiusToFahrenheit - Request 1")
log.info(teststep)
or
def step = context.testCase.getTestStepAt(0) //0 means first step,1 means second step e.t.c
log.info step.name
def step = context.testCase.getTestStepAt(0) //0 means first step,1 means second step e.t.c
log.info step.name
9. How to capture the SOAP response using the grovey script.
// groovyUtils.getXmlHolder("Request name# Response)
sResponse = groovyUtils.getXmlHolder("CelsiusToFahrenheit - Request 1"+"#Response")
sActualValue = sResponse.getNodeValue("//*:CelsiusToFahrenheitResult")
if (responseval == "50")
{
log.info "Test case pass"
}
else
{
log.info "test case fail"
}
import java.lang.*
import java.util.*
import java.io.*
def startDate = new Date()
def datform= startDate.format("yyyy-MM-dd-HH-mm")
11. How to pass the XML from an external source to SOAP ui tool?
//Get the Test data path
def testdatapath=projectdir+"\\TestData\\"+testRunner.testCase.getPropertyValue("sTestcasename")+".xml"
def xmlParser = new XmlParser()
responsecontent = xmlParser.parse(testdatapath)
def finaltestdata =XmlUtil.serialize( responsecontent )
//log.info(finaltestdata)
//Assign xml data to property values
testRunner.testCase.setPropertyValue("pfinaltestdata",finaltestdata)
// pfinaltestdata - property variable contains the XML information.
12. Data driven in SOAPUI tool - no built in feature avaible in SOAP ui for data driven, ready api tool has a built in feature to support the data driven.
To make the data driven in soap ui tool either use the Apache POI library to write and Read data from the Excel.
or
define input data in CSV and retrieve the data from the CSV, write a function to retrive the data from csv.
Below code is to retrieve the data from csv and run for each iteration.
import java.lang.*
import java.util.*
import java.io.*
def sTestdatafile ="inputdata.csv"
//get the path of testdata file
def controlfilepath=projectdir+"\\TestData\\+sTestdatafile
//log.info(controlfilepath)
// projectdir = how to get the project dir, pls refer the question no 7
//Read file
File file = new File(controlfilepath)
BufferedReader reader = new BufferedReader(new FileReader(file))
String line=null
row = 1
while ((line = reader.readLine()) != null)
{
//Avoid first line, as those are columns
if (row >1)
{
// log.info "print number value in if condition:"+number
String[] dataval = line.split(",")
// Assign values to the property
testRunner.testCase.setPropertyValue("sEnv",dataval[1])
testRunner.testCase.setPropertyValue("sTestcasename",dataval[2])
testRunner.testCase.setPropertyValue("sCustomerNo",dataval[3])
testRunner.testCase.setPropertyValue("sCustomerName",dataval[3])
testRunner.testCase.setPropertyValue("sExpected",dataval[3])
//### Calling the submit request - Pass the data for each iteration to the submit request ##########
result = testRunner.runTestStep(testRunner.getTestCase().getTestStepByName("Submit Request"))
sleep 2000
}
row = row+1
}
13. How to get the custom properies?
Custom properties from project level:
def endpoint = context.expand('${#Project#custome property name}')
log.info endpoint
or
def test =testRunner.testCase.testSuite.project.getPropertyValue( "custom property name")
log.info test
Test suite level
def testsuite = context.expand( '${#TestSuite#custom property}' )
log.info testsuite
or
- def sTestSuite = testRunner.testCase.testSuite.getPropertyValue( "custom property name")
- log.info sTestSuite
Test case level
def testcase = context.expand('${#TestCase#custom property}' )
log.info testcase
No comments:
Post a Comment