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



17 comments:

  1. This seems like a huge time-saver... test automation taken to a new level! Awesome.

    ReplyDelete
  2. Hi Ole,


    I need help on to add multiple xml nodes in the request based on the data source. As you see as below,
    I have product that I want to verify the price of products by state and location. State can have only one product or multiple products. Based on this scenario, I would like to add Product Detail xml node based on the data source.


    Here is sample file that I have created.









    VA





    ${DataSource#P_Location}

    ${DataSource#P_Name}

    ${DataSource#P_ProductDetail}













    Data set in excel



    State Location ProductName ProductDetail Prices

    VA Arlington Apple IPhone4 600

    Reston Apple Iphone5 700

    MA Fairfax Apple IPhone4 600

    MD Gaithersburg Apple Iphone4 500

    Gaithersburg Apple Iphone5 600

    Gaithersburg Apple IPod 495

    GA Chantilly Apple IPhone4 300





    Please let me know



    Thank you

    ReplyDelete
    Replies
    1. hi visit this blog you will find result..

      http://lgsofttest.blogspot.no/2013/10/how-to-add-or-deletecomment-xml-node-or.html

      Delete
  3. I tried installing this in SoapUI Pro, but it didn't work.
    - Installed SoapUI
    - Created SoapUIPro/bin/plugins directory
    - Downloaded the soapui-groovy-console-plugin-0.1-SNAPSHOT-plugin.jar
    - Place the jar in SoapUIPro/bin/plugins
    - Start SoapUIPro ....

    I do not see any Groovy Console in any menus.

    ReplyDelete
    Replies
    1. Hi,

      hmm... can you show the contents of the soapui log tab at the bottom of the main window!?

      thanks,

      /Ole

      Delete
  4. Hi Ole,

    I just have a query. Is this feature only for SoapUI Pro or also SoapUI ??
    I configured the groovy console in SoapUI, but when I try to save a script in a folder named ext in the bin folder. It gives me many exceptions

    rg.codehaus.groovy.runtime.InvokerInvocationException: java.io.FileNotFoundException: C:\Program Files\SmartBear\soapUI-4.5.1\bin\ext\scripts\CheckEmailNotificationClass.groovy (Access is denied)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:97)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Caused by: java.io.FileNotFoundException: C:\Program Files\SmartBear\soapUI-4.5.1\bin\ext\scripts\CheckEmailNotificationClass.groovy (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.(Unknown Source)

    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:271)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:55)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at groovy.ui.Console.fileSave(Console.groovy:628)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    ... 45 more
    Please help me with this . .

    ReplyDelete
    Replies
    1. Hi!

      looks like you don't have write permissions to this folder - try saving the scripts under your home/documents folder instead, or try running SoapUI as an administrator.

      Hope this helps!

      /Ole

      Delete
    2. hi,
      is thi ad in only for soapui 4.5.1. does it work for soapui 4.5.2 free version?
      i am not seeing that menu in the list.

      Delete
    3. Hi,

      yes - it should work for 4.5.2 as well - do you see the plugin being picked up by SoapUI in the soapui log when SoapUI starts?

      /Ole

      Delete
    4. No. I have placed the 'soapui-groovy-console-plugin-1.0-plugin.jar' file in "C:\Program Files (x86)\SmartBear\soapUI-4.5.2\bin\ext" path. But i am not seeing the Groovy Console option in the menu list when i right click the root in Soapui 4.5.2

      Delete
    5. Hi,

      the plugin should be saved to C:\Program Files (x86)\SmartBear\soapUI-4.5.2\bin\plugins - you will probably have to create the plugins folder first.

      Hope this helps!

      /Ole

      Delete
    6. it is working fine now. thank you.

      Delete
  5. Hi,
    I tried to download the plugin, but once i click the download link, it displays blank page and "The file you're looking for has been deleted or moved.' message is displayed. Please help me.

    ReplyDelete
    Replies
    1. Hi!

      I'm sorry - the download is now at http://sourceforge.net/projects/soapui-plugins/files/soapui-groovy-console-plugin/ - I've updated the link in the post accordingly. Thanks for pointing this out!

      /Ole

      Delete
  6. Thanks for creating this - going to make my job a lot quicker :)

    ReplyDelete
  7. Hi,

    How do I add a WSDL/WADL file after creating the project? It seems soapUI doesn't provide this kind of API?

    Thx - Yulin

    ReplyDelete
  8. Hi Yulin,

    check out how this is done in the importWadl/importWsdl methods in the NewGenericProjectAction class: https://github.com/SmartBear/soapui/blob/master/soapui/src/main/java/com/eviware/soapui/impl/actions/NewGenericProjectAction.java

    Hope this helps!

    /Ole
    SmartBear Software

    ReplyDelete