-
Notifications
You must be signed in to change notification settings - Fork 16
[CUS-9936] Added 2 new NLP's to check if the sheet name is present in excel file #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ManojTestsigma
wants to merge
1
commit into
dev
Choose a base branch
from
CUS-9936
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
excelActions_cloud/src/main/java/com/testsigma/addons/web/VerifySheetNameAtIndex.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
ManojTestsigma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
129 changes: 129 additions & 0 deletions
129
excelActions_cloud/src/main/java/com/testsigma/addons/web/VerifySheetNameExists.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
ManojTestsigma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.