CustomKeyword for Automatically Downloading PDFs in a Chrome Browser

This CustomKeyword enables a Katalon launched Chrome Browser to automatically download PDFs to a user’s Downloads folder. This keyword would be useful for those use cases where there is no download button to trigger a PDF download. For example, after this keyword is triggered opening https://pdfobject.com/pdf/sample.pdf would automatically download the pdf.

image

PdfDownLoader CustomKeyword

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")
	}
}

// This CustomKeyword setups up a Katalon launched Chrome Browser to auto download PDFs to the users Downloads folder.
// Place the following in your test case before executing WebUI.openBrowser('').
// CustomKeywords.'tools.PdfDownLoader.setDownloadDirectoryAndPreferences'()

3 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!

Happy Birthday Love GIF by Mo Willems Workshop

Thank you for sharing this tip. It is very interesting.