diff --git a/unzipfileandupload/pom.xml b/unzipfileandupload/pom.xml new file mode 100644 index 00000000..87f4a9ea --- /dev/null +++ b/unzipfileandupload/pom.xml @@ -0,0 +1,102 @@ + + + 4.0.0 + com.testsigma.addons + unzipfileandupload + 1.0.2 + jar + + + UTF-8 + 11 + 11 + 1.2.8_cloud + 5.8.0-M1 + 1.0.0 + 3.2.1 + 1.18.20 + + + + + + com.testsigma + testsigma-java-sdk + ${testsigma.sdk.version} + + + org.projectlombok + lombok + ${lombok.version} + true + + + org.junit.jupiter + junit-jupiter-api + ${junit.jupiter.version} + test + + + org.testng + testng + 6.14.3 + + + + org.seleniumhq.selenium + selenium-java + 4.14.1 + + + + io.appium + java-client + 9.0.0 + + + com.fasterxml.jackson.core + jackson-annotations + 2.13.0 + + + org.apache.commons + commons-lang3 + 3.12.0 + + + + + unzipfileandupload + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + + org.apache.maven.plugins + maven-source-plugin + ${maven.source.plugin.version} + + + attach-sources + + jar + + + + + + + \ No newline at end of file diff --git a/unzipfileandupload/src/main/java/com/testsigma/addons/web/UnzipCSVGZFile.java b/unzipfileandupload/src/main/java/com/testsigma/addons/web/UnzipCSVGZFile.java new file mode 100644 index 00000000..4316b612 --- /dev/null +++ b/unzipfileandupload/src/main/java/com/testsigma/addons/web/UnzipCSVGZFile.java @@ -0,0 +1,76 @@ +package com.testsigma.addons.web; + +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import com.testsigma.sdk.annotation.RunTimeData; + +import lombok.Data; + +import org.apache.commons.lang3.exception.ExceptionUtils; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.GZIPInputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +@Data +@Action(actionText = "Unzip the .CSV.GZ file absolutefilepath to destination destfilepath and store the filepath into a variable testdata", + description = "Unzip the .CSV.GZ file and store the extracted file path", + applicationType = ApplicationType.WEB, + useCustomScreenshot = false) +public class UnzipCSVGZFile extends WebAction { + + @TestData(reference = "absolutefilepath") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "destfilepath") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "testdata" , isRuntimeVariable = true) + private com.testsigma.sdk.TestData testData3; + + @RunTimeData + private com.testsigma.sdk.RunTimeData runTimeData; + + @Override + public com.testsigma.sdk.Result execute() { + //Your Awesome code starts here + logger.info("Initiating execution"); + com.testsigma.sdk.Result result = com.testsigma.sdk.Result.SUCCESS; + try { + String zipFilePath = testData1.getValue().toString(); + String destDir = testData2.getValue().toString(); + String fileNameUpload = "output" + System.currentTimeMillis() + ".csv"; + String filePath = destDir + File.separator + fileNameUpload; + unzip(zipFilePath, filePath); + logger.info("Storing the filepath in runtime variable"); + runTimeData.setValue(filePath); + runTimeData.setKey(testData3.getValue().toString()); + logger.info("Stored successfully"); + setSuccessMessage("File was unzipped and stored successfully in : " + testData3.getValue().toString() + " and the filepath is :" + filePath); + } catch (Exception e) { + String errorMessage = ExceptionUtils.getStackTrace(e); + result = com.testsigma.sdk.Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } + private void unzip(String zipFilePath, String destFilepath) throws IOException { + logger.info("Extracting the file"); + FileInputStream fis = new FileInputStream(zipFilePath); + GZIPInputStream gis = new GZIPInputStream(fis); + FileOutputStream fos = new FileOutputStream(destFilepath); + byte[] buffer = new byte[1024]; + int len; + while ((len = gis.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + fos.close(); + gis.close(); + fis.close(); + logger.info("Successfully extracted to location: " + destFilepath); + } +} \ No newline at end of file diff --git a/unzipfileandupload/src/main/java/com/testsigma/addons/web/Uploadunzip.java b/unzipfileandupload/src/main/java/com/testsigma/addons/web/Uploadunzip.java new file mode 100644 index 00000000..54e58747 --- /dev/null +++ b/unzipfileandupload/src/main/java/com/testsigma/addons/web/Uploadunzip.java @@ -0,0 +1,97 @@ +package com.testsigma.addons.web; + +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import com.testsigma.sdk.annotation.Element; +import lombok.Data; + +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebElement; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +@Data +@Action(actionText = "Unzip the zipfile absolutefilepath to destination destfilepath and upload into a element element-locator", +description = "Unzip the file and upload into a element", +applicationType = ApplicationType.WEB, +useCustomScreenshot = false) +public class Uploadunzip extends WebAction { + + @TestData(reference = "absolutefilepath") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "destfilepath") + private com.testsigma.sdk.TestData testData2; + @Element(reference = "element-locator") + private com.testsigma.sdk.Element element; + + + @Override + public com.testsigma.sdk.Result execute() throws NoSuchElementException { + //Your Awesome code starts here + logger.info("Initiating execution"); + com.testsigma.sdk.Result result = com.testsigma.sdk.Result.SUCCESS; + try { + String zipFilePath = testData1.getValue().toString(); + String destDir = testData2.getValue().toString(); + + WebElement webElement = element.getElement(); + String fileNameupload = unzip(zipFilePath, destDir); + String filePath = destDir + File.separator + fileNameupload; + Thread.sleep(1000); + webElement.sendKeys(filePath); + Thread.sleep(1000); + }catch (Exception e) { + String errorMessage = ExceptionUtils.getStackTrace(e); + result = com.testsigma.sdk.Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + setSuccessMessage("File was unzipped and uploaded successfully on the specific element location"); + return result; + } + private static String unzip(String zipFilePath, String destDir) throws InterruptedException { + String fileName = null; + Thread.sleep(1000); + File dir = new File(destDir); + // create output directory if it doesn't exist + if (!dir.exists()) dir.mkdirs(); + FileInputStream fis; + // buffer for read and write data to file + byte[] buffer = new byte[1024]; + try { + fis = new FileInputStream(zipFilePath); + ZipInputStream zis = new ZipInputStream(fis); + ZipEntry ze = zis.getNextEntry(); + while (ze != null) { + fileName = ze.getName(); + File newFile = new File(destDir + File.separator + fileName); + // create directories for sub directories in zip + new File(newFile.getParent()).mkdirs(); + FileOutputStream fos = new FileOutputStream(newFile); + Thread.sleep(1000); + int len; + while ((len = zis.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + fos.close(); + zis.closeEntry(); + ze = zis.getNextEntry(); + Thread.sleep(1000); + } + zis.closeEntry(); + zis.close(); + fis.close(); + Thread.sleep(1000); + } catch (IOException e) { + e.printStackTrace(); + } + return fileName; + } +} \ No newline at end of file diff --git a/unzipfileandupload/src/main/java/com/testsigma/addons/web/Uploadunziplatest.java b/unzipfileandupload/src/main/java/com/testsigma/addons/web/Uploadunziplatest.java new file mode 100644 index 00000000..f2968855 --- /dev/null +++ b/unzipfileandupload/src/main/java/com/testsigma/addons/web/Uploadunziplatest.java @@ -0,0 +1,119 @@ +package com.testsigma.addons.web; + +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import com.testsigma.sdk.annotation.Element; +import lombok.Data; + +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebElement; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +@Data +@Action(actionText = "Unzip the latest zipfile filedirectory to destination destfilepath and upload into a element element-locator", +description = "Unzip the file and upload into a element", +applicationType = ApplicationType.WEB, +useCustomScreenshot = false) +public class Uploadunziplatest extends WebAction { + + @TestData(reference = "filedirectory") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "destfilepath") + private com.testsigma.sdk.TestData testData2; + @Element(reference = "element-locator") + private com.testsigma.sdk.Element element; + + + @Override + public com.testsigma.sdk.Result execute() throws NoSuchElementException { + //Your Awesome code starts here + logger.info("Initiating execution"); + com.testsigma.sdk.Result result = com.testsigma.sdk.Result.SUCCESS; + try { + File zipFilePath = getLastModified(testData1.getValue().toString()); + String destDir = testData2.getValue().toString(); + + WebElement webElement = element.getElement(); + String fileNameupload = unzip(zipFilePath, destDir); + String filePath = destDir + File.separator + fileNameupload; + Thread.sleep(1000); + webElement.sendKeys(filePath); + Thread.sleep(1000); + }catch (Exception e) { + String errorMessage = ExceptionUtils.getStackTrace(e); + result = com.testsigma.sdk.Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + setSuccessMessage("File was unzipped and uploaded successfully on the specific element location"); + return result; + } + private static String unzip(File zipFilePath, String destDir) throws InterruptedException { + String fileName = null; + Thread.sleep(1000); + File dir = new File(destDir); + // create output directory if it doesn't exist + if (!dir.exists()) dir.mkdirs(); + FileInputStream fis; + // buffer for read and write data to file + byte[] buffer = new byte[1024]; + try { + fis = new FileInputStream(zipFilePath); + ZipInputStream zis = new ZipInputStream(fis); + ZipEntry ze = zis.getNextEntry(); + while (ze != null) { + fileName = ze.getName(); + File newFile = new File(destDir + File.separator + fileName); + // create directories for sub directories in zip + new File(newFile.getParent()).mkdirs(); + FileOutputStream fos = new FileOutputStream(newFile); + Thread.sleep(1000); + int len; + while ((len = zis.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + fos.close(); + zis.closeEntry(); + ze = zis.getNextEntry(); + Thread.sleep(1000); + } + zis.closeEntry(); + zis.close(); + fis.close(); + Thread.sleep(1000); + } catch (IOException e) { + e.printStackTrace(); + } + return fileName; + } + + public static File getLastModified(String directoryFilePath) + { + File directory = new File(directoryFilePath); + File[] files = directory.listFiles(File::isFile); + long lastModifiedTime = Long.MIN_VALUE; + File chosenFile = null; + + if (files != null) + { + for (File file : files) + { + if (file.lastModified() > lastModifiedTime) + { + chosenFile = file; + lastModifiedTime = file.lastModified(); + } + } + } + + return chosenFile; + } +} \ No newline at end of file diff --git a/unzipfileandupload/src/main/java/com/testsigma/addons/web/unzipfile.java b/unzipfileandupload/src/main/java/com/testsigma/addons/web/unzipfile.java new file mode 100644 index 00000000..3c225437 --- /dev/null +++ b/unzipfileandupload/src/main/java/com/testsigma/addons/web/unzipfile.java @@ -0,0 +1,98 @@ +package com.testsigma.addons.web; + +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import com.testsigma.sdk.annotation.RunTimeData; + +import lombok.Data; + +import org.apache.commons.lang3.exception.ExceptionUtils; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +@Data +@Action(actionText = "Unzip the zipfile absolutefilepath to destination destfilepath and store the filepath into a variable testdata", +description = "Unzip the file and upload into a element", +applicationType = ApplicationType.WEB, +useCustomScreenshot = false) +public class unzipfile extends WebAction { + + @TestData(reference = "absolutefilepath") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "destfilepath") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "testdata" , isRuntimeVariable = true) + private com.testsigma.sdk.TestData testData3; + + @RunTimeData + private com.testsigma.sdk.RunTimeData runTimeData; + + + @Override + public com.testsigma.sdk.Result execute() { + //Your Awesome code starts here + logger.info("Initiating execution"); + com.testsigma.sdk.Result result = com.testsigma.sdk.Result.SUCCESS; + try { + String zipFilePath = testData1.getValue().toString(); + String destDir = testData2.getValue().toString(); + + String fileNameupload = unzip(zipFilePath, destDir); + String filePath = destDir + File.separator + fileNameupload; + runTimeData.setValue(filePath); + runTimeData.setKey(testData3.getValue().toString()); + Thread.sleep(1000); + setSuccessMessage("File was unzipped and stored successfully in : " + testData3.getValue().toString() + " and the filepath is :" + filePath); + }catch (Exception e) { + String errorMessage = ExceptionUtils.getStackTrace(e); + result = com.testsigma.sdk.Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } + private static String unzip(String zipFilePath, String destDir) throws InterruptedException { + String fileName = null; + Thread.sleep(1000); + File dir = new File(destDir); + // create output directory if it doesn't exist + if (!dir.exists()) dir.mkdirs(); + FileInputStream fis; + // buffer for read and write data to file + byte[] buffer = new byte[1024]; + try { + fis = new FileInputStream(zipFilePath); + ZipInputStream zis = new ZipInputStream(fis); + ZipEntry ze = zis.getNextEntry(); + while (ze != null) { + fileName = ze.getName(); + File newFile = new File(destDir + File.separator + fileName); + // create directories for sub directories in zip + new File(newFile.getParent()).mkdirs(); + FileOutputStream fos = new FileOutputStream(newFile); + Thread.sleep(1000); + int len; + while ((len = zis.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + fos.close(); + zis.closeEntry(); + ze = zis.getNextEntry(); + Thread.sleep(1000); + } + zis.closeEntry(); + zis.close(); + fis.close(); + Thread.sleep(1000); + } catch (IOException e) { + e.printStackTrace(); + } + return fileName; + } +} \ No newline at end of file diff --git a/unzipfileandupload/src/main/java/com/testsigma/addons/web/unzipfilelatest.java b/unzipfileandupload/src/main/java/com/testsigma/addons/web/unzipfilelatest.java new file mode 100644 index 00000000..5956d94e --- /dev/null +++ b/unzipfileandupload/src/main/java/com/testsigma/addons/web/unzipfilelatest.java @@ -0,0 +1,123 @@ +package com.testsigma.addons.web; + +import com.testsigma.sdk.WebAction; +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import com.testsigma.sdk.annotation.Element; +import com.testsigma.sdk.annotation.RunTimeData; + +import lombok.Data; + +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebElement; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +@Data +@Action(actionText = "Unzip the latest zipfile filedirectory to destination destfilepath and store the filepath into a variable testdata", +description = "Unzip the file and upload into a element", +applicationType = ApplicationType.WEB, +useCustomScreenshot = false) +public class unzipfilelatest extends WebAction { + + @TestData(reference = "filedirectory") + private com.testsigma.sdk.TestData testData1; + @TestData(reference = "destfilepath") + private com.testsigma.sdk.TestData testData2; + @TestData(reference = "testdata" , isRuntimeVariable = true) + private com.testsigma.sdk.TestData testData3; + + @RunTimeData + private com.testsigma.sdk.RunTimeData runTimeData; + + + @Override + public com.testsigma.sdk.Result execute() throws NoSuchElementException { + //Your Awesome code starts here + logger.info("Initiating execution"); + com.testsigma.sdk.Result result = com.testsigma.sdk.Result.SUCCESS; + try { + File zipFilePath = getLastModified(testData1.getValue().toString()); + String destDir = testData2.getValue().toString(); + + String fileNameupload = unzip(zipFilePath, destDir); + String filePath = destDir + File.separator + fileNameupload; + runTimeData.setValue(filePath); + runTimeData.setKey(testData3.getValue().toString()); + Thread.sleep(1000); + setSuccessMessage("File was unzipped and stored successfully in : " + testData3.getValue().toString() + " and the filepath is :" + filePath); + }catch (Exception e) { + String errorMessage = ExceptionUtils.getStackTrace(e); + result = com.testsigma.sdk.Result.FAILED; + setErrorMessage(errorMessage); + logger.warn(errorMessage); + } + return result; + } + private static String unzip(File zipFilePath, String destDir) throws InterruptedException { + String fileName = null; + Thread.sleep(1000); + File dir = new File(destDir); + // create output directory if it doesn't exist + if (!dir.exists()) dir.mkdirs(); + FileInputStream fis; + // buffer for read and write data to file + byte[] buffer = new byte[1024]; + try { + fis = new FileInputStream(zipFilePath); + ZipInputStream zis = new ZipInputStream(fis); + ZipEntry ze = zis.getNextEntry(); + while (ze != null) { + fileName = ze.getName(); + File newFile = new File(destDir + File.separator + fileName); + // create directories for sub directories in zip + new File(newFile.getParent()).mkdirs(); + FileOutputStream fos = new FileOutputStream(newFile); + Thread.sleep(1000); + int len; + while ((len = zis.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + fos.close(); + zis.closeEntry(); + ze = zis.getNextEntry(); + Thread.sleep(1000); + } + zis.closeEntry(); + zis.close(); + fis.close(); + Thread.sleep(1000); + } catch (IOException e) { + e.printStackTrace(); + } + return fileName; + } + + public static File getLastModified(String directoryFilePath) + { + File directory = new File(directoryFilePath); + File[] files = directory.listFiles(File::isFile); + long lastModifiedTime = Long.MIN_VALUE; + File chosenFile = null; + + if (files != null) + { + for (File file : files) + { + if (file.lastModified() > lastModifiedTime) + { + chosenFile = file; + lastModifiedTime = file.lastModified(); + } + } + } + + return chosenFile; + } +} \ No newline at end of file