15 January 2008 - 2.0.1 |
soapUI provides extensive options for scripting, using Groovy as its scripting language (you can get more information, tutorials, etc on the Groovy Web Site). Scripts can be used at the following places in soapUI:
All scripts have access to a number of situation-specific variables, always including a log
object
for logging to the Groovy Log and a context
object for perform context-specific PropertyExpansions and/or
property handling (if applicable). A context-specific variable is always available for directly accessing the soapUI
object model.
Script editors are generally available as inspectors at the bottom of the corresponding objects editor, each having a run button, a drop-down edit menu (same as the right-click popup), an information label, and a help button;
The popup-menu (as shown above) contains standard edit-related actions and will in soapUI Pro contain a "Get Data" menu option that expands to show all properties available within the current scope. Selecting a property (or the option to create a new one) will eventually create a script to get the variable, for example
def test = context.expand( '${#Project#test}' )
Which gets the Project-level "test" property.
It is also possible to drag a property from the navigator tree when it is in Property-Mode into the script; if the dragged property is "within scope" (ie can be accessed via property-expansion), the corresponding access script will be created at the caret location
soapUI Pro adds the possibility to have a central library of Groovy Classes that can be accessed from any script within soapUI, which can be useful for centralizing common tasks/functionality and for creating soapUI extensions. Use as follows;
soapui.demo
should be in a soapui/demo
folder under the specified scripts folder.Remember that the script files must be valid classes, not just arbitrary scripts... see example below.
The following example is included with the soapUI Pro distribution, it consists of the following Greet.groovy file that is located in the default scripts folder:
package soapui.demo class Greet { def name def log Greet(who, log) { name = who; this.log = log } def salute() { log.info "Hello $name" } def static salute( who, log ) { log.info "Hello again $who!" } }
If we create a Groovy Script Step in a TestCase, we can use the above class with the following:
def greet = new soapui.demo.Greet( "Ole", log ) greet.salute()
Running this from within the Groovy Editor will show (roughly) the following in the Groovy Editors log:
Wed Jun 27 01:36:14 CEST 2007:INFO:Hello Ole
We can also call the static salute method:
soapui.demo.Greet.salute( "Ole", log )
Which will produce the following output:
Wed Jun 27 01:36:14 CEST 2007:INFO:Hello again Ole!
If we now modify the above Greet.groovy file and change the first salute method to include an exclamation mark at the end, soapUI will pick up the modified file (once it has been saved), which is seen in the soapUI log:
Wed Jun 27 01:39:20 CEST 2007:INFO:C:\workspace\soapui-pro\scripts\Greet.groovy is new or has changed, reloading...
And now when we rerun our initial script we get;
Wed Jun 27 01:40:16 CEST 2007:INFO:Hello Ole!