Sunday, June 16, 2013

SOAP UI Groovy Scripts

------------------------------------------------------------------------
//Read input values from window prompt and setting read at testsuite level
------------------------------------------------------------------------
import com.eviware.soapui.support.*
def alert = com.eviware.soapui.support.UISupport
def userName = alert.prompt("Username","Webservices-UserName")
def password = alert.prompt("Password","Webservices-Password")
testSuite.setPropertyValue( "userName", userName)
testSuite.setPropertyValue( "password", password)
---------------------------------------------------------------------------------
------------------------------------------------------------------------
//Read input values from window prompt and setting read at project level
------------------------------------------------------------------------
import com.eviware.soapui.support.*
def alert = com.eviware.soapui.support.UISupport
def userName = alert.prompt("Username","Webservices-UserName")
def password = alert.promptPassword("Password","Webservices-Password").toString()
project.setPropertyValue( "userName", userName)
project.setPropertyValue( "password", password)
-------------------------------------------------------------------------------------
//SOAP UI Groovy script to create and write data into excel
------------------------------------------------------------------------
import jxl.*
import jxl.write.*
WritableWorkbook WritableExcel = Workbook.createWorkbook(new File("D:\\yt.xls"))
WritableSheet sheet = WritableExcel.createSheet("H",0)
log.info(sheet)
Label label = new Label(0, 2, "Text input in Excel");
sheet.addCell(label);
WritableExcel.write()
WritableExcel.close()
--------------------------------------------------------------------------------------
//SOAP UI GROOVY SCRIPT TO WRITE A FOLDER AND THEN WRITING TXT OR EXCEL FILE INTO IT
------------------------------------------------------------------------

createFolder = new File("D:/SOAPUI/sam2922")  
createFolder.mkdir()
file = new File("D:/SOAPUI/sam2922.xls")
file.createNewFile()
Workbook workbook = Workbook.getWorkbook(new File("D:/SOAPUI/sam2922/ash9.xls"))
------------------------------------------------------------------------
//SOAP UI GROOVY SCRIPT FOR SETTING A JDBC CONNECTION AND THEN RETRIEVING VALUE FROM TABLE AND THEN PASSING IT TO NEXT STEP
------------------------------------------------------------------------
import groovy.sql.Sql
groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
sql = Sql.newInstance("jdbc:oracle:thin:int_db/Oracle11g@XX.YYY.ZZZ.QQ:1111:DDDD", "oracle.jdbc.driver.OracleDriver")
row = sql.firstRow("SELECT * FROM ORDERS WHERE ORDER_NUM= 'wegfdsfdsfGSFT'")
FIRSTNAME= row.FIRSTNAME
context["FIRSTNAME"] = row.FIRSTNAME
def string1 = FIRSTNAME;
context.setProperty("FIRSTNAME",string1)
log.info context.getProperty("FIRSTNAME")
---------------------------------------------------------------------------------
//SOAP UI GROOVY SCRIPT FOR ASSERTING WHETHER A PARTICULAR NODE VALUE IS STRING OR NOT
------------------------------------------------------------------------
groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
holder = groovyUtils.getXmlHolder("GetManuscriptFileListView - Request 1-Response")
out = holder.getNodeValue("//FileId")
assert out.class == String
----------------------------------------------------
//SOAP UI GROOVY SCRIPT TO GET THE NAME OF A PROJECT
a = testRunner.testCase.testSuite.project.name
---------------------------------------------------------------------------------
//SOAP UI GROOVY SCRIPT TO GET THE RESPONSE FROM A TEST STEP
---------------------------------------------------------------------------------
b = testRunner.testCase.getTestStepByName("GetManuscriptFileListView - Request 1").getProperty("Response").getValue()
---------------------------------------------------------------------------------
//SOAP UI GROOVY SCRIPT TO GET THE RESPONSE OF A TEST STEP    
---------------------------------------------------------------------------------
a = testRunner.testCase.getTestStepByName("GetManuscriptFileListView - Request 1")
responseData = a.getProperty( "Response" )
log.info(responseData.value)
---------------------------------------------------------------------------------
//SOAP UI  GROOVY SCRIPT TO WRITE A RESPONSE TO TEXT FILE
---------------------------------------------------------------------------------
createFolder = new File("D:/SOAPUI/SAM222")  
createFolder.mkdir()
file = new File("D:/SOAPUI/SAM222/ash16.txt")
file.createNewFile()
a = testRunner.testCase.getTestStepByName("GetManuscriptFileListView - Request 1")
responsedata = a.getProperty( "Response" )
file.write(responsedata.value)
---------------------------------------------------------------------------------
//SOAP UI GROOVY SCRIPT TO GET HOLD OF TESTSUITE, TEST CASE, TEST STEP
---------------------------------------------------------------------------------
testSuite = testRunner.testCase.testSuite.project.testSuites["ManuscriptServiceTestSuite"]
testCase = testSuite.getTestCaseAt(0)
testStep1 = testCase.getTestStepCount()
---------------------------------------------------------------------------------
//SOAP UI GROOVY TEST SCRIPT TO WRITE TODAY'S DATE TO A TEXT FILE AND ALSO TO PROPERTY STEP
---------------------------------------------------------------------------------
log.info("In the WeatherTestSuite Setup Script")
sdf = new java.text.SimpleDateFormat("yyyy-MM-dd")
todayStr = sdf.format(new Date())
props = new java.util.Properties ()
file = new File("D:/SOAPUI/SAM222.txt")
if(!file.exists())
{
file.createNewFile()
fis = new FileInputStream (file )
props.setProperty ( "today" , todayStr)
props.setProperty ( "zipCode" , "20904")
fos = new java.io.FileOutputStream ( file )
props.store(fos, "Writing the zipcode and today's date")
}
---------------------------------------------------------------------------------
//SOAP UI GROOVY SCRIPT FOR WRITING VALUES TO PROPERTY STEP
---------------------------------------------------------------------------------
a = testRunner.testCase.getTestStepByName("Properties")
a.setPropertyValue("SystemId", "80")
a.setPropertyValue("Section", "90")
---------------------------------------------------------------------------------
//SOAP UI GROOVY SCRIPT FOR ASSERTING WHETHER A PARTICULAR NODE VALUE IS STRING OR NOT
---------------------------------------------------------------------------------
groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
holder = groovyUtils.getXmlHolder("GetManuscriptFileListView - Request 1-Response")
out = holder.getNodeValue("//FileId")
assert out.class == String
---------------------------------------------------------------------------------
//SOAP UI  Groovy Script: Generate random string from given character set
---------------------------------------------------------------------------------
def generator = { String alphabet, int n ->
  new Random().with {
    (1..n).collect { alphabet[ nextInt( alphabet.length() ) ] }.join()
  }
}
generator( (('A'..'Z')+('0'..'9')).join(), 9 )
---------------------------------------------------------------------------------i
//SOAP UI  Groovy Script: Generate random numbers
---------------------------------------------------------------------------------
nt a = 9
nr = "CONT"
for(i = 0; i < 5; i++)
{
random = new Random()
randomInteger= random.nextInt(a)
nr = nr + randomInteger
}
log.info nr
---------------------------------------------------------------------------------i
//SOAP UI  Groovy Script: Generate random number and set value at test case level
---------------------------------------------------------------------------------
a = testRunner.testCase.getTestStepByName("Properties")
nr = "CONT"
b = nr+String.valueOf((int)Math.random()*100000)
a.setPropertyValue("TransID", b);
---------------------------------------------------------------------------------
//SOAP UI GROOVY SCRIPT TO CREATE A EXCEL WORK BOOK TIME STAMP
---------------------------------------------------------------------------------
def  timestamp = new Date().format("yyyyMMddhhmm")
log.info (timestamp)
file name    ("d:\\output-"+timestamp+".xls"))
---------------------------------------------------------------------------------
//SOAP UI GROOVY SCRIPT LOOP TEST CASE FOR MULTIPLE TIMES
for( c in 0..5 )
testRunner.runTestStepByName( “My Request” )
---------------------------------------------------------------------------------

2 comments:

  1. Hi,

    How to add multiple values to one Property. Is there any way to add the values using seperator.
    Ex: Sector = reits, healthcare, banks

    Please respond ASAP.

    Thank you.

    ReplyDelete
  2. Hi,

    READY API GET CALL

    I have taken a data source .csv file in order to fetch all the rows. I am using that in a REST request. The request is going to the response in the format of
    /XDImgService/rest/wado?requestType=WADO&studyUID=2.16.840.1.113669.632.20.1211.10000098591&seriesUID=1.2.840.113704.1.111.5692.1127828999.2&objectUID=1.2.840.113704.7.1.1.6632.1127829031.2&contentType=application/dicom.

    It's a GET REQUEST with 5 parameters.

    how to write a groovy script in order to hold that request in requestholder.

    ReplyDelete