Forum Discussion

BTscott's avatar
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:   ...
  • tristaanogre's avatar
    6 years ago

    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.

  • AlexKaras's avatar
    6 years ago



    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);'POST', cWebServiceURL, false);
    // or
    //'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 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; } //----------------------------------------------------------------------------