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
For creating a Custom Function, you can navigate to the Custom Functions page as follows:
Select Test Development > Custom Functions from the left context menu
Steps for creating a Custom Function is explained in detail in the following section Create the Custom Function.
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 Test Development > Test Cases from the left context menu
Steps for creating a Test Step using a Custom Function is explained in detail in the following section Add a Test Step using the Custom Function.
Info: If you have not enabled multiple Applications and Versions, you don't need to select a specific application and version.
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: Select the previously created Custom Function from the list.
- 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 successful execution of this step compulsory for a Test Case to pass.
- Prerequisite: Select a previously created Test Step if successful execution of that step is necessary 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 a simple web page alert dialog using the JavascriptExecutor Class 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 com.testsigma.customfunc.common.TestsigmaCustomFunctions;
import com.testsigma.customfunc.common.CustomTestStep;
import com.testsigma.customfunc.result.ResultConstants;
import com.testsigma.customfunc.result.TestStepResult;
public class CustomStepsSample extends TestsigmaCustomFunctions{
@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;
}
}
Note: Remove the package declaration on line 1 before compiling the Custom Function Code in Testsigma
2. Copy the code and create a new Java Class inside the package "com.testsigma.customfunc.web.examples" by renaming the Class name CustomStepsSample and method name createCustomSteps. You may place the Class inside any package under com.testsigma.customfunc package as per your convenience.
3. Put your Custom code between lines 12 to 14 and fix the compilation errors. You can use an IDE for this(Recommended: Eclipse or Netbeans IDE)
Given below is a sample Custom Function for our JavaScriptExecutor code that creates the alert shown in the beginning of this section:
import com.testsigma.customfunc.common.TestsigmaCustomFunctions;
import com.testsigma.customfunc.common.CustomTestStep;
import com.testsigma.customfunc.result.ResultConstants;
import com.testsigma.customfunc.result.TestStepResult;
import org.openqa.selenium.JavascriptExecutor;
public class JSAlert extends TestsigmaCustomFunctions{
@CustomTestStep
public TestStepResult createAlert() {
//your custom code starts here
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("alert('Hi');");
//your custom code ends here
TestStepResult result= new TestStepResult();
result.setStatus(ResultConstants.SUCCESS);
result.setMessage("custom step Executed successfully");
return result;
}
}
As you can see in the above code, we do not have any package declarations. We need to remove the first line i.e package declaration in the Custom Function Code for the code to compile successfully.
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!!