Skip to content
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

feat/CR-1874-Extract row data from excel(local file path or upload section) #78

Merged
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
107 changes: 107 additions & 0 deletions excelactions_local/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testsigma.addons</groupId>
<artifactId>excelactions_local</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<testsigma.sdk.version>1.2.17_cloud</testsigma.sdk.version>
<junit.jupiter.version>5.8.0-M1</junit.jupiter.version>
<testsigma.addon.maven.plugin>1.0.0</testsigma.addon.maven.plugin>
<maven.source.plugin.version>3.2.1</maven.source.plugin.version>
<lombok.version>1.18.20</lombok.version>

</properties>

<dependencies>
<dependency>
<groupId>com.testsigma</groupId>
<artifactId>testsigma-java-sdk</artifactId>
<version>${testsigma.sdk.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.14.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>9.0.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>

</dependencies>
<build>
<finalName>excelactions_local</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven.source.plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
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.RunTimeData;
import com.testsigma.sdk.annotation.TestData;
import lombok.Data;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.NoSuchElementException;

import java.io.*;
import java.net.URL;
import java.nio.file.Paths;

@Data
@Action(actionText = "Excel: Read the entire Row of the Excel from Local filePath using the Row Index and store it in a "
+ "variable named testdata",
description = "Read the entire Row of the Excel file from given filepath or URL using the Row number and store it in a "
+ "variable named testdata",
applicationType = ApplicationType.WEB, useCustomScreenshot = false)
public class ExtractCompleteFieldValuesForGivenFilePath extends WebAction {

@TestData(reference = "filePath")
private com.testsigma.sdk.TestData filePath;
@TestData(reference = "Index")
private com.testsigma.sdk.TestData testData1;
@TestData(reference = "testdata", isRuntimeVariable = true)
private com.testsigma.sdk.TestData testData2;
@RunTimeData
private com.testsigma.sdk.RunTimeData runTimeData;

@Override
public com.testsigma.sdk.Result execute() throws NoSuchElementException {
logger.info("Initiating execution");

com.testsigma.sdk.Result result = com.testsigma.sdk.Result.SUCCESS;


String fileLocation = null;
File excelFile = null;
try {
logger.info("filePath: " + getFilePath().getValue().toString());
fileLocation = getFilePath().getValue().toString();

excelFile = null;

// Check if it is a URL or a local file path
if (fileLocation.startsWith("http://") || fileLocation.startsWith("https://")) {
excelFile = downloadFile(fileLocation);
} else {
excelFile = new File(fileLocation);
}


StringBuilder entireFieldValues = new StringBuilder();
logger.info("entireFieldValues: " + entireFieldValues);

if (!excelFile.exists() || !excelFile.isFile()) {
if (fileLocation.startsWith("http://") || fileLocation.startsWith("https://")) {
setErrorMessage("Error occurred while downloading file from URL: " + fileLocation);
} else {
setErrorMessage("The provided file path is invalid: " + fileLocation);
}
return com.testsigma.sdk.Result.FAILED;
}


try (FileInputStream inputStream = new FileInputStream(excelFile)) {
// Load the workbook and get the first sheet
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
logger.info("Case Row");
int rowIndex = Integer.parseInt(testData1.getValue().toString());
var row = sheet.getRow(rowIndex);
if (row != null) {
logger.info("Values in row " + (rowIndex + 1) + ":");

// Iterate over all cells in the row
for (Cell cell : row) {
String CellValueForRow;
// Check cell type and print the value
switch (cell.getCellType()) {
case STRING:
CellValueForRow = cell.getStringCellValue();
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
CellValueForRow = cell.getDateCellValue().toString();
} else {
CellValueForRow = Double.toString(cell.getNumericCellValue());
}
break;
case BOOLEAN:
CellValueForRow = Boolean.toString(cell.getBooleanCellValue());
break;
case FORMULA:
CellValueForRow = cell.getCellFormula();
break;
default:
CellValueForRow = "N/A";
}
entireFieldValues.append(CellValueForRow);
entireFieldValues.append(",");
}
} else {
logger.info("Row " + (rowIndex + 1) + " is empty or this is the last column with data");
}
} catch (IOException e) {
String errorMessage = "Error reading Excel file: " + ExceptionUtils.getStackTrace(e);
setErrorMessage(errorMessage);
logger.warn(errorMessage);
return com.testsigma.sdk.Result.FAILED;
}
if (entireFieldValues.length() > 0) {
entireFieldValues.deleteCharAt(entireFieldValues.length() - 1);
}

logger.info("Storing values");
runTimeData.setKey(testData2.getValue().toString());
runTimeData.setValue(entireFieldValues.toString());

setSuccessMessage("Extracted the Complete Row Values and Stored it in variable "
+ testData2.getValue().toString() + " = " + runTimeData.getValue());


} catch (Exception e) {
String errorMessage = ExceptionUtils.getStackTrace(e);
result = com.testsigma.sdk.Result.FAILED;
setErrorMessage(errorMessage);
logger.warn(errorMessage);
} finally {
if (fileLocation != null && (fileLocation.startsWith("http://") || fileLocation.startsWith("https://")) && excelFile != null) {
excelFile.delete(); //delete temp file if it was a download
}

}
return result;
}

private File downloadFile(String fileUrl) throws IOException {
URL url = new URL(fileUrl);
String fileName = Paths.get(url.getPath()).getFileName().toString();
File tempFile = File.createTempFile("downloaded-", fileName);
try (InputStream in = url.openStream();
OutputStream out = new FileOutputStream(tempFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
return tempFile;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testsigma-sdk.api.key=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyMjMyMmM2Ni04NWYzLWIyN2UtN2FiOS0zM2U2M2Q4OWM1MGIiLCJ1bmlxdWVJZCI6IjQwNzEiLCJpZGVudGl0eUFjY291bnRVVUlkIjoiMzUifQ.YnVqMLJYnowSD3bofU-GGdEJq_87WYOOCdPxZKODbwXjbJ9-WQUQQhalUQC6c05R16vsZbE09J47KXpdK9Yeiw
Loading