Skip to content
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

ExcludedArguments functionality for Chromium-based drivers #144

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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", excludedArguments);
}
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
1 change: 1 addition & 0 deletions src/main/resources/localization/be.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"loc.browser.arguments": "Атрымалі стартавыя аргументы браўзэра з settings файла: %s",
"loc.browser.excludedArguments": "Атрымалі выключаныя аргументы браўзэра з settings файла: %s",
"loc.browser.back": "Вяртаемся да папярэдняй старонкі",
"loc.browser.forward": "Пераходзім да наступнай старонкі",
"loc.browser.capabilities": "Атрымалі capabilities браўзэра з settings файла: %s",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/localization/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"loc.browser.arguments": "Got browser start arguments from settings file: %s",
"loc.browser.excludedArguments": "Got excluded browser arguments from settings file: %s",
"loc.browser.back": "Return to previous page",
"loc.browser.forward": "Proceed to the next page",
"loc.browser.capabilities": "Got browser capabilities from settings file: %s",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/localization/pl.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"loc.browser.arguments": "Pobrano argumenty uruchamiania przeglądarki z pliku ustawień: %s",
"loc.browser.excludedArguments": "Pobrano wykluczone argumenty przeglądarki z pliku ustawień: %s",
"loc.browser.back": "Powrót do poprzedniej strony",
"loc.browser.forward": "Przejście do następnej strony",
"loc.browser.capabilities": "Pobrano możliwości przeglądarki z pliku ustawień: %s",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/localization/ru.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"loc.browser.arguments": "Получили стартовые аргументы браузера из settings файла: %s",
"loc.browser.excludedArguments": "Получили исключённые аргументы браузера из settings файла: %s",
"loc.browser.back": "Возврат на предыдущую страницу",
"loc.browser.forward": "Перейти на следующую страницу",
"loc.browser.capabilities": "Получили capabilities браузера из settings файла: %s",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/localization/uk.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"loc.browser.arguments": "Отримані аргументи запуску браузера з файлу налаштувань: %s",
"loc.browser.excludedArguments": "Отримані виключені аргументи браузера з файлу налаштувань: %s",
"loc.browser.back": "Повертання до попередньої сторінки",
"loc.browser.forward": "Перехід до наступної сторінки",
"loc.browser.capabilities": "Отримані можливості браузера з файлу налаштувань: %s",
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
Loading