A Bare-Bones Process Runner Component for loadUI

Here comes something I put together on the train on my way to work this morning; a very simple loadUI runner component that executes any desired process command when triggered. Use it for pinging, sending mail or running other programs as part of a loadUI test. It's only around 40 lines of code, so let's jump right in!

This time let's start with the actual code:

/**
* Runs a process
*
* @name Process Runner
* @category runners
*/

// the command itself
createProperty('command', String,"")

sample = { message, sampleId ->
try
{
// start a process and wait for it to finish
def proc = command.value.execute()
proc.waitFor()

// add result properties
message["ExitValue"] = proc.exitValue()
message["Stdout"] = proc.in.text
message["Errout"] = proc.err.text

// fail if process failed
if( proc.exitValue() != 0 )
failureCounter.increment()
}
catch( e )
{
// add error properties
message["ExitValue"] = -1
message["Errout"] = e.message

failureCounter.increment()
}

return message
}

layout {
property( property:command, label:"Command", constraints: 'w 200!' )
action( label:'Run Once', action: { triggerAction('SAMPLE') }, constraints: 'aligny bottom' )
}


This is a very bare-bones runner component;
  • The header specifies it to be a runner (which takes care of a lot of internals) and gives the component a nicer name
  • One property is defined for holding the actual command
  • The "sample" message handler (which is called for each incoming trigger message) first uses groovys process support for running the command and then adds the results to the outgoing message
  • The global failure counter is incremented either if the process returns an error code or if it fails to start altogether (for example if the command is incorrect)


The UI is equally minimalistic:

But it does what it is intended for; runs the specified command as a process. So for example if we want to run ping regularly we can have the following setup:

Click here for full size image

The generator triggers the process runner which runs a simple ping command;

ping -n 1 192.168.250.4


The output is sent both to a TableLog so we can see what's going on, to a statistics component so we can get some execution stats, and to an assertion that will fail if the ping takes more than 500ms. Also, since ping returns a non-zero exit code when failing to reach the target host, the component will automatically increase the global failure counter if the ping fails.

That's it! I'm going to leave the rest to you, for example you could add

  • The common counters available on all runner components
  • The possibility to specify "arguments" to the runner via its input message which would be substituted into the command before it is executed
  • More detailed outputs that write each line from stdout or stderr as separate messages
  • etc


To install the component simply create a "ProcessRunner.groovy" file in your loaduiscript-components folder and set its contents to the above code; save the file and it will be available in loadUI in the "Runners" category of the component bar right away.


Have fun!


/Ole


Close

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox