Showing posts with label groovy. Show all posts
Showing posts with label groovy. Show all posts

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.

Thursday, June 02, 2011

Groovy 5 - adding basic assertion using Groovy in soapUI

From the time i started using soapUI(both open source & Pro), I was struggling with one very simple though a painful issue. Adding basic assertion (like SOAP response, Not SOAP Fault) in each and every test step (where number of teststep is around 10+) of your TestCase/TestSuite. There may/are few alternatives available which can be used to acheive this, like create few basic assertion in one teststep and then clonse these assertion into other teststeps.

The above mentioned apporach can be used however that also require many click here and there. So i have written few lines of code to automate that part & these code lines can be customized and extended as per your testing needs.

To proceed with just create a new Groovy Test Step and execute that test step once. Please note: i am assuming you already have all the teststep created under a testcase.

/*   
@Title : Gr-ooooo-vy 5
@Author : Pradeep Bishnoi
@Description : Add 2 basic assertion (soap & not soap fault) in all teststeps using the Groovy script
*/

import com.eviware.soapui.model.testsuite.Assertable
def project = context.testCase.testSuite.project
def testSuite = project.getTestSuiteAt(1)
def testCase = testSuite.getTestCaseAt(0)

wsdlTestSteps = testCase.getTestStepsOfType( com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep.class ) //test step

def testStepCount = wsdlTestSteps.size()
( 0..< testStepCount).each
{
        // SOAP Response      Not SOAP Fault
        if (!(wsdlTestSteps[it].getAssertionByName("Not SOAP Fault")))
        {
            wsdlTestSteps[it].addAssertion("Not SOAP Fault")
        }
        if (!(wsdlTestSteps[it].getAssertionByName("SOAP Response")))
        {
            wsdlTestSteps[it].addAssertion("SOAP Response")
        }
       
 log.info("Assertion created into Teststep :: " + testCase.getTestStepByName(wsdlTestSteps[it].getName()).getName().toString())
}

Do share the same with others & provide your comments on the same, anything.
// Slight formatting/editing may be required in the code lines when pasting it into soapUI script editor.

Tuesday, May 24, 2011

Groovy IV - how to log response size, request size, time taken


/*
@Title : Gr-oooo-vy Code IV
@Description : to log the response size, request size and time taken to get the response data.
@Author : Pradeep Bishnoi
*/

Last few blogs were related to Groovy script, however i didn't mentioned about one of most useful groovy code lines which can be very handy when you want to verify the certain things without actually looking into the response data.

The cases where you want to time taken by server to provide the response, or the validating the response data by verifying/comparing  the size of response data or request data. I personally use these script lines to frequently see all the logged data when executing the testcases/testsuite for validating the correctness of my data. For instance, if you recv a valid response for a request size would be xx Bytes and whenever there is any error it would be yy Bytes. Similarly, if the timetaken by response is exceeding your assigned/recommended values, you can use this to verify (note: you can also use 'assert' keyword to use any specific value as the assertion).

Use the below provided code lines in the groovy script assertion of each test step in a testcase.

log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
log.info("Request size :" + messageExchange.requestContent.size())
log.info("Response size :" + messageExchange.responseContent.size())
log.info("Time :" + messageExchange.getTimeTaken())

With every run of the teststep, script log section/tab will update the display with the size of request, response & timetaken to provide the response.

Monday, May 23, 2011

Groovy III - text file as Data Source for input

/*
@Title : Gr-ooo-vy Code III
@Description : using text file as Data Source using Groovy code lines
@Author : Pradeep Bishnoi
*/

This is another blog post unleashing the power of Groovy script to perform certain set of task. SoapUI users, who have used the 15 days trail license of SoapUI Pro version, must be missing various important features, after license expiration. Well, in this blog i will try to replicate one of the important feature of Data Source [provided by Pro version] using the Groovy. A lot can be achieved using the Groovy...

Recommendation : I would encourage users to go for soapUI Pro license to avail many more useful features + the support from soapUI support team. Go soapUI Pro!!

Copy and paste the below provided code lines in the TearDown script for the selected TestCase.


import com.eviware.soapui.support.XmlHolder

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def folderName = "D:/Automate/response"  //to store the resonse
def createFolder = new File(folderName)
createFolder.mkdir()

def testSuite = testRunner.testCase.testSuite.project.testSuites["My_TestSuite"]
def testCase = testSuite.getTestCaseAt(0)
def testStep1 = testCase.getTestStepCount()

def xHolder = new XmlHolder(testCase.getTestStepAt(0).getProperty("Response").getValue())

File tempFile = new File("D:/data_source_file.txt")
List lines = tempFile.readLines()
def i=0
def temp1 = 0
def inputValue

def myTestStep
def myTestRunner
def myTestStepContext
//Loop through all the names of the test steps.
( 0..<testStep1 ).each
{
    i=0

   tempFile.eachLine
    {
        testCase.setPropertyValue("inputValue", lines[i++])
        inputValue = context.expand( '${#TestCase#inputValue}' )
        myTestStep = testCase.getTestStepAt(temp1)
        myTestRunner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(myTestCase, null)
        myTestStepContext = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext( myTestStep )
        myTestStep.run( myTestRunner, myTestStepContext )
       

xHolder = new XmlHolder(testCase.getTestStepAt(temp1).getProperty("Response").getValue())
        f = new File( folderName + '/' + inputValue + '_' +  testCase.getTestStepAt(temp1).getName().toString() + '.txt')
        f.append("\r\n\r\n\r\n\r\n\r\n")
        f.append("#################################################\r\n")
        f.append("\t\t\t Response \r\n")
        f.append("#################################################\r\n\r\n\r\n\r\n\r\n")
        f.write(xHolder.prettyXml)
        xHolder.clear()
   
    }
    temp1++
}


Now all the response will be stored in the filename with specific name [inputvalue_teststepname.txt] in the folder name defined in the above part of the code.Also, you have to create a user defined property under the TestCase (test properties) and name it as "inputValue" [Case Sensitive]. And use this user defined property in all the teststep as the parameterized input. 
Updated : Code lines mentioned above are updated after recieving the feedback from the users. There was some problem with HTML tags, so the actual code was not visible. Now it should work.

Thursday, May 19, 2011

Gr-oo-vy II – testrunner/context variable

Use of the testrunner variable to get the response data of any specific teststep.
def request = testRunner.testCase.getTestStepByName( “myTestStepName” );
def responseData = request.getProperty( “Response” );
log.info(responseData.value)
To display the name of project, testsuite, testcase & teststep:
def project = context.testCase.testSuite.project
log.info(project.name + “   ” + project)
def testSuite = project.getTestSuiteAt(1)  // 1 is the index number of testsuite
log.info(testSuite.name + “   ” + testSuite)
def testCase = testSuite.getTestCaseAt(0)  // 0 is the index number of testcase
log.info(testCase.name + “      ” + testCase)
def testStep = testCase.getTestStepAt(7) // 7 is the index number of teststep
log.info(testStep.name + “    ” + testStep)
To count the number of testsuites, testcases, teststeps use the below code :
def project = context.testCase.testSuite.project
log.info(project.name + “   ” + project.testSuiteCount)
def testSuite = project.getTestSuiteAt(1)
log.info(testSuite.name + “   ” + testSuite.testCaseCount)
def testCase = testSuite.getTestCaseAt(0)
log.info(testCase.name + “      ” + testCase.testStepCount)
def testStep = testCase.getTestStepAt(7)
log.info(testStep.name + “    ” + testStep)
We can use the above code and put them into a loop (for/each) to iterate through all the elements. In below example, we iterate through all the test steps under a testcase :
for (int count in 0..<testcase.teststepcount)
{
log.info(testCase.getTestStepAt(count).getName() + “  is our testStep number ” + count)
log.info(“Is this test step disabled? : ” + testCase.getTestStepAt(count).isDisabled())
}
OR

( 0..<teststep1 ).each{
    log.info(testCase.getTestStepAt(it).getName())
    }