Automatic, timed file downloads with WebDriver - javascript

I am automating a website with WebDriver but my file download needs are a little different from those I have found when googling.
I have a website which creates orders. When I click the 'place order' button, it redirects me to the Print Order page. As a "convenience" for me, this page automatically launches the download operation in its body onload, meaning that when I click the 'place order' button, I go to a new page and then a file download dialog immediately appears to let me download the generated pdf, thus blocking the browser.
Here are the solutions I found/thought of, and why I couldn't use them:
Configure Firefox/Chrome profiles to silently download files. Can't use this because I have the requirement of timing how long the download takes.
Override window.open with a function that prevents the download, and allows me to grab the URL and download it with wget. Can't use this because the file download is started from the onload function of the next page, so any javascript I run on this page will be lost.
Cancel the onload function or try to execute code before the onload function. Can't find a way to do this in webdriver.
Download the print page with wget, modify the html to change the onload handler, and inject the modified html back into selenium. Can't find a way to replace an entire page, including <head> and <body> tag and URL.
Unfortunately I can't change the source code of this website because I am in QA and I don't have that sort of leverage with development. Does anyone have any ideas for a way to download this file in an automatic manner that can be timed?
Thanks.

Seems to me you have 2 options.
Use an AUTOIT script to download the file once you receive the popup, this tests the functionality of the page opening and the download.
Download the file just using http get. This misses out testing that the page loads but will still check that the file exists.
The other tests you mention above seem to be too complicated.

Related

How to track when browser receives file on URL click

I have a URL which onclick it downloads a file. Some of these files are quite big and it takes some time for the browser to receive it. This can be frustrating for the user, especially for the inexperienced ones, who don't realize that the file is about to be downloaded.
I tried to add the tag "download" in the element but it doesn't work for my Chrome, Firefox version. As an alternative I added:
target = "_blank"
Which opens a new blank page and once the download starts it closes it. But this solution is not very elegant.
Is there a way to track when the browser will receive the file? So I am able to show an indicator?

Google chrome Javascript debugger

is it possible to reload the content of the viewport without reloading the whole inspector.
I would like to make some changes to a javascript file in the debugger then reload the page to see the temp changes I have made (on load), before I commit to the file and upload. Where I'm working at the moment there is caching I can't get round so I have to wait a minute or two to see what my changes do on load.
I have found the page I want to work with under source and can save changes, but really need to see some actions that are fired on the loading of the page.
Is this possible?
Thanks
you can't do that with chrome debugger tool. each JS resource will reload on page load so you will loose your change.
you can proxy tools like Fiddler which will allow you to load script from your local machine while loading a third party website. You can create a copy on your own machine and then use fiddler to intercept the call for that file and send your local copy instead

Loading a webpage from an application and then clicking a link on that page?

I'm writing an app which should load a website. After the website is loaded, I have to fire up a 2nd command to the web page, which is kind of Javascript scriptlet.
I did this manually in my browser and in generally it seems to work fine. They way I did it manually was I created two shortcuts in my browser:
the URL to this website (e.g. http://www.example.net/123456-e.aspx)
the shortcut to a Scriptlet which calls a function on this website (e.g. javascript:__doPostBack('Video_Info1$Rating_control1$lnk_star5','')).
I first click the URL shortcut and after its loaded, I fire up the 2nd shortcut.
But how can I do this in a C# application?
But, what I'm trying to do is quite the opposite I think.
I'd like to SEND a _postBack to an existing site.
I'm not writing my own site which contains a postBack control!
Let me explain a little in detail...
the site I'm loading in my app is an existing ASPX site in the web.
In this site, you'll find several _doPostback entries and in a browser, the postBack event (when I hit it manually within a bookmark/shortcut) will be send back and has its desired result (in this case, its assigning "5 stars" to tell everybody, that something is supergood ;-).
So, what I'd like to do is to write my C# App which will send this _postBack command every time I hit the OK button or some other events.
Just as it is with my link above
"javascript:__doPostBack('Video_Info1$Rating_control1$lnk_star5','')"
This is the bookmark I have on my browsers bookmark panel - a shortcut!
.... Just to be honest, its a kind of cheat, which will do an automatic voting on an external site...
My prob is now,... how can I send this _postBack straight to the site, to increase my stars?
I am not sure what exactly you are trying to achieve, The code which you are seeing (_doPastBack(..)) is actually going to invoke a server method (code in your codebehind of that page).
You can create a webpage and have an iframe and load your first page in that.The user will be able to click on any of the button and invoke the corresponding server code if there is one attached.
<html>
<body>
<iframe src="http://www.example.net/123456-e.aspx"></iframe>
</body>

Precache file with Javascript in the browser?

How can I do this:
the page loads
javascript loads a remote PDF file into local memory
the user clicks a button/link
the system launches the PDF reader or starts a download dialog with the PDF file already in memory
In other words, it's a regular file download in the browser EXCEPT that the file has already been loading in the background in order to speed up its receipt when/if the user decides to download the file.
You would have to encode the file (perhaps via a servlet), then you could get it through an XHR, and write it into a data uri, which you could then attach to a button or link.
This technique would probably only work on small files and very recent browsers.
StackOverflow won't let me post an example link as a link, so to test the concept, you'll have to copy the following line into an html file and see if you can load the link:
pdf link
This worked perfectly in Chrome when I tested it just now, and worked partially in Firefox. It didn't work at all in my version of IE.
Another potential solution is to make absolutely sure that the pdf is being cached, and then try to load it in a hidden iframe. Whether this works or not will depend on how the user has their browser set up.
You should consider not doing it at all, given the difficulties.

Is it possible to open a file from a shared folder outside the browser

We have a file handling ASP.Net web control used in intranet web applications, that currently uses ActiveX to handle file check-outs, and check-ins. Works fine, in IE&Win.
But now we are trying to get rid of ActiveX & IE only behavior...
If a file is checked out, it is copied to file share, with access right limited to the checking-out user.
Using a hidden iframe, and setting the src of the iframe to something like "file:////file_share/dictionary/users_stuff/someDoc.doc", an open/download dialog is shown, so the user can open and edit the shared file in Word, Excel, etc directly from the file share.
Works fine for file types browsers can't handle themselves.
But for file types like txt, images, html the browser simply loads the file to the iframe, or opens the file, if the user is given a link. And the user can't edit the file without manually launching the appropriate application and copying the url. Showing the users a "Copy this url to your preferred application, and try to edit it" would not be really user friendly...
My question is: is it possible to get the browser (without ActiveX, IE...) to pass the link to the OS, or show a "What do you want to do with this file" dialog of some sort?
If not, what and how could be achieved?
The closest I could come up with is this thread:
https://stackoverflow.com/a/1394725/1195927
#Red says no (and the original poster agrees) BUT #Daan seems to have a solution.
I have not tested, so YMMV.
If a javascript/html solution is not found, I may have an ugly hack for you . . .
See my post here: Launching a Downloadable Link

Categories