While automation, the come to situations where you need to need to download a file on clicking a link. This generally involves a lot of User Interface (GUI) overhead like syncing the download box, clicking the buttons, Managin G The Save as box, etc. This many a time causes sync issues. Moreover, we end up automating something, which is not at all needed to be automated or tested. In this situation, all need are a code snippet in Visual Basic for Quick Test Professional that automatically downloads The file in the background without the need of any GUI appearances. You just need to provide the download link and the file path in your system. The function "Funcdownloadfile" below takes the file path and the download link as parameters in String and performs the D Ownload in the background. You just need to capture the download URL from the potential download link and call this function and you'll find the Do Wnload completed at the specified path. the function supports AuthenticaTed downloads and proxy settings. The function is based Onadodb STREAM object and winhttp API from Microsoft.
Subutildownloadfile (strFilePath, strURL)SetWinhttpreq =CreateObject("winhttp.winhttprequest.5.1") 'winhttpreq.setproxy httprequest_proxysetting_proxy, "xxx.xxx.xxx.xxx:zzzz" 'Required only if your Internet routes through a proxy. Not required in 90% cases. 'can ignore this line for first attempt but add it if your download are hindered, X is IP and Z is PortTemp= Winhttpreq.open ("POST", strURL,false) 'winhttpreq.setcredentials "Username", "Password", Httprequest_setcredentials_for_server 'Required only if the file download server Required authentication. Not required in 90% cases. Change Username and Password Wuth actuals. winhttpreq.send () winhttpreq.waitforresponse strresult=Winhttpreq.responsebodySetOStream =CreateObject("ADODB.stream") ConstadTypeBinary =1 ConstAdsavecreateoverwrite =2Ostream.type=adtypebinary ostream.open ostream.write strresult ostream.savetofile strFilePath, AdSaveCreateOverWrite End Sub
QTP download files to local (go) via URL address