juoppja
juoppja

Reputation: 11

How to force download PDF attachment using Robot Framework + Selenium Library?

In my test automation suite I need to force download of a PDF file. The file in question is a PDF that has mime-type of an application/octet-stream, and furthermore, the server assigns

Content-Disposition: attachment;
X-Download-Options: noopen 

to the file.

When the link is clicked, a prompt gets in the way:

enter image description here

I know about the options that can be set when initializing Open Browser keyword so I did the following:

*** Settings ***
Library    SeleniumLibrary

*** Variables ***
${PREFERENCES}    set_preference("browser.download.dir",r"C:${/}DL");set_preference("browser.download.lastDir",r"C:${/}DL");set_preference("browser.download.folderList", 2);set_preference("browser.helperApps.alwaysAsk.force", False);set_preference("browser.helperApps.neverAsk.openFile", "application/octet-stream, application/pdf")


*** Test Cases ***
Initial Setup

*** Keywords ***
Initial Setup
    Open Browser    about:blank    Firefox    ff_profile_dir=${PREFERENCES}

However, setting preferences like this isn't enough, the prompt is still there. BUT when I debug the automation suite and within that browser instance change the option manually for PDF files to "Save File": enter image description here

It works. When the option is set, a file named handlers.json gets updated:

{"defaultHandlersVersion":{"en-US":4},"mimeTypes":{"application/pdf":{"action":0,"extensions":["pdf"]}},"schemes":{"irc":{"stubEntry":true,"handlers":[null,{"name":"Mibbit","uriTemplate":"https://www.mibbit.com/?url=%s"}]},"ircs":{"stubEntry":true,"handlers":[null,{"name":"Mibbit","uriTemplate":"https://www.mibbit.com/?url=%s"}]},"mailto":{"stubEntry":true,"handlers":[null,{"name":"Yahoo! Mail","uriTemplate":"https://compose.mail.yahoo.com/?To=%s"},{"name":"Gmail","uriTemplate":"https://mail.google.com/mail/?extsrc=mailto&url=%s"}]}}}

How do I set the value when initializing Firefox using Selenium?

I tried also creating a custom Firefox profile manually, then defined Selenium Library to read that custom profile on Open Browser but this is not a viable option because I don't want to store that custom profile in version control system.

Upvotes: 0

Views: 1403

Answers (1)

Anice Charaf
Anice Charaf

Reputation: 19

Just add to ${PREFERENCES} : set_preference("pdfjs.disabled", True)

Upvotes: 0

Related Questions