JMeter for testing Windows Forms applications using Java Robot class

26 Oct, 2016 | 5 minutes read

Apache JMeter™ is an open-source desktop Java application that is designed to load test and measure performance. It can be used to simulate loads of various scenarios and output performance data in several ways, including CSV, XML files, or graphs. Because it is 100% Java-based, it is available on every OS that supports Java 6 or later.

The protocols supported by JMeter are:

  • Web − HTTP, HTTPS sites ‘web 1.0’ web 2.0 (ajax, flex and flex-ws-amf)
  • Web Services − SOAP / XML-RPC
  • Database via JDBC drivers
  • Directory − LDAP
  • Messaging Oriented service via JMS
  • Service − POP3, IMAP, SMTP
  • FTP Service

As an open-source tool, JMeter has the possibility its extend with a lot of plugins and external implementation of libraries. The most compact way of performing this scenario is written as an external Java Class and implemented in JMeter as external.jar library. The Java Request sampler lets you control a Java class that implements the org.apache.jmeter.protocol.java.sampler.JavaSamplerClient interface and it is packed/exported in .jar file. By writing the implementation of this interface in Java code, JMeter can be used to control input test parameters and test data collection after test execution.

In this article, it will be described:

  1. How java.awt.Robot class can be used in conjunction with JMeter to test Windows Forms application
  2. How to configure custom developed Java class to interact with JMeter
  3. How to configure JMeter for interaction with custom developed Java class

All of the above will be described in the example of testing Windows Calculator as a simple example.

Java Robot Class

Java.awt.Robot class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed, in other words, Robot class can be used to “simulate” mouse clicking or keyboard inputs in any java-based application.

This class has two main functionalities: mouse control and keyboard control.

Mouse control functions

interworks

This function moves the cursor to the coordinate (x, y) which is defined with respect to the top-left screen corner.

interworks-1

This pair of functions performs the mouse click, where buttons are constants usually defined as InputEvent.BUTTON1_MASK – used to simulate clicking of the left mouse button, InputEvent.BUTTON2_MASK – used to simulate clicking of right mouse button.

Keyboard control functions

Keyboard action is emulated by the following pair of functions:

interworks-3

Keycodes are defined in java.awt.event.KeyEvent.

Developing Java class that interacts with JMeter

Interaction with your custom-developed Java class and JMeter, for the purpose of this example, is described in the following diagram:

Interaction with custom developed Java class and JMeter

In order for your custom-developed Java class to interact with JMeter, it needs to extend org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient and implement the method runTest from org.apache.jmeter.samplers.SampleResult class.

Note: ApacheJMeter_core.jar and ApacheJMeter_java.jar need to be included as external libs in the Eclipse project for building JMeter .jar libraries. ApacheJMeter_core.jar and ApacheJMeter_java.jar are placed in <JMeter_home>/lib/ext folder.

ApacheJMeter_core.jar and ApacheJMeter_java.jar need to be included as external libs in the Eclipse project for building JMeter .jar libraries

JMeter: Define input test parameters

To define input test parameters in JMeter, you can use the User Defined Variables built-in configuration element. In this simple example, there are two input parameters, named Num1 and Num2 which sum will be calculated using Windows Calculator.

Two input parameters, named as Num1 and Num2 which sum will be calculated using Windows Calculator.

Further these parameters need to be transferred to the JavaRequest sampler in order to be utilized in custom-developed Java class as variables named Number1 and Number2, respectively.

Transfer of these parameters to the JavaRequest sampler

Note: Classname property of the JavaRequest sampler is set up to use custom custom-developed Java class that was exported as .jar. In order exported .jar to be shown in the Classname dropdown, it needs to be placed in <JMeter_home>/lib/ext folder.

Java Class: Get input test parameters in the clipboard

In order for your Java class to manipulate with input test parameters, those test parameters need to be transferred from JMeter to the developed Java class and put into the clipboard, so the Robot can “manipulate” with the test parameters.

Transferring test parameters from JMeter to Java Class

To use test parameters defined in JMeter, in your custom-developed Java class, first, it is needed to define JMeter parameters as Arguments implementing the method getDefaultParameters from AbstractJavaSamplerClient class.

Transferring test parameters from JMeter to the custom-developed class is done by the getParameter method from JavaSamplerContext under the implementation of the runTest method.

Putting test parameters into the clipboard

For the purpose of putting test parameters into the clipboard, StringSelection and Clipboard can be used from java.awt.datatransfer package and getSystemClipboard method from java.awt.Toolkit class. In this example, we implemented the method setClipboardContents that provides the functionality of putting test parameters into the clipboard.

Java Class: Execute Robot commands

Keyboard control methods from java.awt.Robot class can be used to open Windows Calculator, enter above explained test parameters, and their sum will be calculated. The sum is put into a clipboard in order to be further transferred to JMeter for assertion (note that on particular points a delay on execution is added, so the robot “waits” until particular operations are completed).

Java Class: Set test results in clipboard and transfer to JMeter

The test result, in this example the sum, from the clipboard is “returned” to custom custom-developed Java class with the implement method getClipboardContents.

Finally, the test result is transferred to JMeter by using org.apache.jmeter.samplers.SampleResult class, which is implemented under the runTest as a return object.

JMeter: Assert the test result returned from the custom-developed Java class

The test result from the developed Java class can be asserted or further processed in your JMeter test. In this example, a simple Response Assertion is used that verify the sum of input test parameters. Used function __intSum is built in JMeter function, while variables ${Num1} and ${Num2} are defined in the User Defined Variables element named in this example as Input Test Parameters.

Variables ${Num1} and ${Num2} are defined in User Defined Variables element

After running the JMeter test, the calculator will be opened, numbers will be summed and after the test is finished, under View Results Tree element will display the test result.

The test result displayed under View Results Tree element

You can download, the developed Java class used for this example: robotwincalculator-java, exported .jar: robotwincalculator-jar and JMeter test: jmeterwincalculator.

Additional and interesting samples using JMeter to test other Win applications such as MS Word and MS Excel can be also downloaded as jmeterwinword and jmeterwinexcel.