https://www.qatechies.com

QTP or UFT and VBScript : How can you download file from Server via WinSCP command line using VBScript?

Latest Tosca,QTP,UFT,CodedUI Interview Questions

QTP or UFT and VBScript : How can you download file from Server via WinSCP command line using VBScript?


Welcome to this post!-“QTP or UFT and VBScript : How can you download file from 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 – “QTPUFT and VBScript : How can you download file from Server via WinSCP command line using VBScript?” consists of technical interview question which have been part of many interviews either external or internal. Usually these are assumed to be known in deep 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:

  1. How to get file from network folder using WinSCP command line?
  2. How can you download file from server using VBScript?
  3. How can you download files from server using QTP or UFT via WinSCP command line?
  4. How to get files from server location using WinSCP command line options?
  5. How can you download file from Server via WinSCP 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- the files on server arrive at some specific time and you can manually access these files only 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.

Well, it’s completely dependent on the project in which you are working and its requirement. Some projects use old tools as well as new tools which are available in the market, that is, to support project activities. Some part of the project is dependent on some features which are restricted and can only be accessed by tools like WinSCP, putty, etc. and finding a solution on it to automate is another challenge.

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 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.

Sound Good! right?


Focusing on Function Head Comment Section

So, to begin with, let us focus on discussing the most important part of writing code which is always create a heading comment section whenever you are writing code, it’s a good practice and really matters when you are liable for the module or application under test in which you were working. The readers or peers should be able to concentrate or focus on the usage of function just by going through your comments. Let me show you a good way with example.

Consider the heading comments below:

'Function Name: Fn_DownloadFileFromServerWinSCP
'Documentation: download any file from server to local folder via WinSCP using vbscript
'Created By: Prachur Saxena

It looks fair enough for anyone reading the heading comments of the function as there is good information given here. But we can save more time of code readers by providing the input and output expected or valid values for this function.

Don’t you think so it will be really a good for another person to go through it easily if we add some more data?

Let’s revisit the same again,

'Function Name: Fn_DownloadFileFromServerWinSCP
'Documentation: download any file from server to local folder via WinSCP using VBScript
'Created By: Prachur Saxena
'Date : 13/1/2018
'Modified By: NA
'Revision Comments: NA
'Return Values: True or False
'Example: Fn_ DownloadFileFromServerWinSCP "serverpath","C:UsersPublicDocumentsFiles”,”host”,”userid”,”password”

Wow! now that’s look fantastic and quite impressive to understand for anyone to use the function in his code.

With this information he skips many steps like code debugging, asking another tester to understand the code and actually waste both the persons time, etc. The more you put input parameters examples the better it becomes as it gives much clarity on the working logic.

There are 3 additional comments which are shown here i.e. Date, Modified By, Revision Comments – these are a MUST and should be mentioned in the comments. The impact initially won’t occur to you if you are the developer of the function but will give someone else broader view if is being asked to work on it.

If This Sounds Often to you, then make a HABIT of doing it.

Here I believe our very first part of writing a good function heading with comments ends.


Now let’s focus on the code that we are targeting on the post that is how to download any file from server location to your local folder via WinSCP command line using VBScript.

Part 1 : Variables:

Dim FileToDownload, 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

FileToDownload = variable for 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

flag=false
ErrFlag=false
‘check for / at the end of FTP file path first
If not right(FTPFilePath,1)=”/” then
   FTPFilePath= FTPFilePath + “/”
End if
‘complete the filename with path
FileToDownload = FTPFilePath + Filename
RunCmnd = “Winscp.com /command “
+ “””Open sftp://” + UserID +”:+Password+”@”+Host+
“/ -hostkey=””” +
“””ssh-rsa 2048 your key here”””+ “ “ +”””get -preservetime “+FileToDownload+” “ + DestPath + “”
RunCmnd= RunCmnd& “”” “”Exit”””
Set ShellObj= CreateObject(“wscript.shell”)

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. Then complete the filename with ftp path

FileToDownload = FTPFilePath + Filename

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 shell will be used to execute the WinSCP command line using VBScript.


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 ”+ “cdProgram Files (x86)Winscp” &  “ & “ & RunCmnd & “ & “ & “exit”)

Once this is done there are two ways of code exists, 1- you can skip the below code to check the download has happened or not, or 2- you implement the code shown below to check download status of the desired file. It’s completely up to you.

Now let’s check if your download happened correctly or not.


Part 4 : Result

In this part you have either error in hand or a successful download 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 download was successful then PASS event is called and value of return is set to True.

Do while not ExecCmd.StdOut.AtEndOfStream
   strText = ExecCmd.StdOut.ReadLine()
if InStr(1,strText,FileName,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
If Err.Number =0 and flag=true and ErrFlag=False then
  Fn_DownloadFileFromServerWinSCP = true
Else
 Fn_DownloadFileFromServerWinSCP = false
End if

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_DownloadFileFromServerWinSCP
'Documentation: download any file from server to 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_ DownloadFileFromServerWinSCP "serverpath",
"C:UsersPublicDocumentsFiles”,”host”,”userid”,”password”,”data.xlsx”
'-----------------------------------------------------------
Function Fn_ DownloadFileFromServerWinSCP (FTPFilePath,DestPath,Host,UserID,Password,Filename)
Dim FileToDownload, RunCmnd, ShellObj,ExecCmnd,strText,flag,ErrFlag
flag=false
ErrFlag=false
‘check for / at the end of FTP file path first
If not right(FTPFilePath,1)=”/” then
   FTPFilePath= FTPFilePath + “/”
End if
‘complete the filename with path
FileToDownload = FTPFilePath + Filename
RunCmnd = “Winscp.com /command “
+ “””Open sftp://” + UserID +”:+Password+”@”+Host+
“/ -hostkey=””” +
“””ssh-rsa 2048 your key here”””+ “ “ +”””get -preservetime “+FileToDownload+” “ + DestPath + “”
RunCmnd= RunCmnd& “”” “”Exit”””
Set ShellObj= CreateObject(“wscript.shell”)
Set ExecCmd =ShellObj.Exec(“cmd /K ”+ “cdProgram Files (x86)Winscp” &  “ & “ & RunCmnd & “ & “ & “exit”)
‘get the status of download by iterating through the stdout object
Do while not ExecCmd.StdOut.AtEndOfStream
   strText = ExecCmd.StdOut.ReadLine()
if InStr(1,strText,FileName,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
If Err.Number =0 and flag=true and ErrFlag=False then
  Fn_DownloadFileFromServerWinSCP = true
Else
 Fn_DownloadFileFromServerWinSCP = false
End if
‘now release objects
Set ExecCmnd=nothing
Set ShellObj=nothing
End Function

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 with few good points to be noted :

  1. Prior calling the function you must know which type of resource you are targeting for the download.
  2. Often this kind of functions are used to download any file type.
  3. The download may take some time for larger files which completely depends on your internal network speed. So, choose the options wisely.
  4. This function gives more flexibility in downloading the files to your project designated folders.
  5. 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 download file from server 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?

Or if you prefer General Interview Questions please follow below links:

UFT General Interview Questions – Part 1

UFT General Interview Questions – Part 2


If you would like to keep track of further articles on UFT (QTP). I recommend you to SUBSCRIBE by Email and have new UFT articles sent directly to your inbox.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

advanced-floating-content-close-btn
https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5034726663464946 (adsbygoogle = window.adsbygoogle || []).push({});
advanced-floating-content-close-btn 
*************************** Do you want to learn TOSCA? Do you want to excel in Career? Try my New Courses: 1. Tricentis Tosca and Working with Excel   2. Tricentis Tosca and UI Automation Great News!!   Price has been slashed down for limited period of time.So Hurry!!! Click The Shown Links and Enjoy Learning. ***************************
 
error: Content is protected !!