diff --git a/ftp_file_upload/pom.xml b/ftp_file_upload/pom.xml new file mode 100644 index 0000000..f1104a9 --- /dev/null +++ b/ftp_file_upload/pom.xml @@ -0,0 +1,103 @@ + + + 4.0.0 + com.testsigma.addons + ftp_file_upload + 1.0.1 + jar + + + UTF-8 + 11 + 11 + 1.2.13_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 + + + + commons-net + commons-net + 3.11.1 + + + + + ftp_file_upload + + + 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 + + + + + + + diff --git a/ftp_file_upload/src/main/java/com/testsigma/addons/web/FTPUploadFile.java b/ftp_file_upload/src/main/java/com/testsigma/addons/web/FTPUploadFile.java new file mode 100644 index 0000000..4b1d686 --- /dev/null +++ b/ftp_file_upload/src/main/java/com/testsigma/addons/web/FTPUploadFile.java @@ -0,0 +1,164 @@ +package com.testsigma.addons.web; + +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.Result; +import com.testsigma.sdk.WindowsAction; +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.net.ftp.FTP; +import org.apache.commons.net.ftp.FTPClient; +import org.openqa.selenium.NoSuchElementException; + +import java.io.*; +import java.net.URL; +import java.nio.file.Paths; + +@Data +@Action(actionText = "FTP: Connect to FTP server and upload a file. FTP server details: Host: Host-Name, Port: Port-No, UserName: User-Name, Password: User-Password, Upload from Local-File-Path to Remote-Directory(ex: /Users/username/Downloads) with file name Remote-File-Name. Uploaded file path will be stored in runtime variable: variable-name", + description = "Uploads a file from a local file or URL to a remote server using FTP, and stores the uploaded file path in a runtime variable.", + applicationType = ApplicationType.WEB, + useCustomScreenshot = false) +public class FTPUploadFile extends WindowsAction { + + @TestData(reference = "Host-Name") + private com.testsigma.sdk.TestData hostName; + + @TestData(reference = "Port-No") + private com.testsigma.sdk.TestData portNo; + + @TestData(reference = "User-Name") + private com.testsigma.sdk.TestData userName; + + @TestData(reference = "User-Password") + private com.testsigma.sdk.TestData userPassword; + + @TestData(reference = "Local-File-Path") + private com.testsigma.sdk.TestData localFilePath; // Local file path or URL + + @TestData(reference = "Remote-Directory") + private com.testsigma.sdk.TestData remoteDirectory; // Remote directory to upload to + + @TestData(reference = "Remote-File-Name") + private com.testsigma.sdk.TestData remoteFileName; // Remote file name after upload + + @TestData(reference = "variable-name", isRuntimeVariable = true) + private com.testsigma.sdk.TestData uploadedFilePath; // Runtime variable for storing uploaded file path + + @RunTimeData + private com.testsigma.sdk.RunTimeData runTimeData; + + @Override + public com.testsigma.sdk.Result execute() throws NoSuchElementException { + logger.info("Initiating execution for FTP file upload"); + + String user = userName.getValue().toString(); + String host = hostName.getValue().toString(); + String port = portNo.getValue().toString(); + String password = userPassword.getValue().toString(); + String localFile = localFilePath.getValue().toString(); + String remoteDir = remoteDirectory.getValue().toString(); + String remoteFile = remoteFileName.getValue().toString(); + + FTPClient ftpClient = new FTPClient(); + File localFileToUpload = null; + try { + // Determine if the localFile is a URL or a local path + if (localFile.startsWith("http://") || localFile.startsWith("https://")) { + localFileToUpload = downloadFile(localFile); + } else { + localFileToUpload = new File(localFile); + } + + + // Verify the local file exists + if (!localFileToUpload.exists()) { + setErrorMessage("Local file not found: " + localFile); + return Result.FAILED; + } + + // Append extension if remote file name has no extension + if (!remoteFile.contains(".")) { + String localFileName = localFileToUpload.getName(); + int dotIndex = localFileName.lastIndexOf('.'); + if (dotIndex > 0) { + String extension = localFileName.substring(dotIndex); + remoteFile += extension; + logger.info("Remote file name updated to include extension: " + remoteFile); + } else { + setErrorMessage("Local file does not have an extension: " + localFileName); + return Result.FAILED; + } + } + + ftpClient.connect(host, Integer.parseInt(port)); + boolean loginSuccess = ftpClient.login(user, password); + + if (!loginSuccess) { + setErrorMessage("Failed to login to FTP server. Check credentials."); + return Result.FAILED; + } + + ftpClient.enterLocalPassiveMode(); + ftpClient.setFileType(FTP.BINARY_FILE_TYPE); + + boolean changedDir = ftpClient.changeWorkingDirectory(remoteDir); + if (changedDir) { + logger.info("Changed to directory: " + remoteDir); + } else { + setErrorMessage("Remote directory does not exist: " + remoteDir); + return Result.FAILED; + } + + try (InputStream inputStream = new FileInputStream(localFileToUpload)) { + logger.info("Start uploading file to " + remoteDir + "/" + remoteFile); + boolean done = ftpClient.storeFile(remoteFile, inputStream); + + if (done) { + String outputPath = remoteDir + "/" + remoteFile; + runTimeData.setValue(outputPath); + runTimeData.setKey(uploadedFilePath.getValue().toString()); + setSuccessMessage("File uploaded successfully to: " + outputPath); + return Result.SUCCESS; + } else { + setErrorMessage("File upload failed for: " + localFile); + return Result.FAILED; + } + } + + } catch (IOException ex) { + setErrorMessage("FTP Error: " + ex.getMessage()); + logger.warn("Exception occurred: " + ex); + return Result.FAILED; + } finally { + try { + if (ftpClient.isConnected()) { + ftpClient.logout(); + ftpClient.disconnect(); + } + logger.info("FTP connection closed."); + } catch (IOException ex) { + logger.warn("Error while closing FTP connection: " + ex); + } + if (localFileToUpload != null && localFile.startsWith("http")) { + localFileToUpload.delete(); // delete temp file if it was a download + } + } + } + + 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; + } +} \ No newline at end of file diff --git a/ftp_file_upload/src/main/resources/testsigma-sdk.properties b/ftp_file_upload/src/main/resources/testsigma-sdk.properties new file mode 100644 index 0000000..30c7d2f --- /dev/null +++ b/ftp_file_upload/src/main/resources/testsigma-sdk.properties @@ -0,0 +1 @@ +testsigma-sdk.api.key=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyMjMyMmM2Ni04NWYzLWIyN2UtN2FiOS0zM2U2M2Q4OWM1MGIiLCJ1bmlxdWVJZCI6IjQwMDQiLCJpZGVudGl0eUFjY291bnRVVUlkIjoiMzUifQ.lBjbuuWPNsHATQprYpnMTMXHpRarskoHsUtRkc2wgkPq4hmDlr8ivQJu2PHxx8pEkoPcF_fwtvSNkpqkdGov7A \ No newline at end of file