Showing posts with label SOAP UI. Show all posts
Showing posts with label SOAP UI. Show all posts

Sunday, July 14, 2013

Update SoapUI Request with groovy or Property Transfer using groovy in SOAP UI

//Update SoapUI Request with groovy or Property Transfer using groovy in SOAP UI
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

// get XmlHolder for request message def

ReqHolder = groovyUtils.getXmlHolder( "CelsiusToFahrenheit#Request" )

ReqHolder1 = groovyUtils.getXmlHolder( "FahrenheitToCelsius#Request" )

// Pass value to request node
ReqHolder["//tem:Celsius"] = "100"

// write updated request back to teststep
ReqHolder.updateProperty()

// Run the Request
testRunner.runTestStepByName("CelsiusToFahrenheit")

// Get the response value in a variable
def response = context.expand( '${CelsiusToFahrenheit#Response#declare namespace ns1=\'http://tempuri.org/\'; //ns1:CelsiusToFahrenheitResponse[1]/ns1:CelsiusToFahrenheitResult[1]}' )
log.info(response)


// Pass the new value to another request
ReqHolder1["//tem:Fahrenheit"] = response
ReqHolder1.updateProperty()

// run the test request
testRunner.runTestStepByName("FahrenheitToCelsius")

def response1 = context.expand( '${FahrenheitToCelsius#Response#declare namespace ns1=\'http://tempuri.org/\'; //ns1:FahrenheitToCelsiusResponse[1]/ns1:FahrenheitToCelsiusResult[1]}' )
log.info(response1)

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” )
---------------------------------------------------------------------------------

SOAP UI : SetUp HTTP Basic Authentication for soapUI Test Suties


SetUp HTTP Basic Authentication for soapUI Test Suties

import com.eviware.soapui.impl.wsdl.teststeps.*
for( testCase in testSuite.getTestCaseList() )
{
log.info("Setting HTTP basic auth for all WSDL test requests in test case ["+ testCase.getLabel()+"]")
for( testStep in testCase.getTestStepList() )
{
if( testStep instanceof WsdlTestRequestStep )
{
//Create UNAME variable in tesSuite testProperties and assign Value to it.
testStep.getTestRequest() .setUsername(testSuite .getPropertyValue("UNAME"))
//Create PASSWORD variable in tesSuite testProperties and assign Value to it.
testStep.getTestRequest() .setPassword(testSuite .getPropertyValue("PASSWORD"))
}
}
}

Tuesday, May 21, 2013

SOAP UI Assertions : Adding XPath assertion with Groovy Script from Project level

SOAP UI Assertions : Adding XPath assertion with Groovy Script from Project level
def TSName = ["Testuite1","Testsuite2"]
for(j=0;j<TSName.size();j++)
{
def StepName = ["testStep1","testStep1"]
for(i=0;i<StepName.size();i++)
{
    project.getTestSuiteList().each
{
    if(it.name == TSName[j])
    {
        TS = it.name
        log.info "TS="+TS
        if(project.getTestSuiteByName(TS).getTestCaseByName(TC).getTestStepByName(StepName[i]))
        {
          it.getTestCaseList().each
          {
            TC =it.name
            def asserting = project.getTestSuiteByName(TS).getTestCaseByName(TC).getTestStepByName(StepName[i]).getAssertionByName("XPath Match")
            if (asserting instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XPathContainsAssertion)
            {
                project.getTestSuiteByName(TS).getTestCaseByName(TC)getTestStepByName(StepName[i]).removeAssertion(asserting)
            }
                def assertion = project.getTestSuiteByName(TS).getTestCaseByName(TC)getTestStepByName(StepName[i]).addAssertion("XPath Match")
                if(StepName[i] == "testStep1")
            {
                assertion.path = "declare namespace ns1='http://www.xyz.com/xyz/abc/v1/xyz';\n//ns1:parentnode[1]/ns1:status[1]/ns1:value[1]"
                assertion.expectedContent = "true" 
            }
            else
            {
                assertion.path = "declare namespace ns1='www.xyz.com/xyz/abc/v1/xyz';\n//ns1:parentnode[1]/ns1:status[1]/ns1:value[1]"
                assertion.expectedContent = "true"
            }
          }
    }
    }
}
}
}
log.info "j = "+j

Monday, May 20, 2013

SOAP UI : Adding XPath Assertion to all test steps(same request) in project.

Adding XPath Assertion to all test steps(same request) in project.

TSName = "TestSuiteName"
StepName = "TestStepName"
project.getTestSuiteList().each
{
    if(it.name == TSName)
    {
        TS = it.name
          it.getTestCaseList().each
          {
            TC =it.name
            def asserting = project.getTestSuiteByName(TS).getTestCaseByName(TC).getTestStepByName(StepName).getAssertionByName("XPath Match")
            if (asserting instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XPathContainsAssertion)
            {
                project.getTestSuiteByName(TS).getTestCaseByName(TC)getTestStepByName(StepName).removeAssertion(asserting)
            }
                def assertion = project.getTestSuiteByName(TS).getTestCaseByName(TC)getTestStepByName(StepName).addAssertion("XPath Match")
                if(StepName == "TestStepName")
            {
                assertion.path = "declare namespace ns1='http://www.xyz.com/';\n//ns1:StepResponse[1]/ns1:status[1]/ns1:value[1]"
                assertion.expectedContent = "success"
            }
            else
            {
                assertion.path = "declare namespace ns1='http://www.xyz.com/';\n//ns1:StepRequestResponse[1]/ns1:status[1]/ns1:value[1]""
                assertion.expectedContent = "200"
            }
          }
    }
}

SOAP UI : Adding XPath Assertion from test suite level, test case level and from test step(groovy)

Adding XPath Assertion to soap request test steps from Project level(Copy below script in load script of project)

def asserting = project.getTestSuiteByName("StockQuoteService").getTestCaseByName("GetStockQuote").getTestStepByName("GetStockQuote").getAssertionByName("XPath Match")
if (asserting instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XPathContainsAssertion)
{
project.getTestSuiteByName("StockQuoteService").getTestCaseByName("GetStockQuote")getTestStepByName("GetStockQuote").removeAssertion(asserting)
}

def assertion = project.getTestSuiteByName("StockQuoteService").getTestCaseByName("GetStockQuote")getTestStepByName("GetStockQuote").addAssertion("XPath Match")
assertion.path = "declare namespace ns1='http://www.restfulwebservices.net/ServiceContracts/2008/01';declare namespace a='http://www.restfulwebservices.net/DataContracts/2008/01';\n //ns1:GetStockQuoteResponse[1]/ns1:GetStockQuoteResult[1]/a:Symbol[1]"
assertion.expectedContent = "CTSH"

Adding XPath Assertion to soap request test steps from Test Suite level(Copy below script in setup script of test suite)

def asserting = testSuite.getTestCaseByName("GetStockQuote").getTestStepByName("GetStockQuote").getAssertionByName("XPath Match")
if (asserting instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XPathContainsAssertion)
{
testSuite.getTestCaseByName("GetStockQuote")getTestStepByName("GetStockQuote").removeAssertion(asserting)
}

def assertion = testSuite.getTestCaseByName("GetStockQuote")getTestStepByName("GetStockQuote").addAssertion("XPath Match")
assertion.path = "declare namespace ns1='http://www.restfulwebservices.net/ServiceContracts/2008/01';declare namespace a='http://www.restfulwebservices.net/DataContracts/2008/01';\n //ns1:GetStockQuoteResponse[1]/ns1:GetStockQuoteResult[1]/a:Symbol[1]"
assertion.expectedContent = "CTSH"

Adding XPath Assertion to soap request test steps from Test Case level(Copy below script in setup script of test case)

def asserting = testCase.getTestStepByName("GetStockQuote").getAssertionByName("XPath Match")
if (asserting instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XPathContainsAssertion)
{
testCase.getTestStepByName("GetStockQuote").removeAssertion(asserting)
}

def assertion = testCase.getTestStepByName("GetStockQuote").addAssertion("XPath Match")
assertion.path = "declare namespace ns1='http://www.restfulwebservices.net/ServiceContracts/2008/01';declare namespace a='http://www.restfulwebservices.net/DataContracts/2008/01';\n //ns1:GetStockQuoteResponse[1]/ns1:GetStockQuoteResult[1]/a:Symbol[1]"
assertion.expectedContent = "CTSH"

Adding XPath Assertion to soap request test steps using groovy step/script(Copy below script in groovy script test step) 
def testst = testRunner.testCase.getTestStepByName("GetStockQuote")
def asserting = testst.getAssertionByName("XPath Match")
if (asserting instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XPathContainsAssertion)
{
testst.removeAssertion(asserting)
}

def assertion = testst.addAssertion("XPath Match")
assertion.path = "declare namespace ns1='http://www.restfulwebservices.net/ServiceContracts/2008/01';declare namespace a='http://www.restfulwebservices.net/DataContracts/2008/01';\n //ns1:GetStockQuoteResponse[1]/ns1:GetStockQuoteResult[1]/a:Symbol[1]"
assertion.expectedContent = "CTSH" 



Friday, May 25, 2012

Groovy : Read & Write values from csv file


//groovy script to store values in properties 

def testDataSet = []
def failureList = []
def fileName = "d:\\SOAPUI\\planets.CSV"

// Opens file, iterates through each line, splits the line on the commas into an array, and adds
// the array to testDataSet. (So testDataSet is an array of arrays).
new File(fileName).eachLine { line -> testDataSet.add( line.split(",") ) }

log.info( "Read " + testDataSet.size() + " test values from " + fileName )

// If any iteration fails, it will add an entry to the failure list, and they will all be printed at the end
context.setProperty( "failureList", failureList )

// Store the test data in a property called "testData"
context.setProperty( "testDataSet", testDataSet )


// This indicates which line of the file we will start reading from (0 = first line, 1 = second line, etc.)
// We are starting at the second line because the CSV contains a header row at the top that explains what
//   each column means
context.setProperty( "index", 1 )

//groovy script to read values from properties

def testDataSet = context.getProperty("testDataSet") // Retrieve the test data from the testData property
def index = context.getProperty("index") // Track which line of the test input file we're on
def testDataLine = testDataSet[index] // Select the current test data line

def props = testRunner.testCase.getTestStepByName( "Properties" )
props.setPropertyValue("AccountName", testDataLine[0])
props.setPropertyValue("AccountCode", testDataLine[1])
props.setPropertyValue("mars", testDataLine[2])

Practice Groovy

Groovy Examples

Thursday, May 24, 2012

Web Service Testing with SOAPUI

Groovy Script : Text File Create & Write.


def fileName = "test1.txt"
// Defining a file handler/pointer to handle the file.
def inputFile = new File("d:\\"+fileName)
// Check if a file with same name exisits in the folder.
if(inputFile.exists())
{
// if a file exisits then it will print the message to the log.
log.info("A file named " + fileName + " already exisits in the same folder")
}
else
{
//else it will create a file and write the text "Hello World !"

inputFile.write("Hello World !")
log.info "File got created"

}