Ask a Question

Downloading a File via Redirect Link

SOLVED
BTscott
Contributor

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.

 

12 REPLIES 12
BTscott
Contributor

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) */

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.

 

I updated the following declarations

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

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

tristaanogre
Esteemed Contributor

The problem is not in the declaration of the active x object... it's in the "open" statement... you're trying to open a connection to the URL.  I'm guessing that there's something going on with that line of things, that the connection cannot be opened.

You've removed your full code... can you repost that?

Here's my thinking: the redirect is probably involving some sort of cookie or session or something like that... so, when you try and use the redirect URL, it's failing there.  

Another possibility is that the redirect URL has some characters or something in it that need to be "escaped" in order to work properly in a JavaScript environment.

 

Question: 

 

What are you attempting to test?  Are you testing the ability for the URL to redirect?  Or are you testing the contents of the downloaded file?  Is there a reason why you can't just use the direct URL for the download rather than having to do the redirect?


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

Thank you for your response.

 

I am attempting to test that a file downloads successfully from our site. The site has been setup to provide a re-direct link that launches a save prompt. I am not sure why this approach was taken as opposed to simply providing a direct download link. 

 

Here is the full code for the Javascript:

 

/**
* 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 = getActiveXObject("MSXML2.ServerXMLHTTP");
  var objFSO  = getActiveXObject("Scripting.FileSystemObject");
  var retVal;

  Log.Message("Downloading file", SrcFileURL, pmNormal);
  
  // Download the file
  objHTTP.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) */
tristaanogre
Esteemed Contributor

hrm... It's been a while since I've played with this kind of stuff... but for a redirect... are we sure that a "GET" is the correct command?  Aren't you sending data up via the URL that the server then responds to with the download?


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

Yes, I was thinking the same thing, but am still relatively new to webservice commands. I'm not sure that a POST is correct either. I confirmed that the purpose of redirect is to verify that a user is authorized to download file (ie. user is an admin)

tristaanogre
Esteemed Contributor

OK, then I suspect that what you're getting is not a problem with your code, but something having to do with authorization via a cookie or some such thing.

Is there a reason why you're not running this as a UI executed script that clicks on the links and such?  Because some sort of authentication is required, I suspect that may be the best way to simulate this rather than trying to use an MSXML.HTTP object.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

I would prefer to go through the UI, but we want this to work across browsers (IE, FireFox, Chrome). Specifically, I am having an issue handling the Open or Save file prompt that appears in IE.

cancel
Showing results for 
Search instead for 
Did you mean: