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())
    }

Go Groovy with soapUI - nifty tool for Webservice testing!

Here i go with first groovy post about using Groovy script in soapUI. groovy comes very handy when you want to put some script assertion or performing certain task before/after each teststep execution etc.

First couple of things to remember before we proceed with actual code lines.

# Groovy script can be written in Assertaion, Test Step, TestCase/TestSuite level as TearDown/Setup script.
# To invoke the groovy script at each different level, script editor provides us with the list of variables on top right corner of script editor. So it is possible that the code you have written in groovy test step or as teardown script would not work properly for teststep script assertion.


In most of the cases we use either "context" or "testrunner" variable to invoke or create a new object of any class or to call required method.

I frequently use these 2 standard lines of groovy code, to acheive the required script objective, are :

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

def holder = groovyUtils.getXmlHolder (java.lang.String xmlPropertyOrString) // where this xmlProperty can have either 'teststepname#response' 'teststepname#request' or any string value.


After having the object created refer the groovyUtils APIDocs (http://www.soapui.org/apidocs/com/eviware/soapui/support/GroovyUtils.html) to find out all the relevant methods which can be called with particular input parameter.

Now below are few code lines for quickly performing certain task :


/* 
@Author : Pradeep Bishnoi
@Description : To count the number of nodes under any node in response using xPath & groovy script.
*/
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder ("myTestStepName#Response")
holder.namespaces["namespacename1"] = "http://mywebservice.com/soapui/testing"
log.info("Number of nodes :" + holder.getNodeValue("count(//namespacename1:GetSampleMethodResponse/namespacename1:StudentDetails/namespacename1:Address)"))

// Address is comprised of house number, street name, city, country - so result = 4

/*
@Description : To store the response into a simple text file, append this code to the above.
*/

new File('D:/response_myTestStepName.txt').write(holder.prettyXml)

Do provide your inputs/comments/question on the same blog and share if you have something interesting. Thanks!

Tuesday, May 17, 2011

TestRunner - soapUI project execution from command line

Folks, in this post i will be covering the use of test runner - by which you can execute your soapUI projects (from command line) without opening soapUI application.

This feature will by very useful when you want to perform the regression test on the newly deployed code & to store the response data in some files. And execution can be done by anyone (including people who don't know "how to use soapUI").

testrunner.bat (soapui_install_dir/bin folder) is a command line utility which is getting called whenever we execute our testcases with some predefined (or userdefined) conditions. In soapUI application, right click on any selected testcase/test suite and then select "launch test runner" from the context menu to call the test runner.bat file. This will open a new window wherein you can provide the required configuration data like, testcase/testsuite to be executed, endpoint url, folder to store the response, ignore the error results and so on.
 After providing all the details, start the testrunner.bat by clicking launch button. In the window it will display the command line arguments being passed for the selected combination of input data.

Similarly, you can directly call the testrunner.bat from the command line utitlity and provide those parameters to perform the test execution. This will save lot of time to perform the test execution and also it can be executed by anyone (in your absence).
I have created a simple batch file (my_soapUI.bat) to perform this set of activity. So to perform the regression test, i need to just execute the "my_soapUI.bat" file from the command line and rest would be taken care.

cd C:\Program Files\eviware\soapUI-Pro-3.6\bin
testrunner.bat -ehttp://127.0.0.1/soapUI/Service.svc -sLoad_Test_Apr2011_TestSuite -r -a -fD:\Automate\store_response -I "C:\Documents and Settings\pradeep.bishnoi\My Documents\my_soapui-project.xml"

:: To override the endpoint for the teststeps use
:: -e(Endpoint URL)                
:: -ehttp://x.x.x.x/WebService.svc


:: TestSuite to run, used to narrow down the tests to run
:: -s(TestSuite Name under the project)                
:: -sTestSuiteSoapUI

:: TestCase to run, used to narrow down the tests to run
:: -c(TestCase Name under the project/testSuite)                
:: -cTestCaseSoapUI

:: r : Turns on printing of a small summary report. To be used when you want to save generated reports
:: -r                

:: f : Specifies the root folder to which test results should be exported
:: -f(Complete path of the folder where to save the response)
:: -fD:\soapUI\responseReport

:: a : Turns on exporting of all test results, not only errors. To be used when you want to export both passed and failed test step reports
:: -a                

:: I : Do not stop if error occurs, ignore them. Execution will continue even when any test step fails/error comes.
:: -I                

:: After providing all the parameters, pass the complete soapUI project file location in double quotes "" which contains the above mentioned testSuite & testCases for execution.
:: example      "C:\soapui-project.xml"
To use the same, just copy the code lines (in yellow colored font) and save them into a "my_soapUI.bat" file and edit the required parameters like endpoint, project file name, and folder to store the response etc.

EndNote : Biggest advantage of using the command line approach is #Faster execution since we are directly executing the project without opening the soapUI.