goliontus's avatar
goliontus
Occasional Contributor
8 years ago
Status:
Implemented

SFTP/FTP TestStep

Hi

 

I would like to suggest the development of a FTP/SFTP TestStep where I can FTP a generated DataSink file.

 

That will assist me greatly in automating an entire end to end process without the need for Groovy.

 

Thank you.

 

Regards

 

Charles

  • Hello Charles,

     

    I can definitely second your request as I had run into the same situation where I had to do an FTP upload.

    While we're waiting on the Smartbear to implement it, I have found a solution that you can use meanwhile.

     

    You could also make a Groovy file and add it to the /ext folder so that the FTP function globally available for the entire project.

     

     

    import org.apache.commons.net.ftp.*
    import java.io.InputStream
    import java.io.ByteArrayInputStream
    
    String ftphost = context.expand( '${#Project#ftphost}' )
    //String ftphost = //"va-dev-dmstable.alterian.com"
    
    String ftpuser = context.expand( '${#Project#username}' )
    String ftppwd = context.expand( '${#Project#password}' )
    int ftpport = 21
    
    def newDropFolderCategoryName = context.expand( '${DM FTP Drop Folders - Create Drop Folder Category#newDropFolderCategoryName}' )
    def newImportDropFolderName = context.expand( '${DM FTP Drop Folders - Create Drop Folder Category#newImportDropFolderName}' )
    
    String ftpDir = "/${newDropFolderCategoryName}/${newImportDropFolderName}"
    
    int TENSECONDS  = 10*1000
    int THIRTYSECONDS = 30*1000
    
    //Declare FTP client
    FTPClient ftp = new FTPClient()
    
    try 
    {
      ftp.setConnectTimeout(TENSECONDS)
      ftp.setDefaultTimeout(TENSECONDS)
      ftp.connect(ftphost, ftpport)
    
      //30 seconds to log on.  Also 30 seconds to change to working directory.
      ftp.setSoTimeout(THIRTYSECONDS)
      
      def reply = ftp.getReplyCode()
    
      if (!FTPReply.isPositiveCompletion(reply)) 
      {
        throw new Exception("Unable to connect to FTP server")
      }
    
      if (!ftp.login(ftpuser, ftppwd)) 
      {
        throw new Exception("Unable to login to FTP server")
      }
    
      if (!ftp.changeWorkingDirectory(ftpDir))
      {
        throw new Exception("Unable to change working directory on FTP server")
      }
    
      //Change the timeout here for a large file transfer that will take over 30 seconds
      //ftp.setSoTimeout(THIRTYSECONDS);   
      ftp.setFileType(FTPClient.ASCII_FILE_TYPE)
      ftp.enterLocalPassiveMode()
    
     // String filetxt = "Some String file content"
     // InputStream is = new ByteArrayInputStream(filetxt.getBytes('US-ASCII'))
     String filename = "C:\\QA_Automation_Branch\\DataFiles\\email_list_test_file_15_records_FTP.csv";
     is = new FileInputStream(filename);
      
    
      try
      {
        if (!ftp.storeFile("email_list_test_file_15_records_FTP.csv", is)) 
        {
          throw new Exception("Unable to write file to FTP server")
        }
      //Make sure to always close the inputStream
      }
      finally 
      {
        is.close()
      }
    
    } 
    catch(Exception e) 
    {
      //handle exceptions here by logging or auditing
      
    } 
    finally 
    {
      
      //if the IO is timed out or force disconnected, exceptions may be thrown when trying to logout/disconnect
      try 
      {
    
        //10 seconds to log off.  Also 10 seconds to disconnect.
        ftp.setSoTimeout(TENSECONDS);
        ftp.logout();
    
        //depending on the state of the server the .logout() may throw an exception,
        //we want to ensure complete disconnect.
      } 
      catch(Exception innerException)
      {
        
        //You potentially just want to log that there was a logout exception.
        
      }
      finally 
      {
        //Make sure to always disconnect.  If not, there is a chance you will leave hanging sockects
        ftp.disconnect();
      }
    }
    return true

     

  • Status changed:
    New Idea
    to
    Selected for Development

    An FTP step will be available in ReadyAPI v2.3.  

     

    Thanks,

    Mike Giller

    Product Owner

  • cyw's avatar
    cyw
    New Contributor

    GillerM, 

     

    I see that the FTP test step is for uploads. Is there one for downloads?

     

    Thanks