Skip to content

ExcludedArguments functionality for Chromium-based drivers #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public AbstractDriverOptions<?> getDriverOptions() {
setChromePrefs(chromeOptions);
setCapabilities(chromeOptions);
setChromeArgs(chromeOptions);
setExcludedArguments(chromeOptions);
chromeOptions.setPageLoadStrategy(getPageLoadStrategy());
setLoggingPreferences(chromeOptions, ChromeOptions.LOGGING_PREFS);
return chromeOptions;
Expand All @@ -42,11 +43,6 @@ private void setChromeArgs(ChromeOptions options) {
for (String arg : getBrowserStartArguments()) {
options.addArguments(arg);
}
// workaround for Selenium issue https://github.com/SeleniumHQ/selenium/issues/11750
final String allowOriginsArgument = "--remote-allow-origins=*";
if (!getBrowserStartArguments().contains(allowOriginsArgument)) {
options.addArguments(allowOriginsArgument);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.chromium.ChromiumOptions;
import org.openqa.selenium.logging.LoggingPreferences;

import java.io.File;
Expand All @@ -25,6 +26,7 @@ abstract class DriverSettings implements IDriverSettings {
private Map<String, Object> capabilities;
private Map<String, Level> loggingPreferences;
private List<String> startArguments;
private List<String> excludedArguments;

protected DriverSettings(ISettingsFile settingsFile) {
this.settingsFile = settingsFile;
Expand Down Expand Up @@ -81,6 +83,23 @@ protected List<String> getBrowserStartArguments() {
return startArguments;
}

protected List<String> getExcludedArguments() {
if (excludedArguments == null) {
String path = getDriverSettingsPath(CapabilityType.EXCLUDED_ARGS);
boolean isValuePresent;
try {
getSettingsFile().getValue(path);
isValuePresent = true;
}
catch (IllegalArgumentException e) {
isValuePresent = false;
}
excludedArguments = isValuePresent ? getSettingsFile().getList(path) : Collections.emptyList();
logCollection("loc.browser.excludedArguments", startArguments);
}
return excludedArguments;
}

protected String getBinaryLocation(String defaultBinaryLocation) {
String value = (String) getSettingsFile().getValueOrDefault(getDriverSettingsPath("binaryLocation"), defaultBinaryLocation);
int varStartIndex = value.indexOf('%');
Expand All @@ -91,7 +110,7 @@ protected String getBinaryLocation(String defaultBinaryLocation) {
}

@SafeVarargs
private final <T> void logCollection(String messageKey, final T... elements) {
private <T> void logCollection(String messageKey, final T... elements) {
if (elements.length == 1 &&
((elements[0] instanceof Map && !((Map)elements[0]).isEmpty())
|| (elements[0] instanceof List && !((List)elements[0]).isEmpty()))) {
Expand Down Expand Up @@ -119,6 +138,10 @@ void setCapabilities(MutableCapabilities options) {
getBrowserCapabilities().forEach(options::setCapability);
}

<T extends ChromiumOptions<T>> void setExcludedArguments(T chromiumOptions) {
chromiumOptions.setExperimentalOption("excludeSwitches", getExcludedArguments());
}

void setLoggingPreferences(MutableCapabilities options, String capabilityKey) {
if (!getLoggingPreferences().isEmpty()) {
LoggingPreferences logs = new LoggingPreferences();
Expand All @@ -143,6 +166,7 @@ private enum CapabilityType {
CAPABILITIES("capabilities"),
OPTIONS("options"),
START_ARGS("startArguments"),
EXCLUDED_ARGS("excludedArguments"),
LOGGING_PREFERENCES("loggingPreferences");

private final String key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public AbstractDriverOptions<?> getDriverOptions() {
setCapabilities(edgeOptions);
setPrefs(edgeOptions);
getBrowserStartArguments().forEach(edgeOptions::addArguments);
setExcludedArguments(edgeOptions);
edgeOptions.setPageLoadStrategy(getPageLoadStrategy());
setLoggingPreferences(edgeOptions, EdgeOptions.LOGGING_PREFS);
return edgeOptions;
Expand Down
12 changes: 8 additions & 4 deletions src/main/resources/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"Performance": "All"
},
"pageLoadStrategy": "Normal",
"startArguments": []
"startArguments": ["--disable-search-engine-choice-screen"],
"excludedArguments": []
},
"edge": {
"capabilities": {
Expand All @@ -36,7 +37,8 @@
"loggingPreferences": {
"Performance": "All"
},
"startArguments": []
"startArguments": [],
"excludedArguments": []
},
"firefox": {
"capabilities": {
Expand Down Expand Up @@ -79,7 +81,8 @@
"download.prompt_for_download": "false",
"download.default_directory": "./downloads"
},
"startArguments": [ "--remote-debugging-port=9222", "--no-sandbox", "--disable-dev-shm-usage" ]
"startArguments": [ "--remote-debugging-port=9222", "--no-sandbox", "--disable-dev-shm-usage" ],
"excludedArguments": []
},
"yandex": {
"binaryLocation": "%USERPROFILE%\\AppData\\Local\\Yandex\\YandexBrowser\\Application\\browser.exe",
Expand All @@ -93,7 +96,8 @@
"download.prompt_for_download": "false",
"download.default_directory": "./downloads"
},
"startArguments": []
"startArguments": [],
"excludedArguments": []
}
},
"timeouts": {
Expand Down
12 changes: 8 additions & 4 deletions src/test/resources/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"loggingPreferences": {
"Performance": "All"
},
"startArguments": []
"startArguments": ["--disable-search-engine-choice-screen"],
"excludedArguments": ["enable-automation"]
},
"edge": {
"capabilities": {
Expand All @@ -35,7 +36,8 @@
"loggingPreferences": {
"Performance": "All"
},
"startArguments": []
"startArguments": [],
"excludedArguments": ["enable-automation"]
},
"firefox": {
"capabilities": {
Expand Down Expand Up @@ -78,7 +80,8 @@
"download.prompt_for_download": "false",
"download.default_directory": "./downloads"
},
"startArguments": [ "--remote-debugging-port=9222", "--no-sandbox", "--disable-dev-shm-usage" ]
"startArguments": [ "--remote-debugging-port=9222", "--no-sandbox", "--disable-dev-shm-usage" ],
"excludedArguments": ["enable-automation"]
},
"yandex": {
"binaryLocation": "%USERPROFILE%\\AppData\\Local\\Yandex\\YandexBrowser\\Application\\browser.exe",
Expand All @@ -92,7 +95,8 @@
"download.prompt_for_download": "false",
"download.default_directory": "./downloads"
},
"startArguments": []
"startArguments": [],
"excludedArguments": ["enable-automation"]
}
},
"timeouts": {
Expand Down