The never-ending quest for somewhat useful SoapUI plug-ins has led to a Groovy Console plug-in, which allows you to run arbitrary Groovy code from inside SoapUI. Why would you want to do that? Read on and find out.
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, i.e. 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 plug-ins described earlier. The only caveat specific to this plug-in 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.
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
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!
If you’d like to have a little more fun exploring SoapUI’s Groovy APIs, please feel free to view the recording of our “Let’s Get Groovy” webinar below. In this recording, the SoapUI team demonstrates how easily you can leverage the power of Groovy scripts in SoapUI, clean up your test data and control the test flow, along with other useful tips on Groovy scripting.
Also feel free to watch our most recent Groovy webinar, which was held live on February 20. In this recording, Niclas Reimertz, Erik Yverling, Henrik Olsson and Henrik Johansson show some of the advanced aspects of using Groovy in SoapUI and LoadUI, including:
- MockService Scripting in SoapUI
- Script Assertion in SoapUI
- Create a custom component in LoadUI
And please let me know if you have any troubles, suggestions or comments!
Thanks for your time!
About the author:
Ole Lensmar is the creator of SoapUI and the Chief Architect at SmartBear Software. He’s passionate about most things related to software development; designing, coding, testing, deploying – you name it. This article was originally posted on Ole’s own blog.
- Digging in with SmartBear’s API Tools
- Using the JDBC TestStep in SoapUI
- Click! – Using Gherkin with soapUI