Skip to content

Commit b57a63b

Browse files
[CUS-12130] updated add-on to store filename/path/extension
1 parent 5e7c165 commit b57a63b

3 files changed

Lines changed: 191 additions & 1 deletion

File tree

get_latest_downloaded_file_path/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>get_latest_downloaded_file_path</artifactId>
9-
<version>1.0.2</version>
9+
<version>1.0.3</version>
1010
<packaging>jar</packaging>
1111

1212
<properties>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.Result;
6+
import com.testsigma.sdk.WebAction;
7+
import com.testsigma.sdk.ApplicationType;
8+
import com.testsigma.sdk.annotation.Action;
9+
import com.testsigma.sdk.annotation.TestData;
10+
import lombok.Data;
11+
import org.apache.commons.lang3.exception.ExceptionUtils;
12+
import org.openqa.selenium.NoSuchElementException;
13+
14+
import java.io.File;
15+
import java.nio.file.Files;
16+
17+
@Data
18+
@Action(actionText = "Verify if the latest downloaded file type is expected-file-type",
19+
description = "Verifies that the most recently downloaded file has the expected file type/extension",
20+
applicationType = ApplicationType.WEB,
21+
useCustomScreenshot = false)
22+
public class VerifyIfTheLatestFileType extends WebAction {
23+
24+
@TestData(reference = "expected-file-type", allowedValues = {"pdf", "png", "jpg", "jpeg", "csv", "zip", "xlsx", "docx", "txt"})
25+
private com.testsigma.sdk.TestData testData;
26+
27+
@Override
28+
public Result execute() throws NoSuchElementException {
29+
logger.info("Initiating execution");
30+
Result result = Result.SUCCESS;
31+
32+
String currentWindowHandle = driver.getWindowHandle();
33+
try {
34+
String expectedFileType = testData.getValue().toString().trim().toLowerCase().replaceAll("^\\.", "");
35+
logger.info("Expected file type: " + expectedFileType);
36+
37+
String expectedMimeType;
38+
switch (expectedFileType) {
39+
case "pdf": expectedMimeType = "application/pdf"; break;
40+
case "png": expectedMimeType = "image/png"; break;
41+
case "jpg":
42+
case "jpeg": expectedMimeType = "image/jpeg"; break;
43+
case "csv": expectedMimeType = "text/csv"; break;
44+
case "zip": expectedMimeType = "application/zip"; break;
45+
case "xlsx": expectedMimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break;
46+
case "docx": expectedMimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; break;
47+
case "txt": expectedMimeType = "text/plain"; break;
48+
default:
49+
setErrorMessage("Unsupported file type: '" + expectedFileType + "'. Supported types: pdf, png, jpg, jpeg, csv, zip, xlsx, docx, txt");
50+
return Result.FAILED;
51+
}
52+
logger.info("Expected MIME type: " + expectedMimeType);
53+
54+
Utilities utilities = UtilitiesFactory.create(driver, logger);
55+
File localFile = utilities.copyFileFromDownloads();
56+
logger.info("Local temp file: " + localFile.getAbsolutePath());
57+
58+
String actualFileType;
59+
try {
60+
actualFileType = Files.probeContentType(localFile.toPath());
61+
} finally {
62+
localFile.delete();
63+
}
64+
65+
if (actualFileType == null) {
66+
setErrorMessage("Could not determine file type of downloaded file: " + localFile.getName());
67+
return Result.FAILED;
68+
}
69+
logger.info("Actual file type: " + actualFileType);
70+
71+
if (actualFileType.equals(expectedMimeType)) {
72+
setSuccessMessage("Verified: the latest downloaded file type is '" + expectedFileType + "' as expected.");
73+
} else {
74+
setErrorMessage("File type mismatch: expected '" + expectedFileType + "' but found '" + actualFileType + "'.");
75+
result = Result.FAILED;
76+
}
77+
} catch (Exception e) {
78+
logger.info("Error while verifying file type: " + ExceptionUtils.getStackTrace(e));
79+
setErrorMessage("Error while verifying file type: " + e.getMessage());
80+
result = Result.FAILED;
81+
} finally {
82+
driver.switchTo().window(currentWindowHandle);
83+
}
84+
85+
return result;
86+
}
87+
}

0 commit comments

Comments
 (0)