Forum Discussion

BTscott's avatar
BTscott
Contributor
6 years ago

Downloading a File via Redirect Link

Greetings,

 

I am trying to construct a test for downloading a file from my employer's site in different browsers (IE, FireFox, Chrome) and I am using the Javascript solution provided here:

 

https://support.smartbear.com/viewarticle/8999/?st=0

 

However, I receive an error when I provide the download URL as the target URL (strFileURL). It may or may not be important to note that the URL I provide to the Javascript function is a redirect URL: https://[site]/common/redirect.cfm?/tcmain/common/filedownload.cfm+Filename=[FileName].xls&Path=//carrier%20rosters/Medical/&AdminPath=true

 

Here is the error I receive when executing the javascript function:

 

JavaScript runtime error.

Error: 0x80072ee6

Error location:
Line: 20 Column: 11.
 
Line 20 (from download file solution js):

// Download the file
objHTTP.open("GET", SrcFileURL, false);

 

Any advice would be greatly appreciated. Thank you.

 

  • If I read correctly, all you want to determine is if the file downloads correctly, right?

    That's not a browser specific task... that's just making sure that, when a request is sent, the file properly downloads...  That's a web server side, not a client side task, if I'm understanding things properly.

    So... what I would do is this.

     

    1) Split the test... test 1 is to verify that the link you intend to click on has the proper URL for doing the redirect

    2) Run the download test in a browser that you CAN actually run it in (Chrome, Firefox... not IE) to verify that the file is downloaded properly.


  • Hi,

     

    Yes, Robert is correct.

     

    Just several more notes:

    a) TestComplete perfectly supports cross-browser testing and Save File dialog handling. See help for the .SaveFile() method for more details and code samples;

     

    b) One of the recommended ways when working with files download from the web is not to do actual download, but just to check if the web server can return requested file. This is done with the help of the HEAD request. If the HEAD request succeeds this means that the server will be able to provide requested file.

     

    c) Your code misses part with ADO.Stream from the referenced article. This part is essential if you need to store downloaded file to hard drive;

     

    d) I would recommend to use some recording proxy (Fiddler, for example) and record the generated traffic when you download the file from web browser. Then you need to analyse recorded traffic to figure out if some request header and/or authorization is required;

     

    e) MSXML2.ServerXMLHTTP and WinHTTP.WinHttpRequest.5.1 objects that are usually used to issue web requests from test code have .Option property. In the past I used this property to ignore certificate errors. I think that some similair option to automatically handle redirects exists as well. Check the documentation for the object you'd like to use.

     

    Just for a reference, the relevant code in my case looked like this:

    http = HTTPObjectGet('WinHTTP.WinHttpRequest.5.1', false);
    http.open('POST', cWebServiceURL, false);
    // or
    // http.open('POST', cWebServiceURL, false, '<login>', 'password');
    // http.setRequestHeader("Authorization", 'Basic abc123='); // Base64-encoded login/password, depending on the call requirements
    // http.setRequestHeader("Content-Type", 'application/xml'); // or some other required request header(s)
    
    http.send(strBody); // for POST calls
    // or
    // http.send(); // for GET calls
    ...
    //----------------------------------------------------------------------------
    
    function HTTPObjectGet(strObjectId, bIgnoreServerCertErrors)
    {
      // Ignore errors like 'invalid certificate' on the server
      var SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS = 13056;
      var cDefaultHTTP = 'MSXML2.ServerXMLHTTP';
    
      var oHTTP;
    
      strObjectId = strObjectId || cDefaultHTTP;
    if ('undefined' == typeof bIgnoreServerCertErrors)
    bIgnoreServerCertErrors = true; try { oHTTP = Sys.OleObject(strObjectId); } catch(e) { Log.Error(aqString.Format('Failure to create requested %s object. Test stopped.', aqString.Quote(strObjectId)), strObjectId + '\n' + e.Message); // Runner.Stop(true); // stop current test only } if (bIgnoreServerCertErrors) { try { oHTTP.setOption(2, SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS); // MSXML2.ServerXMLHTTP version } catch(e) { try { oHTTP.Option(4) = SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS; // WinHTTP.WinHttpRequest.5.1 version } catch(e) { // Log.Error('Failure to set option to ignore http server errors', e.Message); } } } return oHTTP; } //----------------------------------------------------------------------------

     

  • This is how my javascript function is set up:

     

    /**
    * Check provided column to see if provided value exists within file.
    * If value exists, return row index in which first match was found.
    * If value is not found for given column, return -1
    * 
    * @param SrcFileURL     the link for downloading file
    * @param DestFilepath   the destination in which to save file
    * @param SearchValue    the value to search for within file
    *
    * @return   TRUE if file exists for destination path (download successful)
    */
    function downloadFile(SrcFileURL, DestFilepath) {
      var objHTTP = new ActiveXObject("MSXML2.XMLHTTP");
      var objFSO  = new ActiveXObject("Scripting.FileSystemObject");
      var retVal;
    
      Log.Message("Downloading file", SrcFileURL, pmNormal);
    
      // Download the file
      objHTTP.open() .open("GET", SrcFileURL, false);
      objHTTP.send();
    
      /* wait for file to finish downloading */
      while((objHTTP.readyState != 4) && (objHTTP.readyState != 'complete')) { 
        Delay(100);
      } /* while: file is still downloading */
    
      if (200 == objHTTP.Status && objFSO.FileExists(DestFilepath)) {
        Log.Message("Finished downloading file", "File downloaded to: " + DestFilepath, pmNormal);
    
        retVal  = true;
      } /* if: download was successful */
      else {
        Log.Error("The " + SrcFileURL + " file was not found." + 
        " The returned status is " + objHTTP.Status);
        retVal  = false;
      } /* else: download failed */
    
      return retVal;
    } /* end function: ownloadFile(SrcFileURL, DestFilepath) */
    • BTscott's avatar
      BTscott
      Contributor

      I updated the following declarations 

      var objHTTP = new ActiveXObject("MSXML2.XMLHTTP");
      var objFSO  = new ActiveXObject("Scripting.FileSystemObject");

       

      Now, I get an error on the first declaration stating that ActiveXObject is not defined.

       

      • BTscott's avatar
        BTscott
        Contributor

        I updated the following declarations

        var objHTTP = getActiveXObject("MSXML2.XMLHTTP");
        var objFSO  = getActiveXObject("Scripting.FileSystemObject");

        Now I am back to receiving the '0x80072ee6' error