Thursday, May 19, 2011

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.