cancel
Showing results for 
Search instead for 
Did you mean: 

Downloading a File via Redirect Link

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

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
Community Hero

Re: Downloading a File via Redirect Link

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.



Robert Martin
[Community Expert Group]
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
Extensions available
Community Hero

Re: Downloading a File via Redirect Link

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; } //----------------------------------------------------------------------------

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
12 REPLIES 12
Contributor

Re: Downloading a File via Redirect Link

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

Re: Downloading a File via Redirect Link

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.

 

Contributor

Re: Downloading a File via Redirect Link

I updated the following declarations

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

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

Community Hero

Re: Downloading a File via Redirect Link

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
[Community Expert Group]
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
Extensions available
Contributor

Re: Downloading a File via Redirect Link

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&colon;

 

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

Re: Downloading a File via Redirect Link

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
[Community Expert Group]
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
Extensions available
Contributor

Re: Downloading a File via Redirect Link

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)

Community Hero

Re: Downloading a File via Redirect Link

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
[Community Expert Group]
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
Extensions available
Highlighted
Contributor

Re: Downloading a File via Redirect Link

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.

New Here?
Join us and watch the welcome video:
6 / 7 API Testing Mistakes Video
Top Kudoed Authors