QTP or UFT and VBScript : How can you upload file to Server via WinSCP command line using VBScript?
August 13, 2018
QTP or UFT and VBScript : How can you upload file to Server via WinSCP command line using VBScript?
Welcome to this post!-“QTP or UFT and VBScript : How can you upload file to Server via WinSCP command line using VBScript?”
If you are looking for latest HP UFT QTP interview Questions, then you are at right place. This post – “QTP \UFT and VBScript : How can you upload file to Server via WinSCP command line using VBScript?” consists of technical interview question which have been part of many interviews either external or internal. Plus this post will give a broader perspective on the usage and implementation of Winscp command line options using VBScript in UFT or QTP. Usually these are assumed to be known in depth to check the logical solvency efficiency of the candidate and of course suitability for the project and company. Go ahead and enjoy reading…
This discussion will help you prepare well if you are going for the interview or you need the function to be used in your project.
In the interview usually, you might be questioned in different ways like:
- How to put a file on a network folder using WinSCP command line?
- How can you upload file to server using VBScript?
- How can you upload files to server using QTP or UFT via WinSCP command line?
- How to put files to server location using WinSCP command line options?
- How to upload file to server via WinSCP command line using VBScript?
- How to transfer files to network location via WinSCP command line using VBScript?
- How can you upload file to Server via WinSCP command line using VBScript? And so on.
Now consider a scenario where your project is quite large and is dependent on various different modules (or say applications). Then to work with some of the applications you require WinSCP but not with its GUI version but with command line and its options. As you are an automation tester your aim is to automate this tough part.
Usually these manual intervention-based scenarios are blocker and does not become part of your automation suite. There can be n-number of reasons behind it, for say- putting the files take time on server to arrive (assuming you have access to server through your network using given credentials and paired public key). The other reason can be you don’t have the proper knowledge on WinSCP or any other similar tool’s API or command line options.
In HP UFT or QTP we don’t have any feature or support for another tool like WinSCP or Putty or to work across network. The only option we have is to work with VBScript, which is another challenge as its partial OOP’s language.
Before you start working on this script which I am going to discuss, I would recommend you to get familiarize yourself with some command options available in WinSCP. I have worked extensively with WinSCP command line using VBScript as well as GUI and have used most of its features not just downloading or uploading files.
Firstly, I would request you to try and understand the model of WinSCP and related commands, go through some articles on how exactly it works. This will help you gain some confidence and idea about the tool working in different environment not just windows but like Linux, Unix etc. Mostly automation tester who have developer background get grip on it easily but people coming from manual testing find it difficult to work with the tool options on command line. Considering them too in the discussion, I will visit the code in parts to make you understand what exactly is meant step by step.
Sounds Good! right?
Let’s focus on the code that we are targeting on the post that is how to upload any file\files to server location from your local folder via WinSCP command line using VBScript or How to upload file to server via WinSCP command line using VBScript.
Part 1 : Variables:
Dim FileToUpload, RunCmnd, ShellObj,ExecCmnd,strText,flag,ErrFlag flag=false ErrFlag=false
In the beginning of the code it is really a good practice to write all your variables. Here we have declared all variables like
FileToUpload= variable for upload file with path,
RunCmnd = storing the command,
ShellObj = referencing the shell object,
ExecCmnd = executing the run command,
strText = storing the output messages ,
flag= status of transfer,
ErrFlag = status of error found
Part 2 : Assignments
‘check for / at the end of FTP file path first If not right(FTPFilePath,1)="/" then FTPFilePath= FTPFilePath + "/" End if Set FSO= CreateObject("Scripting.FileSystemObject") Set fldr = FSO.GetFolder(SourcePath) Set files = fldr.Files ‘check for \ at the end of source file path If not right(SourcePath,1)="\" then SourcePath= SourcePath + "\" End if RunCmnd = "Winscp.com /command " + """Open sftp://" + UserID +":+Password+"@"+Host+ "/ -hostkey=""" + """ssh-rsa 2048 your key here"""+ " " +"""put "+FileToUpload+ " " + FTPFilePath +" """ RunCmnd= RunCmnd& """ ""Exit""" Set ShellObj= CreateObject("wscript.shell") Set ExecCmd =ShellObj.Exec("cmd /K "+ "cd\Program Files (x86)\Winscp" & " & " & RunCmnd & " & " & "exit")
In this section we are assigning the variables with values or creating the instances of the class. Firstly, we are setting the flags to false
flag=false
ErrFlag=false
Then we are checking for “/” at the end of FTP file path, whether its present or not. If not then append it. This will help us in setting up complete file path as below:
FTPFilePath = FTPFilePath + “\”
Similarly, we are checking for “\” at the end of Source file path, whether its present or not. If not then append it. This will help us in setting up complete Source file path as below:
SourcePath= SourcePath + “\”
Now set the RunCmnd with winscp statement and put the input variables for running winscp command with options as shown in the code above.
Lastly. create a shell object,
Set ShellObj= CreateObject(“wscript.shell”)
This object is required to execute the command via command line.
Part 3 : Execution
Now in this part, we are just executing the command with the help of windows shell.
Set ExecCmd =ShellObj.Exec(“cmd /K ”+ “c:\Program Files (x86)\Winscp” & “ & “ & RunCmnd & “ & “ & “exit”)
Once this is done there are two ways of catching errors either,
1- you can skip the below code to check the upload has happened or not, (as manually you can check files have been transferred or not using Winscp GUI)
or
2- you implement the code shown below to check upload status of the desired file. It’s completely up to you.
Now let’s check if your upload happened correctly or not.
Part 4 : Result
In this part you have either error in hand or a successful upload of the file. In case some error has occurred then it is reported as FAIL along with the description. And here value for return is set to False.
If upload was successful then PASS event is called and value of return is set to True.
‘get the status of upload by iterating through the stdout object Do while not ExecCmd.StdOut.AtEndOfStream strText = ExecCmd.StdOut.ReadLine() if InStr(1,strText,FileToUpload,1) > 0 then Reporter.ReportEvent micPass,"File got transferred" flag= true end if ‘check for errors here If Instr(1,ucase(strText),"ERROR",1)>0 or InStr(1,ucase(strText),"FAILED",1)>0 or InStr(1,strText,"Cannot create remote file,1")>0 or InStr(1,ucase(strText),"No Such File or Directory",1) > 0 then Reporter.ReportEvent micFail,"Exit due to error", strText ErrFlag=true ExitTest(1) Exit do Else Reporter.ReportEvent micFail,"Message from command line is ::", strText End if Loop
Part 5: Releasing objects
A very crucial step is releasing the objects which you have used. Most of the times we do not release them thinking this might happen itself, but in VbScript there is no guarantee to be honest. For some objects it happens correctly and for some it might have memory links open. So, to avoid any future issues, it’s always good to write code for release the binding as shown below:
‘now release objects
Set ExecCmnd=nothing
Set ShellObj=nothing
Till now we have discussed the complete code but in parts, now let’s have a look on the complete code:
A word of Caution!
Before running this code please check you have admin privileges to run the code using winscp command line.
'------------------------------------------------------------' 'Function Name: Fn_UploadFileToServerWinSCP 'Documentation: upload any file to server from local folder via WinSCP command line using VBScript 'Created By: Prachur Saxena 'Date : 13/1/2018 'Modified By: NA 'Revision Comments: NA 'Return Values: True or False 'Example: Fn_ UploadFileToServerWinSCP "serverpath", "C:\Users\Public\Documents\Files","host","userid","password" '----------------------------------------------------------- '------------------------------------------------------------' Function Fn_ UploadFileToServerWinSCP (FTPFilePath,SourcePath,Host,UserID,Password) Dim FileToUpload, RunCmnd, ShellObj,ExecCmnd,strText,flag,ErrFlag flag=false ErrFlag=false On error resume next ‘check for / at the end of FTP file path first If not right(FTPFilePath,1)="/" then FTPFilePath= FTPFilePath + "/" End if Set FSO= CreateObject("Scripting.FileSystemObject") Set fldr = FSO.GetFolder(SourcePath) Set files = fldr.Files ‘check for \ at the end of source file path If not right(SourcePath,1)="\" then SourcePath= SourcePath + "\" End if if files.Count > 0 then For each file in files ‘complete the filename with path FileToUpload = SourcePath + Filename RunCmnd = "Winscp.com /command " + """Open sftp://" + UserID +":+Password+"@"+Host+ "/ -hostkey=""" + """ssh-rsa 2048 your key here"""+ " " +"""put "+FileToUpload+ " " + FTPFilePath +" """ RunCmnd= RunCmnd& """ ""Exit""" Set ShellObj= CreateObject("wscript.shell") Set ExecCmd =ShellObj.Exec("cmd /K "+ "cd\Program Files (x86)\Winscp" & " & " & RunCmnd & " & " & "exit") ‘get the status of upload by iterating through the stdout object Do while not ExecCmd.StdOut.AtEndOfStream strText = ExecCmd.StdOut.ReadLine() if InStr(1,strText,FileToUpload,1) > 0 then Reporter.ReportEvent micPass,"File got transferred" flag= true end if ‘check for errors here If Instr(1,ucase(strText),"ERROR",1)>0 or InStr(1,ucase(strText),"FAILED",1)>0 or InStr(1,strText,"Cannot create remote file,1")>0 or InStr(1,ucase(strText),"No Such File or Directory",1) > 0 then Reporter.ReportEvent micFail,"Exit due to error", strText ErrFlag=true ExitTest(1) Exit do Else Reporter.ReportEvent micFail,"Message from command line is ::", strText End if Loop 'set variable blank or empty FileToUpload = "" Next If Err.Number =0 and flag=true and ErrFlag=False then Fn_UploadFileToServerWinSCP = true Else Fn_UploadFileToServerWinSCP = false End if ‘now release objects Set ExecCmnd=nothing Set ShellObj=nothing End Function
The best part of this solution is that it works for both the single file and multiple files because I’m iterating through the files collection object. So, if it has one or more files we are uploading each one of them on the desired server location.
The other point which we see here is that we can also pass just the filename which we want, for that we need extra input parameter as filename.
In different scenarios of course, the parameters will change. It’s up to you how well you utilize the options of WinSCP command line using VBScript .
The good point about this function is that it is flexible enough to accept changes and gives you a broader view while debugging.
Plus, this makes the function more adaptable to any framework in question like linear, hybrid, keyword etc. You can use this with any existing framework or when designing a framework from scratch.
Conclusion
Let’s conclude our discussion on “How to upload file to server via WinSCP command line using VBScript” with few good points to be noted :
- Prior calling the function you must know which type of resource you are targeting for the upload.
- Often this kind of functions are used to upload any file type.
- The download may take some time for larger files which completely depends on your internal network speed. So, choose the options wisely.
- This function gives more flexibility in uploading the files to your project designated folders.
- This function can be part of any framework in hand, i.e. regardless of kind of framework it can be easily adapted.
This brings us to the end of our discussion on “How to upload file to server from your local folder via WinSCP command line using VBScript or How to upload file to server via WinSCP command line using VBScript ”.
I really hope you have enjoyed reading the post. If you have any doubt on this please feel free to add your comment below.
And if you like to read more on UFT or QTP Technical Interview Questions please follow below links:
UFT Technical Interview Questions – Set 1
UFT Technical Interview Questions – Set 2
How to Download Resource From QC\ALM?
How to Upload Resource To QC\ALM?
UFT – How to download file from Server via WinSCP using VBScript?