Writing a Custom Module using the Scala language

Document created by Dave Murphy Employee on Jul 19, 2017
Version 1Show Document
  • View in full screen mode

Introduction

 

The CloudTest “Custom Modules” feature allows custom Java code, contained in one or more jars, to be called from CloudTest Scripts. The Scala language runs on the Java JVM.  Therefore it is also possible to create Custom Modules using Scala and call Scala code from CloudTest Scripts. This article describes how to do that.

 

The Scala code can do anything that Java code can do, such as add Transactions and Messages to the CloudTest Result.

 

This article assumes you have Scala installed on your machine.

 

The Scala code

 

First, we’ll create the Scala code. The Scala compiler produces class files that can be accepted by the JVM. Therefore, the basic approach is to compile the Scala code into class files and then create a jar file from those. You can, of course, use any development environment and/or tools you like to achieve this. In this example, we’re using simple OS command-line tools.

 

Here’s our example Scala code. It is a simple class with one method. The constructor takes a pointer to the SOASTA Scripting Custom Module API. The single method takes a string as input, outputs a message to the CloudTest Result, and then returns a string.

 

// This is what I named my Scala package. // This package name will be referenced // in the CloudTest Script that calls the Custom Module. package myscalaexample  // Import what is needed from the SOASTA Scripting Custom Module API. import com.soasta.scripting.v1.Result import com.soasta.scripting.v1.ResultLevel import com.soasta.scripting.v1.ScriptEnvironment  // The class. // Constructor takes a pointer to the SOASTA Scripting Custom Module API. class MyCustomModule(env: ScriptEnvironment) {     // The method that the CloudTest Script will call.     // It takes a string as input and returns a string.     def doSomething(someInput: String): String =     {         // Output a message to the CloudTest Result.         val result = env.getContext().getResult();         result.postMessage(             ResultLevel.INFO,             "Message from Scala: doSomething was called with this input",             someInput);          // Return string output to the calling CloudTest Script.         return s"The input to MyCustomModule.doSomething was: $someInput"     } }

 

Create a directory in the file system to hold the Scala code. In this article, we’ll call that directory “/ScalaExample”.

Place the above Scala code into the file “MyCustomModule.scala”, in the directory “/ScalaExample /CustomModule/myscalaexample”.

 

Download the Scripting Custom Module Java API jar (“cloudtest-scripting-environment.jar”) from CloudTest, and place it in the directory “/ScalaExample /CustomModule”.

 

Compile the Scala code by running the following command on the OS command line in the “/ScalaExample /CustomModule” directory:

 

scalac myscalaexample/MyCustomModule.scala -cp cloudtest-scripting-environment.jar

which produces the file “MyCustomModule.class”.

 

Then create a jar by running the following command on the OS command line:

 

jar cf MyCustomModule.jar myscalaexample/MyCustomModule.class

which produces the file “MyCustomModule.jar”.

 

Importing the Scala code into CloudTest

 

In CloudTest, create a new folder called “/Scala Custom Module example” to contain this example.

 

Create a new Custom Module in that directory by importing the jar file created above into a new Custom Module called “MyCustomModule.jar”.

 

Depending upon what features of the Scala language are used, some jars from the Scala language itself must be included in CloudTest. You will find these jars in the Scala installation’s “lib” sub-directory. At a minimum, for even the simplest Scala program, the jar named “scala-library.jar” wil be needed. Import that jar into CloudTest.

 

Which particular Scala jars are needed depends upon what the Scala program does. To be safe, one could always just import all of the jars in Scala’s “lib” sub-directory. Note that only one set of the Scala jars is needed in CloudTest, to be shared among all Scripts that call Scala.

 

Create the CloudTest Composition

 

In the “/Scala Custom Module example” folder in CloudTest, create the Script that will call the Scala Custom Module. Here’s a simple example:

 

ScalaScript.png

 

As shown above, link the Scala Custom Module created earlier (“MyCustomModule.jar”) to the Script. Also link the Custom Modules for any needed Scala jars. In the above simple example, only the single core jar “scala-library.jar” is needed.

 

Create a CloudTest Clip containing this Script, and then create a CloudTest Composition containing that Clip.

 

Playing the Composition

 

When the Composition is played, the Script will call into the example Scala code. The Scala code will add an event to the Result showing the input passed from the Script. The Script will then add an event to the Result showing the output returned from the Scala code.

 

 

Although this example only output events to the Result, the Scala code can of course do anything that Java code can do, such as add Transactions and Messages to the CloudTest Result.

Attachments

    Outcomes