Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion excelActions_cloud/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.testsigma.addons</groupId>
<artifactId>excelactions_cloud</artifactId>
<version>1.0.19</version>
<version>1.0.20</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.testsigma.addons.web;

import com.testsigma.sdk.ApplicationType;
import com.testsigma.sdk.WebAction;
import com.testsigma.sdk.annotation.Action;
import com.testsigma.sdk.annotation.TestData;
import lombok.Data;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Paths;

@Data
@Action(actionText = "Verify sheet name sheet-name is present at index sheet-index in Excel file at file-path",
description = "Verifies if a sheet with the given name is present at the specified index (0-based) in the Excel file",
applicationType = ApplicationType.WEB)
public class VerifySheetNameAtIndex extends WebAction {

@TestData(reference = "sheet-name")
private com.testsigma.sdk.TestData sheetName;

@TestData(reference = "sheet-index")
private com.testsigma.sdk.TestData sheetIndex;

@TestData(reference = "file-path")
private com.testsigma.sdk.TestData filePath;

@Override
public com.testsigma.sdk.Result execute() {
logger.info("Initiating sheet name at index verification");
com.testsigma.sdk.Result result = com.testsigma.sdk.Result.SUCCESS;

try {
String excelFilePath = filePath.getValue().toString();
String expectedSheetName = sheetName.getValue().toString();
int expectedIndex = Integer.parseInt(sheetIndex.getValue().toString());

logger.info("Excel file path: " + excelFilePath);
logger.info("Expected sheet name: " + expectedSheetName);
logger.info("Expected sheet index: " + expectedIndex);

// Load the Excel file
File excelFile = getExcelFile(excelFilePath);

try (FileInputStream fis = new FileInputStream(excelFile);
XSSFWorkbook workbook = new XSSFWorkbook(fis)) {

int numberOfSheets = workbook.getNumberOfSheets();

// Check if the index is valid (0-based indexing)
if (expectedIndex < 0 || expectedIndex >= numberOfSheets) {
String errorMsg = "Sheet index " + expectedIndex + " is out of range.<br>" +
"Valid index range: 0 to " + (numberOfSheets - 1) + " (0-based index).<br>" +
"Total sheets in file: " + numberOfSheets;
logger.warn(errorMsg.replace("<br>", " | "));
setErrorMessage(errorMsg);
return com.testsigma.sdk.Result.FAILED;
}

// Get the actual sheet name at the specified index
String actualSheetName = workbook.getSheetName(expectedIndex);
logger.info("Actual sheet name at index " + expectedIndex + ": " + actualSheetName);

if (actualSheetName.equals(expectedSheetName)) {
String successMsg = "Sheet name verification successful!<br>" +
"Sheet '" + expectedSheetName + "' is present at index " + expectedIndex + ".<br>" +
"Total sheets in file: " + numberOfSheets;
logger.info(successMsg.replace("<br>", " | "));
setSuccessMessage(successMsg);
result = com.testsigma.sdk.Result.SUCCESS;
} else {
String errorMsg = "Sheet name verification failed!<br>" +
"Expected sheet name: '" + expectedSheetName + "'<br>" +
"Actual sheet name at index " + expectedIndex + ": '" + actualSheetName + "'<br>" +
"Total sheets in file: " + numberOfSheets;
logger.warn(errorMsg.replace("<br>", " | "));
setErrorMessage(errorMsg);
result = com.testsigma.sdk.Result.FAILED;
}
}

} catch (NumberFormatException e) {
String errorMessage = "Invalid number format for sheet index: " + e.getMessage();
logger.warn(errorMessage);
setErrorMessage(errorMessage);
result = com.testsigma.sdk.Result.FAILED;
} catch (Exception e) {
String errorMessage = "Error verifying sheet name at index: " + ExceptionUtils.getMessage(e);
logger.warn("Full error: " + ExceptionUtils.getStackTrace(e));
setErrorMessage(errorMessage);
result = com.testsigma.sdk.Result.FAILED;
}

return result;
}

/**
* Gets the Excel file from a local path or downloads from URL
*/
private File getExcelFile(String filePath) throws IOException {
if (filePath.startsWith("http://") || filePath.startsWith("https://")) {
logger.info("Downloading file from URL: " + filePath);
return downloadFile(filePath);
} else {
logger.info("Using local file: " + filePath);
File file = new File(filePath);
if (!file.exists()) {
throw new IOException("File not found: " + filePath);
}
return file;
}
}

/**
* Downloads a file from URL to a temporary location
*/
private File downloadFile(String fileUrl) throws IOException {
URL url = new URL(fileUrl);
String fileName = Paths.get(url.getPath()).getFileName().toString();
File tempFile = File.createTempFile("excel-sheet-index-verify-", "-" + fileName);
try (InputStream in = url.openStream();
OutputStream out = new FileOutputStream(tempFile)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
logger.info("Downloaded file to: " + tempFile.getAbsolutePath());
return tempFile;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.testsigma.addons.web;

import com.testsigma.sdk.ApplicationType;
import com.testsigma.sdk.WebAction;
import com.testsigma.sdk.annotation.Action;
import com.testsigma.sdk.annotation.TestData;
import lombok.Data;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Data
@Action(actionText = "Verify sheet name sheet-name exists in Excel file at file-path",
description = "Verifies if a sheet with the given name exists in the specified Excel file",
applicationType = ApplicationType.WEB)
public class VerifySheetNameExists extends WebAction {

@TestData(reference = "sheet-name")
private com.testsigma.sdk.TestData sheetName;

@TestData(reference = "file-path")
private com.testsigma.sdk.TestData filePath;

@Override
public com.testsigma.sdk.Result execute() {
logger.info("Initiating sheet name verification");
com.testsigma.sdk.Result result = com.testsigma.sdk.Result.SUCCESS;

try {
String excelFilePath = filePath.getValue().toString();
String expectedSheetName = sheetName.getValue().toString();

logger.info("Excel file path: " + excelFilePath);
logger.info("Expected sheet name: " + expectedSheetName);

// Load the Excel file
File excelFile = getExcelFile(excelFilePath);

try (FileInputStream fis = new FileInputStream(excelFile);
XSSFWorkbook workbook = new XSSFWorkbook(fis)) {

int numberOfSheets = workbook.getNumberOfSheets();
List<String> sheetNames = new ArrayList<>();
boolean sheetFound = false;

// Iterate through all sheets to find the matching sheet name
for (int i = 0; i < numberOfSheets; i++) {
String currentSheetName = workbook.getSheetName(i);
sheetNames.add(currentSheetName);
if (currentSheetName.equals(expectedSheetName)) {
sheetFound = true;
logger.info("Sheet '" + expectedSheetName + "' found at index " + i);
}
}

if (sheetFound) {
String successMsg = "Sheet name '" + expectedSheetName + "' exists in the Excel file.<br>" +
"Total sheets in file: " + numberOfSheets + "<br>" +
"All sheet names: " + sheetNames;
logger.info(successMsg.replace("<br>", " | "));
setSuccessMessage(successMsg);
result = com.testsigma.sdk.Result.SUCCESS;
} else {
String errorMsg = "Sheet name '" + expectedSheetName + "' does NOT exist in the Excel file.<br>" +
"Total sheets in file: " + numberOfSheets + "<br>" +
"Available sheet names: " + sheetNames;
logger.warn(errorMsg.replace("<br>", " | "));
setErrorMessage(errorMsg);
result = com.testsigma.sdk.Result.FAILED;
}
}

} catch (Exception e) {
String errorMessage = "Error verifying sheet name: " + ExceptionUtils.getMessage(e);
logger.warn("Full error: " + ExceptionUtils.getStackTrace(e));
setErrorMessage(errorMessage);
result = com.testsigma.sdk.Result.FAILED;
}

return result;
}

/**
* Gets the Excel file from a local path or downloads from URL
*/
private File getExcelFile(String filePath) throws IOException {
if (filePath.startsWith("http://") || filePath.startsWith("https://")) {
logger.info("Downloading file from URL: " + filePath);
return downloadFile(filePath);
} else {
logger.info("Using local file: " + filePath);
File file = new File(filePath);
if (!file.exists()) {
throw new IOException("File not found: " + filePath);
}
return file;
}
}

/**
* Downloads a file from URL to a temporary location
*/
private File downloadFile(String fileUrl) throws IOException {
URL url = new URL(fileUrl);
String fileName = Paths.get(url.getPath()).getFileName().toString();
File tempFile = File.createTempFile("excel-sheet-verify-", "-" + fileName);
try (InputStream in = url.openStream();
OutputStream out = new FileOutputStream(tempFile)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
logger.info("Downloaded file to: " + tempFile.getAbsolutePath());
return tempFile;
}
}