Skip to content

Commit

Permalink
Merge pull request #193 from Xceptance/#192-support-preferences
Browse files Browse the repository at this point in the history
#192 Support passing preferences into browser configurations
  • Loading branch information
wurzelkuchen committed Apr 5, 2024
2 parents 3ceb778 + 7ce193d commit d94fc9b
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 9 deletions.
6 changes: 5 additions & 1 deletion config/browser.properties
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@
# .arguments: Additional command line arguments for the browser to apply.
# As you can specify only on 'arguments' property for a browser at a time you need to chain multiple arguments.
# Multiple arguments are chained by semicolon (";") e.g.: `-window-position=0,0 ; -window-size=400,300`

#
# .preferences: Global browser preferences for Chrome and Firefox
# As you can specify only on 'preferences' property for a browser at a time you need to chain multiple preferences.
# Multiple preferences are chained by semicolon (";") e.g.: `homepage=https://www.xceptance.com ; geolocation.enabled=true ; renderer.memory_cache.size=120000`
#
# .downloadDirectory: browsers default download directory

################################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,27 @@ else if (Neodymium.configuration().useProxy())
options.addArguments(config.getArguments());
}

if (StringUtils.isNotBlank(config.getDownloadDirectory()))

if (config.getPreferences() != null && !config.getPreferences().isEmpty())
{
options.setExperimentalOption("prefs", config.getPreferences());
}

if ((config.getPreferences() != null && !config.getPreferences().isEmpty()) || StringUtils.isNotBlank(config.getDownloadDirectory()))
{
HashMap<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", config.getDownloadDirectory());
prefs.put("plugins.always_open_pdf_externally", true);

// if we have configured prefs, we need to add all to the experimental options
if(config.getPreferences() != null && !config.getPreferences().isEmpty())
{
prefs.putAll(config.getPreferences());
}
// if we have configured a download folder separately, it'll override the general config
if(StringUtils.isNotBlank(config.getDownloadDirectory()))
{
prefs.put("download.default_directory", config.getDownloadDirectory());
prefs.put("plugins.always_open_pdf_externally", true);
}

options.setExperimentalOption("prefs", prefs);
}
Expand All @@ -253,15 +269,45 @@ else if (firefoxBrowsers.contains(browserName))
options.addArguments(config.getArguments());
}
if (StringUtils.isNotBlank(config.getDownloadDirectory()))
{
FirefoxProfile profile = new FirefoxProfile();
}

if ((config.getPreferences() != null && !config.getPreferences().isEmpty()) || StringUtils.isNotBlank(config.getDownloadDirectory()))
{
FirefoxProfile profile = new FirefoxProfile();

profile.setPreference("browser.download.dir", config.getDownloadDirectory());
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", popularContentTypes());
profile.setPreference("pdfjs.disabled", true);
profile.setPreference("browser.download.folderList", 2);
// if we have configured prefs, we need to add all to the experimental options
if (config.getPreferences() != null && !config.getPreferences().isEmpty())
{
// differentiate types of preference values to avoid misunderstanding
config.getPreferences().forEach((key, val) -> {
if (val.equals("true") || val.equals("false"))
{
profile.setPreference(key, Boolean.parseBoolean(val.toString()));
}
else if (StringUtils.isNumeric(val.toString()))
{
profile.setPreference(key, Integer.parseInt(val.toString()));
}
else
{
profile.setPreference(key, val.toString());
}
});
}

// if we have configured a download folder separately, it'll override the general config
if (StringUtils.isNotBlank(config.getDownloadDirectory()))
{
profile.setPreference("browser.download.dir", config.getDownloadDirectory());
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", popularContentTypes());
profile.setPreference("pdfjs.disabled", true);
profile.setPreference("browser.download.folderList", 2);
}
options.setProfile(profile);
}

wDSC.setWebDriver(new FirefoxDriver(new GeckoDriverService.Builder().withAllowHosts("localhost").build(), options));
}
else if (internetExplorerBrowsers.contains(browserName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openqa.selenium.MutableCapabilities;

Expand Down Expand Up @@ -30,6 +31,8 @@ public class BrowserConfiguration

private List<String> arguments;

private Map<String, Object> preferences;

private String downloadDirectory;

/**
Expand Down Expand Up @@ -222,6 +225,20 @@ public void setArguments(List<String> arguments)
this.arguments = arguments;
}

public Map<String, Object> getPreferences()
{
return preferences;
}

public void addPreference(String key, Object val)
{
if (this.preferences == null)
{
this.preferences = new HashMap<String, Object>();
}
this.preferences.put(key, val);
}

public String getDownloadDirectory()
{
return downloadDirectory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class BrowserConfigurationMapper

private static final String ARGUMENTS = "arguments";

private static final String PREFERENCES = "preferences";

private static final String DOWNLOAD_DIRECTORY = "downloadDirectory";

// Appium specific properties
Expand Down Expand Up @@ -129,6 +131,7 @@ else if ("edge".equals(emulatedBrowser))
}

String emulatedPlatformName = browserProfileConfiguration.get(PLATFORM_VERSION);

if (!StringUtils.isEmpty(emulatedPlatformName))
{
testEnvironmentProperties.put("osVersion", emulatedPlatformName);
Expand Down Expand Up @@ -296,14 +299,43 @@ else if (!StringUtils.isEmpty(globalHeadless))
if (!StringUtils.isEmpty(arguments))
{
List<String> args = new LinkedList<>();

for (String arg : arguments.split(";"))
{
// cut off trailing/leading whitespace because the browsers can't handle it
args.add(arg.trim());
}
browserConfiguration.setArguments(args);
}

// additional browser preferences
String preferences = browserProfileConfiguration.get(PREFERENCES);
if (!StringUtils.isEmpty(preferences))
{
for (String pref : preferences.split(";"))
{
String[] keyVal = pref.split("=");
if (pref.length() > 1)
{
String key = keyVal[0].trim();
String val = keyVal[1].trim();

// differentiate types of preference values to avoid misunderstanding
if (val.equals("true") | val.equals("false"))
{
browserConfiguration.addPreference(key, Boolean.parseBoolean(val));
}
else if (StringUtils.isNumeric(val))
{
browserConfiguration.addPreference(key, Integer.parseInt(val));
}
else
{
browserConfiguration.addPreference(key, val);
}
}
}
}

String downloadDirectory = browserProfileConfiguration.get(DOWNLOAD_DIRECTORY);
if (!StringUtils.isEmpty(downloadDirectory))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,19 @@ public static void beforeClass() throws IOException
properties.put("browserprofile.chrome.testEnvironment", "local");
properties.put("browserprofile.chrome.acceptInsecureCertificates", "true");
properties.put("browserprofile.chrome.arguments", "headless");

properties.put("browserprofile.chrome.preferences",
"homepage=https://www.xceptance.com ; geolocation.enabled=true ; renderer.memory_cache.size=120000");
properties.put("browserprofile.chrome.downloadDirectory", "target");


properties.put("browserprofile.firefox.name", "Mozilla Firefox");
properties.put("browserprofile.firefox.browser", "firefox");
properties.put("browserprofile.firefox.arguments", "headless");

properties.put("browserprofile.firefox.preferences",
"media.navigator.permission.disabled=true ; browser.startup.homepage=https://www.xceptance.com ; app.update.backgroundMaxErrors=1");

properties.put("browserprofile.firefox.downloadDirectory", "target");

properties.put("browserprofile.multiFirefox.name", "Multi Argument Firefox");
Expand Down Expand Up @@ -327,6 +335,13 @@ private void checkChrome(BrowserConfiguration config)
LinkedList<String> list = new LinkedList<>();
list.add("headless");
Assert.assertEquals(list, config.getArguments());

HashMap<String, Object> prefs = new HashMap<>();
prefs.put("geolocation.enabled", true);
prefs.put("renderer.memory_cache.size", 120000);
prefs.put("homepage", "https://www.xceptance.com");
Assert.assertEquals(prefs, config.getPreferences());

Assert.assertEquals(new File("target").getAbsolutePath(), config.getDownloadDirectory());
}

Expand Down Expand Up @@ -355,6 +370,12 @@ private void checkFirefox(BrowserConfiguration config)
LinkedList<String> list = new LinkedList<>();
list.add("headless");
Assert.assertEquals(list, config.getArguments());

HashMap<String, Object> prefs = new HashMap<>();
prefs.put("media.navigator.permission.disabled", true);
prefs.put("app.update.backgroundMaxErrors", 1);
prefs.put("browser.startup.homepage", "https://www.xceptance.com");
Assert.assertEquals(prefs, config.getPreferences());
Assert.assertEquals(new File("target").getAbsolutePath(), config.getDownloadDirectory());
}

Expand Down

0 comments on commit d94fc9b

Please sign in to comment.