How can I Enable Chrome's "Download PDFs" Option Using Preferences

KSE: Version: 9.5.0, Build 217
Windows 11 Enterprise (64-bit)
Chrome Version 126.0.6478.183 (Official Build) (32-bit)

My use case: Automatically download a page’s PDF file when the user opens a page using Chrome, then programmatically check the PDF for some defined text.

Hello folks, Does anyone know which Katalon Chrome ‘Desired Capabilities’ settings I can use to enable Chrome’s 'Download PDFs" option (PDFs auto download when a page is opened). I’ve see a few suggestions but they have not been clear in their syntax usage. A clear example would be very much appreciated, thank you for any help/pointers.

2 Likes

Hi there,

Thank you very much for your topic. Please note that it may take a little while before a member of our community or from Katalon team responds to you.

Thanks!

Looking forward to the support from other community members. Let’s join hand helping @Dave_Evers

I figured out a solution using Chrome preferences.
First create a custom keyword as below, (see PdfDownloader).
Next call the custom keyword from your test case.

Custom keyword: PdfDownloader

package tools
import java.nio.file.Path
import java.nio.file.Paths
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.configuration.RunConfiguration

public class PdfDownLoader {
	@Keyword
	def setDownloadDirectoryAndPreferences() {
		// Get desired capabilities for WebDriver
		Map desiredCapabilities = RunConfiguration.getDriverPreferencesProperties("WebUI")
		// Set project download folder path
		String projectDownloadFolder = System.getProperty("user.home") + "/Downloads"

		// Create Path objects for project and download folder
		Path projectPath = Paths.get(projectDownloadFolder)
		Path downloadPath = projectPath.resolve(projectPath)

		// Print download path
		println('DownloadPath: ' + downloadPath)

		// Convert download path to string
		def customizedDownloadDirectory = downloadPath.toString()

		// Get existing preferences or create a new map
		Map prefs = desiredCapabilities.get("prefs")
		if (prefs == null) {
			prefs = [:]
		}

		// Set download directory and other preferences
		prefs.put("download.default_directory", customizedDownloadDirectory)
		prefs.put("download_dir", customizedDownloadDirectory)
		prefs.put("download.directory_upgrade", true)
		prefs.put("download.prompt_for_download", false)
		prefs.put("plugins.always_open_pdf_externally", true)
		prefs.put("profile.default_content_settings.popups", "0")
		prefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", "1")

		// Set WebDriver preferences properties
		RunConfiguration.setWebDriverPreferencesProperty("prefs", prefs)
		RunConfiguration.setWebDriverPreferencesProperty("args", [
			"--no-sandbox",
			"--disable-dev-shm-usage"
		])
		RunConfiguration.setWebDriverPreferencesProperty("useAutomationExtension", "false")
	}
}
// Call this keyword before opening a Browser session
// Call using: CustomKeywords.'tools.PdfDownLoader.setDownloadDirectoryAndPreferences'()

Test Case: downloadSamplePdf

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

// Calls the PdfDownloader Keyword
CustomKeywords.'tools.PdfDownLoader.setDownloadDirectoryAndPreferences'()

//Opens the sample pdf
WebUI.openBrowser('https://pdfobject.com/pdf/sample.pdf')

//CustomKeywords.'tools.PdfDownLoader.setDownloadDirectoryAndPreferences'() does the downloading
//once the page is open