Tuesday, February 12, 2013

A groovy console for SoapUI

The never-ending quest for somewhat useful SoapUI plugins has come to a Groovy Console plugin - allowing you to run arbitrary groovy code from inside SoapUI. Why would you want to do that? Read on and find out.

Preamble


Quite often I've found myself in a scenario where I wanted to automate certain actions in SoapUI, for example the creation of a template project or TestCase, initialization of some properties in a certain way - or preparation of some external database. SoapUI already has pretty extensive scripting support - but these scripts are all event-driven, ie they occur before or after something happens in SoapUI, for example before the execution of a TestCase, or after a project loads. Wouldn't it be nice to have a "playing ground" for exploring the SoapUI object model and executing arbitrary groovy scripts? Mais oui!

Integrating the Groovy Console


Fortunately, groovy already contains a pretty complete Console implementation, the Groovy Console. It allows you to do just what I needed; run arbitrary scripts - load/save these from a file - inspect their results, etc. Since the console is swing component that can be easily embedded and configured in any swing application, putting it into SoapUI was pretty straight-forward. I opted to add a "Groovy Console" action to the popup menus of the Workspace, Project, TestSuite and TestCase nodes in SoapUI - the corresponding consoles will have these objects available for scripting.

From a code-point-of-view this follows the model of the previously created SoapUI plugins described earlier. The only caveat specific to this plugin was the handling of removal of objects and closing of projects, which is done by registering the corresponding listeners to catch these events. To avoid memory leaks, these listeners have to be released accordingly - have a look at the code at GitHub if you are curious how this is done.

The Result


As mentioned above - a "Groovy Console" menu option has been added which opens the corresponding Console in a standalone window;



Here you are free to type and run any scripts using the SoapUI API, and since we opened the console from the Workspace menu, a workspace object is available (as mentioned above, corresponding project, testSuite and testCase objects are available in the consoles opened from their objects menus).

Let's walk through a simple example.   Create an empty workspace (via the File menu) and open the Groovy Console from the workspaces right-click menu. In the console type the following;

project = workspace.createProject( "My Project", null )

press Ctrl-Enter and you will see a new project popping up in the Workspace:





Now let's add a TestSuite, TestCase and HTTP TestStep;

testSuite = project.addNewTestSuite( "TestSuite" )
testCase = testSuite.addNewTestCase( "TestCase" )
testCase.addTestStep( "httprequest", "HTTP Request" )

You will notice that when adding the TestStep a dialog will show up to fill in the endpoint for the TestStep.

Now open the TestCase window and run the TestCase (via scripting):

com.eviware.soapui.support.UISupport.UISupport.showDesktopPanel( testCase )
testRunner = testCase.run( null, false )

finally - lets show an alert with the run status:

com.eviware.soapui.support.UISupport.showInfoMessage( "TestCase finished with status $testRunner.status" )

You can easily save all this to a single file which you load and run as required - I've put it on Gist for you to marvel at :-)

Quirks


The integrated Groovy Console has some quirks that you should be aware of; opening another window via its File Men will create a Console that is "decoupled" from the SoapUI object model. Also - Console windows are not always closed correctly - depending on how you remove/close their corresponding objects containing project. I'll get back to fixing these (and any others you might find) if there is enough interest/nagging - be sure to let me know!

That's it


Download the plugin from here and save it to your soapui\bin\plugins folder (and restart SoapUI). The source code is at GitHub. SoapUI can as always be obtained from http://www.soapui.org.

Have fun exploring SoapUIs groovy APIs - and don't miss the great groovy webinars by the SoapUI team;
- Let's get groovy Webinar
- Let's get groovier Webinar

And please let me know if you have any troubles, suggestions or comments!

thanks for your time!

/Ole