Generic Addon

There may be a scenario you may have to write an utility in your automation project. E.g. It may be a case where you select 2 quantities of the same article in the shopping bag and want to check the total price in the Retail project. Quantity can be anything .. may be 2 or 3 or 4.
Or think of a scenario where you want to verify the mutliple of 2 numbers and validate the result.

This can be achieved with the help of Generic Addons. Let’s see how it is done.

Refer to blog post TestProject Addon to learn about the set up and how to create web addon.

To demonstrate the example, we will use a sample HTML file to verify multiplication of two numbers and validate the result.


Sample HTML file

<!DOCTYPE html>
<html>
  <head>
    <style>
      .h2heading {
        text-align: center;
        font-family: Tahoma;
      }
      #firstNumber,
      #secondNumber {
        margin: 5px;
        font-family: Tahoma;
      }
      body {
        margin: 30px;
      }
    </style>
  </head>

  <body>
    <h2 class="h2heading">Sample form to check Multiplication and Division</h2>
    <form>
      1st Number : <input type="text" id="firstNumber" /><br />
      2nd Number : <input type="text" id="secondNumber" /><br />
      <input type="button" onclick="multiplyBy()" value="Multiply" />
    </form>
    <p>
      The Result is : <br />
      <span id="result"></span>
    </p>

    <script>
      function multiplyBy() {
        num1 = document.getElementById("firstNumber").value;
        num2 = document.getElementById("secondNumber").value;
        document.getElementById("result").innerHTML = num1 * num2;
      }
    </script>
  </body>
</html>

Save file as HTML file. Once the file is saved, make sure to have a local server and run the HTML file.

How to run the html file

With the help of node.js, we can install the server and start the server. Click here to learn on how to start the server and run the html file. Once the html file is run it will look like the below.


Create the Addon

Simple generic addon code

package main.Addon;

import io.testproject.java.annotations.v2.Action;
import io.testproject.java.annotations.v2.Parameter;
import io.testproject.java.enums.ParameterDirection;
import io.testproject.java.sdk.v2.addons.GenericAction;
import io.testproject.java.sdk.v2.addons.helpers.AddonHelper;
import io.testproject.java.sdk.v2.enums.ExecutionResult;
import io.testproject.java.sdk.v2.exceptions.FailureException;
import io.testproject.java.sdk.v2.reporters.ActionReporter;

/**
 * Created by Sisirkant on 7/19/2020.
 */
@Action(name = "Multiply two numbers", description = "Multiplication of 2 numbers")
public class MultiplyTwoNumbers implements GenericAction {

    @Parameter(description = "First input value")
    public int inputOne;

    @Parameter(description = "Second input value")
    public int inputTwo;

    @Parameter(description = "Output value", direction = ParameterDirection.OUTPUT)
    public int output;
    @Override
    public ExecutionResult execute(AddonHelper addonHelper) throws FailureException {
        output = inputOne * inputTwo;
        ActionReporter report = addonHelper.getReporter();
        report.result("Two numbers are multiplied successfully!");
        return ExecutionResult.PASSED;
    }
}

Upload the Addon to the test project

Refer to the previous blog on how to upload the addon to the test project.


Record the test script

Once the test case is recorded, it will be like the below

Edit the test case

Lets edit the test case to store the values in the input parameters that we have created in Addon. Two achieve this, we have to create 2 parameters on the fly to store the values of First Number and Second Number fields.

Store the First Number in firstNumber parameter.

Store the Second Number in secondNumber parameter

Now store the multiplication result in result (user created) parameter


Utilize the Addon that was imported

Now time to use the addon that we have imported. We will assign firsNumber two inputOne, secondNumber to inputTwo and create a new variable MutliplicationOfTwoNumbers to the result.

Add a validation : output is equals to result. Refer to the below screenshots


Run the test case

Once the test case is executed, check the report


Modify the test case to fail it

Let’s modify the test case to fail to make sure that our addon is working fine.

In the validation step, instead of comparing with result, enter e.g. 165. Our inputs are 12 and 12 and of course 144 is not equal to 165. So the test case should fail.

Run the test case again and check the report. The test case should fail.


This is how we can write generic addons / utilities with the help of SDK and it can be used in different test cases.

About This Site

The main aim of this site is to share knowledge with fellow software test specialists who are keen to grow both technically and professionally.

Categories