Skip to content

Commit 0634a80

Browse files
[CUS-11673] add data generator to get the value from the list string.
1 parent 5e7c165 commit 0634a80

4 files changed

Lines changed: 219 additions & 1 deletion

File tree

list_data_structure_actions/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>com.testsigma.addons</groupId>
88
<artifactId>list_data_structure_actions</artifactId>
9-
<version>1.0.0</version>
9+
<version>1.0.2</version>
1010
<packaging>jar</packaging>
1111

1212
<properties>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.testsigma.addons.datagenerator;
2+
3+
import com.testsigma.sdk.TestData;
4+
import com.testsigma.sdk.annotation.TestDataFunction;
5+
import com.testsigma.sdk.annotation.TestDataFunctionParameter;
6+
import lombok.Data;
7+
import org.apache.commons.lang3.exception.ExceptionUtils;
8+
9+
@Data
10+
@TestDataFunction(displayName = "Get count of elements in separator separated test-data",
11+
description = "Splits the test-data using the given separator and returns the count of elements")
12+
public class GetElementCountBySeparator extends com.testsigma.sdk.TestDataFunction {
13+
14+
@TestDataFunctionParameter(reference = "separator")
15+
private com.testsigma.sdk.TestData separator;
16+
17+
@TestDataFunctionParameter(reference = "test-data")
18+
private com.testsigma.sdk.TestData inputData;
19+
20+
@Override
21+
public TestData generate() throws Exception {
22+
try {
23+
String separatorValue = this.separator.getValue().toString();
24+
String inputValue = this.inputData.getValue().toString();
25+
26+
if (inputValue.isEmpty()) {
27+
logger.info("Input data is empty, returning count as 0");
28+
setSuccessMessage("Input data is empty. Element count: <b>0</b>.");
29+
return new TestData("0");
30+
}
31+
32+
String[] elements = inputValue.split(separatorValue, -1);
33+
int count = elements.length;
34+
35+
logger.info("Element count: " + count);
36+
setSuccessMessage(String.format("Successfully retrieved element count: <b>%d</b>.", count));
37+
return new TestData(String.valueOf(count));
38+
} catch (Exception e) {
39+
setErrorMessage("Failed to get element count due to " + e.getMessage());
40+
logger.warn("Failed to get element count." + ExceptionUtils.getStackTrace(e));
41+
throw new Exception("Failed to get element count due to " + e.getMessage());
42+
}
43+
}
44+
45+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.testsigma.addons.datagenerator;
2+
3+
import com.testsigma.sdk.TestData;
4+
import com.testsigma.sdk.annotation.RunTimeData;
5+
import com.testsigma.sdk.annotation.RunTimeDataProvider;
6+
import com.testsigma.sdk.annotation.TestDataFunction;
7+
import com.testsigma.sdk.annotation.TestDataFunctionParameter;
8+
import lombok.Data;
9+
import org.apache.commons.lang3.exception.ExceptionUtils;
10+
11+
@Data
12+
@TestDataFunction(displayName = "Store current iteration value using separator separated values",
13+
description = "Iterate separator separated values in test-data and return the current iteration value")
14+
public class WhileLoopStringBySeparator extends com.testsigma.sdk.TestDataFunction {
15+
16+
@TestDataFunctionParameter(reference = "separator")
17+
private com.testsigma.sdk.TestData separator;
18+
19+
@TestDataFunctionParameter(reference = "test-data")
20+
private com.testsigma.sdk.TestData inputData;
21+
22+
@RunTimeData
23+
private com.testsigma.sdk.RunTimeData currentIterationIndex;
24+
25+
@RunTimeDataProvider
26+
private com.testsigma.sdk.RuntimeDataProvider currentIterationIndexProvider;
27+
28+
@Override
29+
public TestData generate() throws Exception {
30+
try {
31+
String separatorValue = this.separator.getValue().toString();
32+
String inputValue = this.inputData.getValue().toString();
33+
String[] inputValues = inputValue.split(separatorValue);
34+
35+
String indexKey = "WhileLoopStringBySeparator_TSIndex";
36+
37+
Object currentIterationIndexData = null;
38+
try {
39+
currentIterationIndexData = currentIterationIndexProvider.getRuntimeData(indexKey);
40+
} catch (Exception ex) {
41+
logger.info("Failed to get currentIterationIndexData " + ex.getMessage());
42+
}
43+
44+
int currentIterationIndexValue;
45+
if (currentIterationIndexData != null) {
46+
currentIterationIndexValue = Integer.parseInt(currentIterationIndexData.toString());
47+
} else {
48+
logger.info("Current iteration index is null");
49+
currentIterationIndexValue = 0;
50+
}
51+
logger.info("Current iteration index value: " + currentIterationIndexValue);
52+
53+
if (inputValues.length <= currentIterationIndexValue) {
54+
logger.info("No more values to iterate the while loop.");
55+
setErrorMessage("No more values to iterate the while loop.");
56+
throw new Exception("No more values to iterate. All values have been consumed.");
57+
}
58+
59+
String iterationValue = inputValues[currentIterationIndexValue].trim();
60+
61+
currentIterationIndexValue++;
62+
currentIterationIndex = new com.testsigma.sdk.RunTimeData();
63+
currentIterationIndex.setKey(indexKey);
64+
currentIterationIndex.setValue(String.valueOf(currentIterationIndexValue));
65+
logger.info("Stored next index " + indexKey + " = " + currentIterationIndexValue);
66+
67+
setSuccessMessage(String.format("Successfully returned iteration value: <b>%s</b>.", iterationValue));
68+
logger.info("Returning iteration value: " + iterationValue);
69+
return new TestData(iterationValue);
70+
} catch (Exception e) {
71+
setErrorMessage("Failed to iterate the while loop due to " + e.getMessage());
72+
logger.warn("Failed to iterate the while loop." + ExceptionUtils.getStackTrace(e));
73+
throw new Exception("Failed to iterate the while loop due to " + e.getMessage());
74+
}
75+
}
76+
77+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.testsigma.addons.restapi;
2+
3+
import com.testsigma.sdk.ApplicationType;
4+
import com.testsigma.sdk.RestApiAction;
5+
import com.testsigma.sdk.Result;
6+
import com.testsigma.sdk.StepActionType;
7+
import com.testsigma.sdk.annotation.Action;
8+
import com.testsigma.sdk.annotation.RunTimeData;
9+
import com.testsigma.sdk.annotation.RunTimeDataProvider;
10+
import com.testsigma.sdk.annotation.TestData;
11+
import lombok.Data;
12+
import org.apache.commons.lang3.exception.ExceptionUtils;
13+
import org.openqa.selenium.NoSuchElementException;
14+
15+
@Data
16+
@Action(actionText = "store the current iteration value using SEPARATOR separated values in TEST-DATA " +
17+
"into a runtime variable RUNTIME-VARIABLE",
18+
description = "Iterate SEPARATOR separated values in TEST-DATA and store current iteration value " +
19+
"in RUNTIME-VARIABLE",
20+
applicationType = ApplicationType.REST_API,
21+
actionType = StepActionType.WHILE_LOOP)
22+
public class WhileLoopStringBySeparator extends RestApiAction {
23+
24+
@TestData(reference = "SEPARATOR")
25+
private com.testsigma.sdk.TestData separator;
26+
27+
@TestData(reference = "TEST-DATA")
28+
private com.testsigma.sdk.TestData inputData;
29+
30+
@TestData(reference = "RUNTIME-VARIABLE", isRuntimeVariable = true)
31+
private com.testsigma.sdk.TestData runtimeVariable;
32+
33+
@RunTimeData
34+
private com.testsigma.sdk.RunTimeData currentIteration;
35+
36+
@RunTimeData
37+
private com.testsigma.sdk.RunTimeData currentIterationIndex;
38+
39+
@RunTimeDataProvider
40+
private com.testsigma.sdk.RuntimeDataProvider currentIterationIndexProvider;
41+
42+
@Override
43+
protected Result execute() throws NoSuchElementException {
44+
try {
45+
String runtimeVariableName = runtimeVariable.getValue().toString();
46+
String indexedRuntimeVariableName = runtimeVariableName + "_TSIndex";
47+
48+
String separatorValue = this.separator.getValue().toString();
49+
String inputValue = this.inputData.getValue().toString();
50+
String[] inputValues = inputValue.split(separatorValue);
51+
52+
Object currentIterationIndexData = null;
53+
try {
54+
currentIterationIndexData = currentIterationIndexProvider.getRuntimeData(indexedRuntimeVariableName);
55+
} catch (Exception ex) {
56+
logger.info("Failed to get currentIterationIndexData " + ex.getMessage());
57+
}
58+
59+
int currentIterationIndexValue;
60+
if (currentIterationIndexData != null) {
61+
currentIterationIndexValue = Integer.parseInt(currentIterationIndexData.toString());
62+
} else {
63+
logger.info("Current iteration index is null");
64+
currentIterationIndexValue = 0;
65+
}
66+
logger.info("Current iteration index value: " + currentIterationIndexValue);
67+
68+
if (inputValues.length <= currentIterationIndexValue) {
69+
logger.info("No more values to iterate the while loop.");
70+
setErrorMessage("No more values to iterate the while loop.");
71+
return Result.FAILED;
72+
}
73+
74+
String iterationValue = inputValues[currentIterationIndexValue].trim();
75+
76+
currentIteration = new com.testsigma.sdk.RunTimeData();
77+
currentIteration.setKey(runtimeVariableName);
78+
currentIteration.setValue(iterationValue);
79+
logger.info("Stored " + runtimeVariableName + " = " + iterationValue);
80+
81+
currentIterationIndexValue++;
82+
currentIterationIndex = new com.testsigma.sdk.RunTimeData();
83+
currentIterationIndex.setKey(indexedRuntimeVariableName);
84+
currentIterationIndex.setValue(String.valueOf(currentIterationIndexValue));
85+
logger.info("Stored next index" + indexedRuntimeVariableName + " = " + currentIterationIndexValue);
86+
setSuccessMessage(String.format("Successfully stored the current iteration value into the " +
87+
"runtime variable <b>%s = %s </b>.", runtimeVariableName, iterationValue));
88+
return Result.SUCCESS;
89+
} catch (Exception e) {
90+
setErrorMessage("Failed to iterate the while loop due to " + e.getMessage());
91+
logger.warn("Failed to iterate the while loop." + ExceptionUtils.getStackTrace(e));
92+
return Result.FAILED;
93+
}
94+
}
95+
96+
}

0 commit comments

Comments
 (0)