Friday, July 15, 2011

10 Groovy scripts on your finger tips - soapUI

In this series of Groovy blogs [numbered 10], i will be sharing very frequently  used "10 groovy scripts" which should be on your finger tips. These would come handy in order to perform any Automation using the Groovy in soapUI.

/* 
@Author : Pradeep Bishnoi
@Description : Collection of groovy script snippets required to achieve automation in soapUI
*/

1. Using Log variable
    log.info("Any Text message " + anyVariable)

2. Using Context variable
    def myVar = context.expand( '${#TestCase#SourceTestStep}') //will expand TestCase property value into the new variable
    context.testCase  // returns the current testCase handle

3. Using TestRunner variable
    testRunner.testCase.getTestStepByName("TestStepName")
    testRunner.testCase // return the handle to current testCase
    testRunner.testCase.testSuite.project.testSuites["My_TestSuite"]

4. Using MessageExchange variable
    messageExchange.getEndpoint() //endpoint to the selected teststep
    messageExchange.getTimestamp()    //timestamp
    messageExchange.getTimeTaken()    //time taken to process the request/response

5. Using Project, TestSuite, TestCase, TestStep methods
    def project = testRunner.testCase.testSuite.project
    def project = context.testCase.testSuite.project

    def myTestSuite = project.getTestSuiteAt(IndexNumber)
    def myTestSuite = project.getTestSuiteByName("Name of the TestSuite")

    def myTestCase = myTestSuite.getTestCaseAt(IndexNumber)
    def myTestCase = myTestSuite.getTestCaseByName("Name of the TestCase")

    def myTestStep = myTestCase.getTestStepAt(IndexNumber)
    def myTestStep = myTestCase.getTestStepByName("Name of the TestStep")

6. RawRequest & RawResponse
    messageExchange.getRequestContentAsXml.toString()
    messageExchange.getResponseContentAsXml.toString()

7. groovyUtils & XmlHolder
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def holder = groovyUtils.getXmlHolder ("Assert_Script#Response")

8. Converting String into Integer & Integer into String using groovy
    anyStringVar = anyIntegerVar.toString()
    anyIntegerVar = anyStringVar.toInteger()

9. Reading & Writing user-defined property with Groovy
    def userIdInStep = testRunner.testCase.getTestStepByName( "UserDefinedProperty" )
    def userIdStr = userIdInStep.getPropertyValue( "myPropertyName" );
    userIdInStep.setPropertyValue("myPropertyName", "StringValueAsInput")

10. Refer soapUI API docs & go soapUI Pro inorder to get Advanced functionality without writing code ;-)

Happy Scripting!! Do share this blog post with others and also share any useful scripts/ideas if you have.

Groovy 9 - capturing RawRequest & Response

Few months back i was struggling to capture the Raw Request data from the test step and to store it (together with response) into a file. Now you might be wondering why to struggle so much if the test step request data can be simply accessed using :
testCase.getTestStepByName("myTestStep").getProperty("Request").getValue()
or 
context.testCase.getTestStepAt(0).getProperty("Request").getValue() 
or
testRunner.testCase.getTestStepAt(0).getProperty("Request").getValue()

Well above script snippets will fetch the request data from the mentioned/selected test step. However, if your test is parametrized then this (request data) will also appear as in same way as it appears in request window [i.e., not with actual parameter values].
Since, it is a parametrized request the original values of parameters can be found in the Raw tab of the teststep request window. This concept can be compared to "PreProcessors in C" or "Inline function of C++" where the actual values of parameter will be replaced before compiling the program. Here also, before sending out the request data (or say encapsulating it in SOAP envelope) to the server the parameter values will be replaced with each parameter calls in the test request.


If raw request data not used, then while performing the comparison of the different request & response data after final run, it would be difficult for us to trace what parameter value was passed during teststep run.
To overcome such issues, soapUI team also exposed couple of methods which can be used to extract the RawRequest / RawResponse data and so on. Shared below is one of the ways to get the raw request data :

messageExchange.getRequestContent().toString()


Monday, July 04, 2011

Groovy 8 – creating and manipulating Excel file using Groovy script in soapUI

Many of soapUI users wonder, if they can create and manipulate the data inside an Excel file using the soapUI's groovy scripting. Well answer is YES and through this blog i am putting an end to their concerns / questions.

As i might have mentioned in my previous blogs that Groovy Script together with soapUI provides utilitarian, seamless & efficient functionalities with infinite possibilities. So, i would say "your thinking is the limit, rest almost everything can be achieved using Groovy in soapUI".

From eviware forum, i found that soapUI is using the free Java Excel API to communicate or manipulate data in the Excel files. So, all this can be achieved using the JXL.jar file. All you need do is place the JXL api file in the “lib” folder of %SOAPUI_INSTALL_DIR%. This file can be downloaded @ http://sourceforge.net/projects/jexcelapi/files/jexcelapi/2.6.12/jexcelapi_2_6_12.zip/download

/*
@Author : Pradeep Bishnoi
@Description : Manipulate the data inside an Excel file using jxl api in groovy
@Ref : Thanks to Andy Khan for sharing the reference documents about JXL API.
*/

import jxl.*
import jxl.write.*
Workbook workbook = Workbook.getWorkbook(new File("c:\\myfile.xls"))
Sheet sheet = workbook.getSheet(0)
Cell a1 = sheet.getCell(0,0) // getCell(row,column) -- place some values in myfile.xls
Cell b2 = sheet.getCell(1,1)  // then those values will be acessed using a1, b2 & c3 Cell.
Cell c2 = sheet.getCell(2,1)
workbook.write()
workbook.close()

WritableWorkbook workbook1 = Workbook.createWorkbook(new File("d:\\output.xls"))
WritableSheet sheet1 = workbook1.createSheet("Worksheet Number 1", 0)
log.info(sheet1.isHidden())
Label label = new Label(0, 2, "Text input in Excel");
sheet1.addCell(label);
workbook1.write()
workbook1.close()

Keep watching this space... soon i will be posting about "how to export the test result & certain other things using the Groovy into the Excel file".