Introduction


Though there is an extensive list of Natural Language Commands/Statements in addition to Test Data Functions available in Testsigma, there might be situations where you need to create Test Steps tailored to your needs. That is the reason why Testsigma provides an option for creating Custom Functions. Custom Functions allows us to use Java(Selenium) program code to make Test Steps that are customised for our needs.


Note: You need to be familiar with writing Selenium based Java Code to create Custom Functions in Testsigma. If you would like to avoid the hassle of creating Custom Functions yourself, you can request our Support Team to create one for you in a very short time!


There are primarily two steps in creating a Custom Function inside Testsigma.

1. Create a Custom Function

2. Add a Test Step using the Custom Function


Navigation


For creating a Custom Function, you can navigate to the Custom Functions page as follows:


Select Project > Select Application > Select Version and then selecting Custom Functions from the left context menu


Steps for creating a Custom Function is explained in detail in the section Create the Custom Function given below.




After creating the Custom Function, we can create a Test Step using the Custom Function by navigating to the Create Test Step page as follows:


Select Project > Select Application > Select Version and then selecting Test Cases from the left context menu


Steps for creating a Test Step using a Custom Function is explained in detail in the section Add a Test Step using the Custom Function given below.



1. Create the Custom Function


In the Custom Functions page, click on the Create button to create a new Custom Function. The Create Custom Function page looks as shown below:



Enter the details as given below:


Class Name (required): Enter the name of the Class used in the Custom Function code.


Custom Function Class (required):  Enter the Custom Function java code in this field.

Check this article to know more about creating Custom Function code - Custom Function code guidelines


Here's a sample input for the Custom Function:




After entering the Custom Function Code and the Class Name in the code, click on Compile and Save to invoke the inbuilt compiler and save the code. 


If there are compilation errors, they will be shown below the Custom Function code input area. Resolve the compilation issues and click on Compile and Save again to check.

If compilation completes successfully without errors, the Custom Function will be saved successfully and you will get a success notification.


The newly added Custom Function will now be visible on the Custom Functions page.


Now you can use this Custom Function in the Create Test Step page and the process is explained in detail in the following section.


2. Create Test Step from Custom Function

We can use previously created Custom Function in the Create Test Step page by selecting the Custom Function radio button while creating a Test Step.


1. First of all, navigate to the Test Case details page and click on the Create Test Step button to start creating a Test Step.


2. Select the Custom Function radio button at the top of Test Step Creation wizard to create a Test Step from Custom Function.



3. The Test Step wizard changes to a Create Custom Function wizard as shown below:


Enter the required details as follows:

  • Step Details: A detailed description of the Test Step.
  • Function: Enter the Function name inside the Custom Class that is to be executed.
  • Arguments: Enter the arguments for the Function( if there are any).
  • Priority: Select the Priority level that you want to set for this Test Step - Major, Minor or Medium.
  • Mandatory: Select the Mandatory checkbox to make the execution of this step compulsory for a test case.
  • Prerequisite: Select a previously created Test Step if successful execution of that step is mandatory before executing this Test Step.


* Some of the options might be visible only when applicable


Here's a sample input for the Custom Function wizard:


After entering all the required details, click on Create button to Create the Test Step using the Custom Function. We would get a success notification for Test Step creation and the Test Step will be listed as shown below:


Custom Function Example

Let us assume you have a code block that creates an alert using the Javascript Execution in Selenium. The code is given below:

JavascriptExecutor jse = new JavascriptExecutor();
jse.executeScript(alert("Hi"));


We will see how we can create a Custom Function from the above code.


1. First, clone the Testsigma Custom Function repository. You can find the following example code in the package com.testsigma.customfunc.web.examples inside the class named CustomStepsSample.


package com.testsigma.customfunc.web.examples;

import org.openqa.selenium.WebDriver;
import com.testsigma.customfunc.common.CustomTestStep;
import com.testsigma.customfunc.result.ResultConstants;
import com.testsigma.customfunc.result.TestStepResult;


public class CustomStepsSample {
  static WebDriver webdriver;
  public CustomStepsSample(WebDriver webdriver){
    this.webdriver = webdriver;
  }
  
  @CustomTestStep
  public TestStepResult createCustomSteps(){
    //your custom code starts here
    
    //your custom code ends here
    TestStepResult result = new TestStepResult();
    result.setStatus(ResultConstants.SUCCESS);
    result.setMessage("Successfully Executed");
    return result;
  }    
}


2. Copy the code and create a new Java Class inside the package "com.testsigma.customfunc.web.examples". You may place the Class inside any package under com.testsigma.customfunc package as per your convenience. Also, Rename the Class and method.


3. Put your Custom code between lines 17 to 19 and fix the compilation errors. You can use an IDE for this(Recommended: Eclipse or Netbeans IDE)


Given below is the above code customised for our custom JavaScript Executor code:

import org.openqa.selenium.WebDriver;
import com.testsigma.customfunc.common.CustomTestStep;
import com.testsigma.customfunc.result.TeststepResult;

public class JSAlert{
    public static WebDriver driver;
    public JSAlert(WebDriver driver){
        this.driver = driver;
    }
    
    @CustomTestStep
    public TeststepResult createAlert() {
        //your custom code starts here
        JavascriptExecutor jse = new JavascriptExecutor();
        jse.executeScript(alert("Hi"));
        //your custom code ends here
        JavascriptExecutor jse = new JavascriptExecutor();
        jse.executeScript(alert("Hi"));
        TeststepResult result= new TeststepResult();
        result.setStatus(0);
        result.setMessage("custom step Executed successfully");
        return result;
    }
}


4. Once the compilation errors are resolved, create a Custom Function with the code as per the above section 1.  Create a Custom Function.




That's all we need to to in order to create a Custom Function.


Happy Test Automation!!