|
| 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