Saving a PDF with Watir

1,155 views
Skip to first unread message

Nicolas Turelier

unread,
Apr 10, 2012, 9:35:50 AM4/10/12
to watir-...@googlegroups.com
Hi there, I have a little Problem with The watir webdriver.

In this case, I have to click a button (no big matters right here) and then, a pop up appears with a PDF file inside.

The problem is : I can't know in advance the name of the popup window, The PDF in the popup is opened with the Acrobat Reader interface and I have to save this PDF in a specific folder on my HardDrive.

How can I do ? I am totally stuck with this problem.

Thanks in advance

(I'm French, sorry for my terrible english)

Željko Filipin

unread,
Apr 10, 2012, 9:49:38 AM4/10/12
to watir-...@googlegroups.com
On Tue, Apr 10, 2012 at 3:35 PM, Nicolas Turelier <nicolas....@gmail.com> wrote:
> The PDF in the popup is opened with the Acrobat Reader interface and I have to save this PDF in a specific folder on my HardDrive.

The easiest thing to do is to disable the PDF reader. This is how to do it in Chrome:

require "watir-webdriver"
browser = Watir::Browser.new :chrome

# disable Chrome PDF Viewer
browser.goto "about:plugins"
browser.span(:text => "Chrome PDF Viewer").parent.parent.parent.a(:text => "Disable", :class => "disable-group-link").click

Then just do what is needed for the popup to appear, and Chrome should download the file to Downloads folder.

Željko
--
watir.com/book - author

Nicolas Turelier

unread,
Apr 10, 2012, 10:50:59 AM4/10/12
to watir-...@googlegroups.com
Thanks a lot, but the problem is that all of my tests are only for IE, this is a specification I can't sidestep :s

Chuck van der Linden

unread,
Apr 10, 2012, 2:16:10 PM4/10/12
to watir-...@googlegroups.com
So is your job to test that IE can properly save a file, or that the PDF viewer can properly save a file?  How many PDF viewers will you test? (there are more than just adobe's you know) and what value is testing someone-elses stuff bringing to your employer?

Or is your objective to determine if the server has the proper PDF file and serves it correctly?

If the objective is the latter, then you might just want to see if you use Watir to find the URL in the link, then briefly bypass the browser and download the file. If you can show us a HTML sample we can guide you in how you would go about determining the link for the PDF file.  Once you have that, you could make a direct HTTP request for the file using standard HTTP support built into ruby via net/http.

See this StackOverflow question for examples of code that would directly download a file using http presuming you know the URL of the file.  http://stackoverflow.com/q/2263540/409820

Bhavesh

unread,
Apr 13, 2012, 12:15:29 PM4/13/12
to Watir General
Hi Chuck,

Here what ever you are saying make sense, even i have gonbe through
the link you proposed.

But what in case if you dont have a http address?

In my case for download, a additional IE window by title "File
Download" gets opened, now i can click on save button on this
window.this open another IE window by title "Save As".

Now here i have to click on Desktop tab, and then have to save the
file. everything in IE.


How can i achieve this?

Also is there any way by whoch we can unzip OR see the files inside
the zip?

Bhavesh

Chuck van der Linden

unread,
Apr 13, 2012, 4:36:44 PM4/13/12
to watir-...@googlegroups.com
You have to have the http address.. if clicking the link will open the file, then the address is there.  You just need to figure out where it is in the HTML and pull it out.

(this is your que to post a sample of the HTML for the thing you are clicking)

All the steps you are describing doing in IE are just taking IE through the process of saving a file, it tests nothing in your code.


There are command line utilities galore to deal with .zip files, I would just use one and have ruby issue the commands for you, it's very good at that sort of thing (if you have not done so, I recommend reading the book 'everyday scripting with ruby')
Reply all
Reply to author
Forward
0 new messages