A couple of default methods(getters and setters) are available for managing Run-time Test Data in Custom Function code:

1. setRuntimeTestData(String name, String value)

2. getRuntimeTestData(String name)


Create/update runtime test data in Custom Function

1. Extend your Custom Function code class from the class TestsigmaCustomFunctions.

2. Use the testsigma provided method setRuntimeData(name, value) to create a new Runtime Test Data variable named 'name' and set its value as 'value'.

setRuntimeData(name,value);

3. You can also update the value of an already existing Runtime Test Data variable named 'name' to 'value' using the same method.


Fetch runtime test data in Custom Function

Suppose you have already stored some value inside a runtime parameter named 'name' in one of the previous steps using the Store NLP or using another Custom Function. You can then follow the below steps to get the stored value:


1. Extend your Custom Function code class from the class TestsigmaCustomFunctions.

2. Use the testsigma provided method getRuntimeData(name) to get the value stored in a Runtime Test Data named 'name'.

String testdata = getRuntimeData(name);

Note: You can use any variable name instead of 'testdata' to store the data.


Here's a sample Custom Function code that uses these methods to simply store and get Runtime data:

import org.openqa.selenium.WebDriver;

import com.testsigma.customfunc.common.CustomTestStep;
import com.testsigma.customfunc.common.TestsigmaCustomFunctions;
import com.testsigma.customfunc.result.ResultConstants;
import com.testsigma.customfunc.result.TestStepResult;
import com.testsigma.testengine.exceptions.TestEngineException;

public class RuntimeTestDataManager extends TestsigmaCustomFunctions{

    @CustomTestStep
  public TestStepResult storeRuntimeTestData(String name, String value) throws TestEngineException{

setRuntimeData(name,value);
TestStepResult result= new TestStepResult();
result.setStatus(ResultConstants.SUCCESS);
result.setMessage("Runtime Testdata variable "+name+" with value "+value+" has been stored successfully");
return result;
}

@CustomTestStep
public TestStepResult getRuntimeTestData(String name) throws TestEngineException{

String testdata = getRuntimeData(name);
TestStepResult result= new TestStepResult();
result.setStatus(ResultConstants.SUCCESS);
result.setMessage("The fetched Runtime Testdata variable "+name+" has value: "+testdata);
return result;
}
}
   
   

In the above code, the user defined methods storeRuntimeTestData() and getRuntimeTestData() uses the Testsigma provided methods setRuntimeData(name,value) and getRuntimeData(name) to store and fetch Runtime Test Data in the above Custom Function, respectively.


We just need to extend the 'TestsigmaCustomFunctions' class for that.


Points to Remember

1. Please make sure the variable names are not duplicated. This might lead to undesirable results.

2. The scope of Runtime variable is limited to a single Execution Configuration. You can use it in all the Test Cases and Test Suites that is under the same Execution Configuration.

3. You can also use the NLP grammar to manage the Runtime Test Data.


Example Scenario

Suppose you have a Test Case with 2 steps as shown below:


1. Goto https://travel.testsigma.com

2. Store text from the element FirstTicketPrice_label into a variable ticketPrice



Let's say you need to fetch value stored inside the runtime variable ticketPrice, change the value from USD to GBP, and then store it again in the same variable. We can use a Custom Function for that.


The Custom function will take the name of the runtime parameter as its argument.

And within the custom function, we will be using the testsigma provided method getRuntimeData(ticketPrice) to fetch and store the value into a variable named testdata, manipulate its value, and then store it to the same variable using the testsigma provided method setRuntimeData(ticketPrice,testdata)


*The above custom function is just provided for illustration purposes.