How to run a specific firefox profile headless

I have the following code to use a specific Firefox profile (controlled with the variable useFireFoxProfile). These profiles have specific selected certificates to login in our application. When running this in normal Firefox it works great, however when running Firefox headless, it looks like it doesn’t open the specific Firefox profile.
I’ve taken a screenshot and it looks like its stuck precisely on the spot where it would normaly would open the certificate selection popup.

import com.kms.katalon.core.configuration.RunConfiguration as RC
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.firefox.FirefoxDriver as FirefoxDriver
import org.openqa.selenium.firefox.FirefoxOptions as FirefoxOptions
import org.openqa.selenium.firefox.FirefoxProfile as FirefoxProfile
import org.openqa.selenium.firefox.ProfilesIni as ProfilesIni
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.util.KeywordUtil as KeywordUtil
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.driver.WebUIDriverType as WebUIDriverType
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint

// Start browser
WebUIDriverType executedBrowser = DriverFactory.getExecutedBrowser()

firefoxUsed = false

switch (executedBrowser) {
    // If browser if FireFox
    case WebUIDriverType.FIREFOX_DRIVER:
        // Set Firefox profile to be used
        
        ProfilesIni profile = new ProfilesIni()

        FirefoxProfile FF = profile.getProfile(useFireFoxProfile)

        FirefoxOptions options = new FirefoxOptions().setProfile(FF)
		.addPreference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/zip, application/octet-stream, application/x-zip-compressed, multipart/x-zip')
		//.addPreference('browser.link.open_newwindow', 3)

        System.setProperty('webdriver.gecko.driver', DriverFactory.getGeckoDriverPath())

		WebDriver driver = new FirefoxDriver(options)

        DriverFactory.changeWebDriver(driver)

        firefoxUsed = true

        break // Other browsers
    default:
        WebUI.openBrowser('')}

// Log test run details
def executionProfile = RC.getExecutionProfile()

WebUI.comment("executionProfile = $executionProfile")

def projectDir = RC.getProjectDir()

WebUI.comment("projectDir = $projectDir")

def reportFolder = RC.getReportFolder()

WebUI.comment("reportFolder = $reportFolder")

// Mention Firefox profile is test is performed by Firefox.
if (firefoxUsed) {
    KeywordUtil.markPassed(('- - - - - - - - - - - - - - - Test continues with profile: ' + useFireFoxProfile) + ' - - - - - - - - - - - - - - -')
}

WebUI.maximizeWindow()
1 Like

See

https://api-docs.katalon.com/com/kms/katalon/core/webui/driver/WebUIDriverType.html

2 Likes

@kazurayam Thank you for this tip!
I’ve added a switch case for the headless driver and added an argument for headless and now it works!

case WebUIDriverType.FIREFOX_HEADLESS_DRIVER:
		// Set Firefox profile to be used
		
		ProfilesIni profile = new ProfilesIni()
	
		FirefoxProfile FF = profile.getProfile(useFireFoxProfile)
	
		FirefoxOptions options = new FirefoxOptions().setProfile(FF)
		.addPreference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/zip, application/octet-stream, application/x-zip-compressed, multipart/x-zip')
		.addArguments('--headless')
	
		System.setProperty('webdriver.gecko.driver', DriverFactory.getGeckoDriverPath())

		WebDriver driver = new FirefoxDriver(options)
	
		DriverFactory.changeWebDriver(driver)
	
		firefoxUsed = true
1 Like