|
soapUI Pro 1.7.6 introduces the possibility to create "extensions" to the soapUI core, allowing custom functionality to be made available both as custom actions and internal event listeners. This article shows you how to create a custom listener that dumps all results in a LoadTest to a file for post-processing
Listener extension basics are described on the soapui.org site in the "Extending" Document . Let's use what we learned there to create a custom LoadTestListener that dumps all TestStep results to a comma-separated file for post-processing.
The Listener
The listener (created in groovy) is rather trivial:
package soapui.demo
import com.eviware.soapui.SoapUI
import com.eviware.soapui.model.support.LoadTestRunListenerAdapter
import com.eviware.soapui.model.testsuite.LoadTestRunContext
import com.eviware.soapui.model.testsuite.LoadTestRunner
import com.eviware.soapui.model.testsuite.TestRunContext
import com.eviware.soapui.model.testsuite.TestRunner
import com.eviware.soapui.model.testsuite.TestStepResult
public class LoadTestRunListener extends LoadTestRunListenerAdapter
{
def file
def writer
def cnt = 0
public void beforeLoadTest(LoadTestRunner runner, LoadTestRunContext context)
{
file = new File( runner.loadTest.name + ".txt" )
writer = file.newWriter()
SoapUI.log.info( "Created LoadTest output file at [${file.absolutePath}]" )
}
public synchronized void afterTestStep(LoadTestRunner runner, LoadTestRunContext context, TestRunner testRunner, TestRunContext testContext, TestStepResult result)
{
writer.writeLine(
"${cnt++},${result.testStep.name},${result.timeTaken},${result.status}")
}
public void afterLoadTest(LoadTestRunner runner, LoadTestRunContext context)
{
writer.close()
SoapUI.log.info( "Closed LoadTest output file at [${file.absolutePath}] with ${cnt} rows" )
}
The listener extends the internal LoadTestRunListenerAdapter class and overrides only those methods of interest:
- beforeLoadTest : called before the LoadTest is executed. Here we just create the output file and writer
- afterTestStep : called after each TestStep execution; we write a row to the file containing an internal counter, the name of the associated TestStep, how much time was taken and the status of the results. This method is synchronized since it can be called by multiple threads simultaneously which could produce invalid outputs
- afterLoadTest : here we just gracefully close the writer
Save the above to a file called (for example) LoadTestRunListener.groovy in your script library
The Configuration
Now we need to tell soapUI Pro to load the above listener; add the bellow to the existing demo-listeners.xml file:
<tns:listener id="LoadTestListener"
listenerClass="com.eviware.soapui.support.scripting.listeners.ScriptLoadTestRunListener"
groovyClass="soapui.demo.LoadTestRunListener"
listenerInterface="com.eviware.soapui.model.testsuite.LoadTestRunListener"/>
Here we use the ScriptLoadTestRunListener utility to "wrap" our listener, providing reload support on modifications (so we don't need to restart soapUI Pro every time we make a change).
Restart soapUI Pro and you should see (at least) the following rows in the soapUI log:
...
01:36:53,539 INFO [SoapUIProGroovyScriptEngineFactory] 5 classes loaded
...
01:36:53,539 INFO [DefaultSoapUICore] Adding listeners from [C:\workspace\soapui-pro\listeners\demo-listeners.xml]
...
The Results
Ok, we're all set to run our first listener. Fire up your favorite LoadTest and run it! After it finished you should have something like the following in the soapUI log:
...
Sat Sep 29 02:00:42 CEST 2007:INFO:Created LoadTest output file at [C:\workspace\soapui-pro\LoadTest 1.txt]
...
Sat Sep 29 02:00:52 CEST 2007:INFO:Closed LoadTest output file at [C:\workspace\soapui-pro\LoadTest 1.txt] with 1985 rows
...
and the created log file contains something similar to
0,ServerSendBase64RPC - Request 1,9,OK
1,ServerSendBase64RPC - Request 1,10,OK
2,ServerSendBase64RPC - Request 1,20,OK
3,ServerSendBase64RPC - Request 1,23,OK
etc...
Now What?
Well, there are of course several next steps possible:
1) Extend the logged information with more columns
2) Write the data to a database instead of a textfile
3) Create a configuration dialog for configuring the above listener
4) etc...
Also, it might be wise to stop using the ScriptLoadTestRunListener when all is working ok, since it takes a little time to always check for an update in the script library. The modified configuration would be as follows:
<tns:listener id="LoadTestListener"
listenerClass="soapui.demo.LoadTestRunListener"
listenerInterface="com.eviware.soapui.model.testsuite.LoadTestRunListener"/>
That's it for now! Hope this gave an insight into the extension possibilities currently available, of course there are many areas of improvement available.
Good Luck!
|