Skip to main content

Custom Virtual User (VU) Example

For our example, assume we are testing a Java thick client application. Although the example is in Java, exactly the same process can be applied to create both C++ and .NET virtual users (VUs).

For simplicity, the Java application contains a single class, called JavaThickClient, which contains just two methods: the login() method logs in to the server using the username and password provided, and the doSomething() method performs an operation on the server.

To test this application, we want to create a workflow containing two scripts: a Login script that will logon to the server, and a DoSomething script that will execute operations on the server. The Login script will be an initial script (i.e. run once at the start of the test) and the DoSomething script will be an iterated script that will be called repeatedly with different parameters.

The first step is to create a new Java VU type, based on the existing JavaVirtualUser type. See Creating a Custom Virtual User type for instructions on how to do this.

Once we have a custom VU type, we need to add code to our newly created classes. For each VU in the test, we will need to create an instance of JavaThickClient, call the login() method from the initial Login script, and then call the doSomething() method from the iterated DoSomething script. It should be obvious then, that a JavaThickClient instance will need to have a longer lifetime than a single script. Therefore, we will store it as a private member of our custom virtual user class (see Relationship between VirtualUser and VirtualUserScript objects):

public class MyJavaVU extends com.facilita.fc.runtime.VirtualUser
{
private JavaThickClient javaThickClient;

@Override
public void pre() throws Exception
{
...

Next, we will provide methods in the MyJavaVU class that delegate to the methods in the JavaThickClient class:

public class MyJavaVU extends com.facilita.fc.runtime.VirtualUser
{
private JavaThickClient javaThickClient;

public void login(String username, String password)
{
this.javaThickClient = new JavaThickClient(username, password);
this.javaThickClient.login();
}

public void doSomething(String parameter)
{
this.javaThickClient.doSomething(parameter);
}

@Override
public void pre() throws Exception
{
...

We want to be able to call the methods on the MyJavaVU class directly from our scripts. To this end, we will add helper methods to our MyJavaVUScript class that forward calls to the MyJavaVU class:

public abstract class MyJavaVUScript extends com.facilita.fc.runtime.VirtualUserScript
{
public void login(String username, String password)
{
getVU().login(username, password);
}

public void doSomething(String parameter)
{
getVU().doSomething(parameter);
}

@Override
public void pre() throws Exception
{
...

We are now in a position to create our two script classes. First the Login script:

public class Login extends CustomVUExample.MyJavaVUScript
{
@Override
public void pre() throws Exception
{
//do not remove following line
super.pre();
}
@Override

public void script() throws Exception
{
//TODO Place your script variables here.
//TODO Place your iterated script code here.
login(getString("username"), getString("password"));
}
}

Secondly, the DoSomething script:

public class DoSomething extends CustomVUExample.MyJavaVUScript
{
@Override
public void pre() throws Exception
{
//do not remove following line
super.pre();
}

@Override
public void script() throws Exception
{
//TODO Place your script variables here.
//TODO Place your iterated script code here.
doSomething(getString("parameter"));
}
}

Note that, in both scripts, the method parameters have been parameterized so that they can be obtained from a data file at runtime.

The relationship between all of the classes in this example is described by the following class diagram:

The complete source code for this example can be viewed here.