Search code examples
seleniumqafwebdrivermanager-java

How Can I set chrome browser to automatically download a pdf using QAF and WebDriverManager


Using a datasheet, I usually pass a browser name into a class I created to select which browser I want to run my tests from. Recently, I've been working on an app in which I need to download a PDF and then verify its contents. I have everything working successfully other than downloading the PDF. With WebDriverManager, it creates a browser profile every time a test runs, and so, I need to update chromeOptions to download PDFs automatically before at the start of the script.

Here is the code that I already have. I just need help with what to put in the prefs for this to work. -

 public static void selectBrowser(String strBrowser) {
    switch (strBrowser) {
        case "Chrome":
                String chromePrefs = "{'goog:chromeOptions':{'prefs':{'profile.default_content_settings.popups':0}}}";
                ConfigurationManager.getBundle().setProperty("chrome.additional.capabilities", chromePrefs);
                TestBaseProvider.instance().get().setDriver("chromeDriver");
                Reporter.log("Chrome Browser was set", MessageTypes.Info);
                break;
        }
}


Solution

  • After researching for hours, finally found that I needed to use the plugins.always_open_pdf_externally preference. Here is the code for anyone who might need it.

    Note, the "goog:" before chromeOptions is necessary since I have WebDriverManager enabled. With 3rd party driver managers, we need to put "goog:" before chromeOptions for it to work.

    You can simply put it in the application.properties like this -

    chrome.additional.capabilities={"goog:chromeOptions":{"args":[--disable-extensions],"prefs":{"plugins.always_open_pdf_externally":true}}}
    

    or you can put it in the code I have up top like this

    String chromePrefs = "{'goog:chromeOptions':{'args':[],'prefs':{\"plugins.always_open_pdf_externally\":true}}}";