|
| 1 | +package com.testsigma.addons.web; |
| 2 | + |
| 3 | +import com.testsigma.addons.web.util.Utilities; |
| 4 | +import com.testsigma.addons.web.util.UtilitiesFactory; |
| 5 | +import com.testsigma.sdk.ApplicationType; |
| 6 | +import com.testsigma.sdk.Result; |
| 7 | +import com.testsigma.sdk.WebAction; |
| 8 | +import com.testsigma.sdk.annotation.Action; |
| 9 | +import com.testsigma.sdk.annotation.RunTimeData; |
| 10 | +import com.testsigma.sdk.annotation.TestData; |
| 11 | +import lombok.Data; |
| 12 | +import lombok.EqualsAndHashCode; |
| 13 | +import org.apache.commons.lang3.exception.ExceptionUtils; |
| 14 | +import org.openqa.selenium.JavascriptExecutor; |
| 15 | +import org.openqa.selenium.NoSuchElementException; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +@EqualsAndHashCode(callSuper = true) |
| 22 | +@Data |
| 23 | +@Action(actionText = "Get latest downloaded content and store in runtime variable variable-name", |
| 24 | + description = "Retrieves the local path of the most recently downloaded file from the browser" + |
| 25 | + " (Chrome/Edge) and stores it in a runtime variable.", |
| 26 | + applicationType = ApplicationType.WEB) |
| 27 | +public class GetLatestDownloadedFileName extends WebAction { |
| 28 | + |
| 29 | + @TestData(reference = "content", allowedValues = {"file-name", "file-path", "file-name-with-extension", "file-extension"}) |
| 30 | + private com.testsigma.sdk.TestData content; |
| 31 | + |
| 32 | + @TestData(reference = "variable-name", isRuntimeVariable = true) |
| 33 | + private com.testsigma.sdk.TestData runtimeVariable; |
| 34 | + |
| 35 | + @RunTimeData |
| 36 | + private com.testsigma.sdk.RunTimeData runTimeData; |
| 37 | + |
| 38 | + @Override |
| 39 | + protected Result execute() throws NoSuchElementException { |
| 40 | + Result result = Result.SUCCESS; |
| 41 | + Utilities utilities = UtilitiesFactory.create(driver, logger); |
| 42 | + String originalWindowHandle = driver.getWindowHandle(); |
| 43 | + try { |
| 44 | + logger.info("Initiated execution"); |
| 45 | + String contentType = content.getValue().toString().trim().toLowerCase(); |
| 46 | + |
| 47 | + String storedValue; |
| 48 | + if ("file-path".equals(contentType)) { |
| 49 | + // Copy the file locally and return the temp file path |
| 50 | + File localFile = utilities.copyFileFromDownloads(); |
| 51 | + logger.info("Local file path: " + localFile.getAbsolutePath()); |
| 52 | + storedValue = localFile.getAbsolutePath(); |
| 53 | + } else { |
| 54 | + // For name/extension, read the original file name from the downloads page |
| 55 | + // without copying the entire file |
| 56 | + ((JavascriptExecutor) driver).executeScript("window.open('about:blank','_blank');"); |
| 57 | + List<String> tabs = new ArrayList<>(driver.getWindowHandles()); |
| 58 | + driver.switchTo().window(tabs.get(tabs.size() - 1)); |
| 59 | + try { |
| 60 | + if (!utilities.isFileDownloaded()) { |
| 61 | + throw new RuntimeException("File is still downloading."); |
| 62 | + } |
| 63 | + String originalPath = utilities.getDownloadedFileLocalPath(); |
| 64 | + logger.info("Original downloaded file path: " + originalPath); |
| 65 | + |
| 66 | + int lastSep = Math.max(originalPath.lastIndexOf('/'), originalPath.lastIndexOf('\\')); |
| 67 | + String originalFileName = lastSep >= 0 ? originalPath.substring(lastSep + 1) : originalPath; |
| 68 | + int dotIndex = originalFileName.lastIndexOf('.'); |
| 69 | + |
| 70 | + switch (contentType) { |
| 71 | + case "file-name": |
| 72 | + storedValue = dotIndex > 0 ? originalFileName.substring(0, dotIndex) : originalFileName; |
| 73 | + break; |
| 74 | + case "file-name-with-extension": |
| 75 | + storedValue = originalFileName; |
| 76 | + break; |
| 77 | + case "file-extension": |
| 78 | + storedValue = dotIndex > 0 ? originalFileName.substring(dotIndex + 1) : ""; |
| 79 | + break; |
| 80 | + default: |
| 81 | + storedValue = originalFileName; |
| 82 | + break; |
| 83 | + } |
| 84 | + } finally { |
| 85 | + driver.close(); |
| 86 | + driver.switchTo().window(originalWindowHandle); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + runTimeData.setKey(runtimeVariable.getValue().toString()); |
| 91 | + runTimeData.setValue(storedValue); |
| 92 | + setSuccessMessage("Successfully stored the latest downloaded " + contentType + " in the runtime variable. " |
| 93 | + + runtimeVariable.getValue().toString() + " = " + storedValue); |
| 94 | + } catch (Exception e) { |
| 95 | + logger.info("Exception Occurred: " + ExceptionUtils.getStackTrace(e)); |
| 96 | + setErrorMessage("Exception Occurred while extracting the latest downloaded file content. Exception: " |
| 97 | + + ExceptionUtils.getMessage(e)); |
| 98 | + result = Result.FAILED; |
| 99 | + } |
| 100 | + return result; |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments