From 0634a80b007903a46ed1f08d7b521ceebb738287 Mon Sep 17 00:00:00 2001 From: ManojTestsigma Date: Tue, 31 Mar 2026 21:47:17 +0530 Subject: [PATCH] [CUS-11673] add data generator to get the value from the list string. --- list_data_structure_actions/pom.xml | 2 +- .../GetElementCountBySeparator.java | 45 +++++++++ .../WhileLoopStringBySeparator.java | 77 +++++++++++++++ .../restapi/WhileLoopStringBySeparator.java | 96 +++++++++++++++++++ 4 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 list_data_structure_actions/src/main/java/com/testsigma/addons/datagenerator/GetElementCountBySeparator.java create mode 100644 list_data_structure_actions/src/main/java/com/testsigma/addons/datagenerator/WhileLoopStringBySeparator.java create mode 100644 list_data_structure_actions/src/main/java/com/testsigma/addons/restapi/WhileLoopStringBySeparator.java diff --git a/list_data_structure_actions/pom.xml b/list_data_structure_actions/pom.xml index 8b979c3e..8a39ad07 100644 --- a/list_data_structure_actions/pom.xml +++ b/list_data_structure_actions/pom.xml @@ -6,7 +6,7 @@ 4.0.0 com.testsigma.addons list_data_structure_actions - 1.0.0 + 1.0.2 jar diff --git a/list_data_structure_actions/src/main/java/com/testsigma/addons/datagenerator/GetElementCountBySeparator.java b/list_data_structure_actions/src/main/java/com/testsigma/addons/datagenerator/GetElementCountBySeparator.java new file mode 100644 index 00000000..053f462a --- /dev/null +++ b/list_data_structure_actions/src/main/java/com/testsigma/addons/datagenerator/GetElementCountBySeparator.java @@ -0,0 +1,45 @@ +package com.testsigma.addons.datagenerator; + +import com.testsigma.sdk.TestData; +import com.testsigma.sdk.annotation.TestDataFunction; +import com.testsigma.sdk.annotation.TestDataFunctionParameter; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; + +@Data +@TestDataFunction(displayName = "Get count of elements in separator separated test-data", + description = "Splits the test-data using the given separator and returns the count of elements") +public class GetElementCountBySeparator extends com.testsigma.sdk.TestDataFunction { + + @TestDataFunctionParameter(reference = "separator") + private com.testsigma.sdk.TestData separator; + + @TestDataFunctionParameter(reference = "test-data") + private com.testsigma.sdk.TestData inputData; + + @Override + public TestData generate() throws Exception { + try { + String separatorValue = this.separator.getValue().toString(); + String inputValue = this.inputData.getValue().toString(); + + if (inputValue.isEmpty()) { + logger.info("Input data is empty, returning count as 0"); + setSuccessMessage("Input data is empty. Element count: 0."); + return new TestData("0"); + } + + String[] elements = inputValue.split(separatorValue, -1); + int count = elements.length; + + logger.info("Element count: " + count); + setSuccessMessage(String.format("Successfully retrieved element count: %d.", count)); + return new TestData(String.valueOf(count)); + } catch (Exception e) { + setErrorMessage("Failed to get element count due to " + e.getMessage()); + logger.warn("Failed to get element count." + ExceptionUtils.getStackTrace(e)); + throw new Exception("Failed to get element count due to " + e.getMessage()); + } + } + +} diff --git a/list_data_structure_actions/src/main/java/com/testsigma/addons/datagenerator/WhileLoopStringBySeparator.java b/list_data_structure_actions/src/main/java/com/testsigma/addons/datagenerator/WhileLoopStringBySeparator.java new file mode 100644 index 00000000..98b7618a --- /dev/null +++ b/list_data_structure_actions/src/main/java/com/testsigma/addons/datagenerator/WhileLoopStringBySeparator.java @@ -0,0 +1,77 @@ +package com.testsigma.addons.datagenerator; + +import com.testsigma.sdk.TestData; +import com.testsigma.sdk.annotation.RunTimeData; +import com.testsigma.sdk.annotation.RunTimeDataProvider; +import com.testsigma.sdk.annotation.TestDataFunction; +import com.testsigma.sdk.annotation.TestDataFunctionParameter; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; + +@Data +@TestDataFunction(displayName = "Store current iteration value using separator separated values", + description = "Iterate separator separated values in test-data and return the current iteration value") +public class WhileLoopStringBySeparator extends com.testsigma.sdk.TestDataFunction { + + @TestDataFunctionParameter(reference = "separator") + private com.testsigma.sdk.TestData separator; + + @TestDataFunctionParameter(reference = "test-data") + private com.testsigma.sdk.TestData inputData; + + @RunTimeData + private com.testsigma.sdk.RunTimeData currentIterationIndex; + + @RunTimeDataProvider + private com.testsigma.sdk.RuntimeDataProvider currentIterationIndexProvider; + + @Override + public TestData generate() throws Exception { + try { + String separatorValue = this.separator.getValue().toString(); + String inputValue = this.inputData.getValue().toString(); + String[] inputValues = inputValue.split(separatorValue); + + String indexKey = "WhileLoopStringBySeparator_TSIndex"; + + Object currentIterationIndexData = null; + try { + currentIterationIndexData = currentIterationIndexProvider.getRuntimeData(indexKey); + } catch (Exception ex) { + logger.info("Failed to get currentIterationIndexData " + ex.getMessage()); + } + + int currentIterationIndexValue; + if (currentIterationIndexData != null) { + currentIterationIndexValue = Integer.parseInt(currentIterationIndexData.toString()); + } else { + logger.info("Current iteration index is null"); + currentIterationIndexValue = 0; + } + logger.info("Current iteration index value: " + currentIterationIndexValue); + + if (inputValues.length <= currentIterationIndexValue) { + logger.info("No more values to iterate the while loop."); + setErrorMessage("No more values to iterate the while loop."); + throw new Exception("No more values to iterate. All values have been consumed."); + } + + String iterationValue = inputValues[currentIterationIndexValue].trim(); + + currentIterationIndexValue++; + currentIterationIndex = new com.testsigma.sdk.RunTimeData(); + currentIterationIndex.setKey(indexKey); + currentIterationIndex.setValue(String.valueOf(currentIterationIndexValue)); + logger.info("Stored next index " + indexKey + " = " + currentIterationIndexValue); + + setSuccessMessage(String.format("Successfully returned iteration value: %s.", iterationValue)); + logger.info("Returning iteration value: " + iterationValue); + return new TestData(iterationValue); + } catch (Exception e) { + setErrorMessage("Failed to iterate the while loop due to " + e.getMessage()); + logger.warn("Failed to iterate the while loop." + ExceptionUtils.getStackTrace(e)); + throw new Exception("Failed to iterate the while loop due to " + e.getMessage()); + } + } + +} diff --git a/list_data_structure_actions/src/main/java/com/testsigma/addons/restapi/WhileLoopStringBySeparator.java b/list_data_structure_actions/src/main/java/com/testsigma/addons/restapi/WhileLoopStringBySeparator.java new file mode 100644 index 00000000..d40d1945 --- /dev/null +++ b/list_data_structure_actions/src/main/java/com/testsigma/addons/restapi/WhileLoopStringBySeparator.java @@ -0,0 +1,96 @@ +package com.testsigma.addons.restapi; + +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.RestApiAction; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.StepActionType; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.RunTimeData; +import com.testsigma.sdk.annotation.RunTimeDataProvider; +import com.testsigma.sdk.annotation.TestData; +import lombok.Data; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; + +@Data +@Action(actionText = "store the current iteration value using SEPARATOR separated values in TEST-DATA " + + "into a runtime variable RUNTIME-VARIABLE", + description = "Iterate SEPARATOR separated values in TEST-DATA and store current iteration value " + + "in RUNTIME-VARIABLE", + applicationType = ApplicationType.REST_API, + actionType = StepActionType.WHILE_LOOP) +public class WhileLoopStringBySeparator extends RestApiAction { + + @TestData(reference = "SEPARATOR") + private com.testsigma.sdk.TestData separator; + + @TestData(reference = "TEST-DATA") + private com.testsigma.sdk.TestData inputData; + + @TestData(reference = "RUNTIME-VARIABLE", isRuntimeVariable = true) + private com.testsigma.sdk.TestData runtimeVariable; + + @RunTimeData + private com.testsigma.sdk.RunTimeData currentIteration; + + @RunTimeData + private com.testsigma.sdk.RunTimeData currentIterationIndex; + + @RunTimeDataProvider + private com.testsigma.sdk.RuntimeDataProvider currentIterationIndexProvider; + + @Override + protected Result execute() throws NoSuchElementException { + try { + String runtimeVariableName = runtimeVariable.getValue().toString(); + String indexedRuntimeVariableName = runtimeVariableName + "_TSIndex"; + + String separatorValue = this.separator.getValue().toString(); + String inputValue = this.inputData.getValue().toString(); + String[] inputValues = inputValue.split(separatorValue); + + Object currentIterationIndexData = null; + try { + currentIterationIndexData = currentIterationIndexProvider.getRuntimeData(indexedRuntimeVariableName); + } catch (Exception ex) { + logger.info("Failed to get currentIterationIndexData " + ex.getMessage()); + } + + int currentIterationIndexValue; + if (currentIterationIndexData != null) { + currentIterationIndexValue = Integer.parseInt(currentIterationIndexData.toString()); + } else { + logger.info("Current iteration index is null"); + currentIterationIndexValue = 0; + } + logger.info("Current iteration index value: " + currentIterationIndexValue); + + if (inputValues.length <= currentIterationIndexValue) { + logger.info("No more values to iterate the while loop."); + setErrorMessage("No more values to iterate the while loop."); + return Result.FAILED; + } + + String iterationValue = inputValues[currentIterationIndexValue].trim(); + + currentIteration = new com.testsigma.sdk.RunTimeData(); + currentIteration.setKey(runtimeVariableName); + currentIteration.setValue(iterationValue); + logger.info("Stored " + runtimeVariableName + " = " + iterationValue); + + currentIterationIndexValue++; + currentIterationIndex = new com.testsigma.sdk.RunTimeData(); + currentIterationIndex.setKey(indexedRuntimeVariableName); + currentIterationIndex.setValue(String.valueOf(currentIterationIndexValue)); + logger.info("Stored next index" + indexedRuntimeVariableName + " = " + currentIterationIndexValue); + setSuccessMessage(String.format("Successfully stored the current iteration value into the " + + "runtime variable %s = %s .", runtimeVariableName, iterationValue)); + return Result.SUCCESS; + } catch (Exception e) { + setErrorMessage("Failed to iterate the while loop due to " + e.getMessage()); + logger.warn("Failed to iterate the while loop." + ExceptionUtils.getStackTrace(e)); + return Result.FAILED; + } + } + +}