|
| 1 | +package com.testsigma.addons.android; |
| 2 | + |
| 3 | +import com.testsigma.sdk.AndroidAction; |
| 4 | +import com.testsigma.sdk.ApplicationType; |
| 5 | +import com.testsigma.sdk.annotation.Action; |
| 6 | +import com.testsigma.sdk.annotation.TestData; |
| 7 | +import com.testsigma.sdk.annotation.RunTimeData; |
| 8 | +import lombok.Data; |
| 9 | +import org.apache.commons.lang3.exception.ExceptionUtils; |
| 10 | +import org.openqa.selenium.NoSuchElementException; |
| 11 | +import java.time.LocalDateTime; |
| 12 | +import java.time.LocalTime; |
| 13 | +import java.time.format.DateTimeFormatter; |
| 14 | +import java.time.format.DateTimeParseException; |
| 15 | + |
| 16 | +@Data |
| 17 | +@Action(actionText = "Subtract minute minutes to the input-datetime with input-datetime-format, convert to output-datetime-format format, and store it in a runtime variable variable-name", |
| 18 | + description = "Subtracts specified minutes to a given input datetime, converts the resulting datetime to a specified date-time format, and stores it in a runtime variable", |
| 19 | + applicationType = ApplicationType.ANDROID, |
| 20 | + useCustomScreenshot = false) |
| 21 | +public class SubtractMinutesToDateTime extends AndroidAction { |
| 22 | + |
| 23 | + @TestData(reference = "input-datetime") |
| 24 | + private com.testsigma.sdk.TestData dateTime; |
| 25 | + @TestData(reference = "input-datetime-format") |
| 26 | + private com.testsigma.sdk.TestData inputFormat; |
| 27 | + @TestData(reference = "output-datetime-format") |
| 28 | + private com.testsigma.sdk.TestData outputFormat; |
| 29 | + @TestData(reference = "minute") |
| 30 | + private com.testsigma.sdk.TestData minutesToAdd; |
| 31 | + |
| 32 | + @TestData(reference = "variable-name", isRuntimeVariable = true) |
| 33 | + private com.testsigma.sdk.TestData variableName; |
| 34 | + |
| 35 | + @RunTimeData |
| 36 | + private com.testsigma.sdk.RunTimeData runTimeData; |
| 37 | + |
| 38 | + @Override |
| 39 | + public com.testsigma.sdk.Result execute() throws NoSuchElementException { |
| 40 | + logger.info("Initiating execution"); |
| 41 | + com.testsigma.sdk.Result result = com.testsigma.sdk.Result.SUCCESS; |
| 42 | + String dateTimeString = dateTime.getValue().toString(); |
| 43 | + String inputFormatString = inputFormat.getValue().toString(); |
| 44 | + String outputFormatString = outputFormat.getValue().toString(); |
| 45 | + String minutesToAddString = minutesToAdd.getValue().toString(); |
| 46 | + String variableNameString = variableName.getValue().toString(); |
| 47 | + |
| 48 | + try { |
| 49 | + int minutes = Integer.parseInt(minutesToAddString); |
| 50 | + |
| 51 | + if (minutes < 0) { |
| 52 | + result = com.testsigma.sdk.Result.FAILED; |
| 53 | + setErrorMessage("The 'minute' value must be a non-negative integer."); |
| 54 | + logger.warn("Invalid input: 'minute' value is negative."); |
| 55 | + return result; |
| 56 | + } |
| 57 | + |
| 58 | + String newDateTime = subtractMinutesToDateTime(dateTimeString, inputFormatString, outputFormatString, minutes); |
| 59 | + |
| 60 | + logger.info("New date-time value: " + newDateTime); |
| 61 | + |
| 62 | + if (newDateTime != null) { |
| 63 | + runTimeData.setValue(newDateTime); |
| 64 | + runTimeData.setKey(variableNameString); |
| 65 | + logger.info("Final value stored in variable '" + variableNameString + "' with value '" + newDateTime + "'"); |
| 66 | + setSuccessMessage(String.format("Subtracted %s minutes from %s and stored the output in '%s' format. %s : %s", |
| 67 | + minutes, dateTimeString, outputFormatString, variableNameString, newDateTime)); |
| 68 | + } else { |
| 69 | + result = com.testsigma.sdk.Result.FAILED; |
| 70 | + setErrorMessage("Failed to subtract minutes. Please check the input datetime, input datetime format, and output datetime format."); |
| 71 | + } |
| 72 | + |
| 73 | + } catch (NumberFormatException e) { |
| 74 | + result = com.testsigma.sdk.Result.FAILED; |
| 75 | + setErrorMessage("Invalid number format for 'minutes': " + minutesToAddString); |
| 76 | + logger.warn("NumberFormatException: " + e); |
| 77 | + |
| 78 | + } catch (Exception e) { |
| 79 | + result = com.testsigma.sdk.Result.FAILED; |
| 80 | + setErrorMessage("An unexpected error occurred: " + ExceptionUtils.getMessage(e)); |
| 81 | + logger.warn("Exception during execution: " + ExceptionUtils.getStackTrace(e)); |
| 82 | + } |
| 83 | + return result; |
| 84 | + } |
| 85 | + |
| 86 | + public String subtractMinutesToDateTime(String dateTimeString, String inputFormatString, String outputFormatString, int minutesToSubtract) { |
| 87 | + try { |
| 88 | + DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(inputFormatString); |
| 89 | + |
| 90 | + if (inputFormatString.contains("HH") || inputFormatString.contains("mm") || inputFormatString.contains("ss")) { |
| 91 | + DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(outputFormatString); |
| 92 | + LocalTime time = LocalTime.parse(dateTimeString, inputFormatter); |
| 93 | + LocalTime newTime = time.minusMinutes(minutesToSubtract); |
| 94 | + logger.info("Successfully subtracted minutes from the input time"); |
| 95 | + |
| 96 | + return newTime.format(outputFormatter); |
| 97 | + |
| 98 | + } else { |
| 99 | + |
| 100 | + DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(outputFormatString); |
| 101 | + LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, inputFormatter); |
| 102 | + |
| 103 | + LocalDateTime newDateTime = dateTime.minusMinutes(minutesToSubtract); |
| 104 | + logger.info("Successfully subtracted minutes from the input datetime"); |
| 105 | + |
| 106 | + return newDateTime.format(outputFormatter); |
| 107 | + } |
| 108 | + |
| 109 | + } catch (DateTimeParseException e) { |
| 110 | + String errorMessage = "Invalid date/time format. Please use the format: " + inputFormatString + ". Error: " + e.getMessage(); |
| 111 | + logger.warn(errorMessage); |
| 112 | + setErrorMessage(errorMessage); |
| 113 | + return null; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments