From 94834e58de5e86c804476873455a555aadae7daa Mon Sep 17 00:00:00 2001 From: Aliaksej Mialeshka Date: Fri, 26 Jul 2024 01:07:07 +0200 Subject: [PATCH 1/2] Implement excludedArguments functionality for Chromium-based drivers +semver: feature closes #143 Also remove "--remote-allow-origins=*" workaround Add --disable-search-engine-choice-screen to settings.json --- .../driversettings/ChromeSettings.java | 6 +---- .../driversettings/DriverSettings.java | 26 ++++++++++++++++++- .../driversettings/EdgeSettings.java | 1 + src/main/resources/settings.json | 12 ++++++--- src/test/resources/settings.json | 12 ++++++--- 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/main/java/aquality/selenium/configuration/driversettings/ChromeSettings.java b/src/main/java/aquality/selenium/configuration/driversettings/ChromeSettings.java index 61c1477..fafb3c7 100644 --- a/src/main/java/aquality/selenium/configuration/driversettings/ChromeSettings.java +++ b/src/main/java/aquality/selenium/configuration/driversettings/ChromeSettings.java @@ -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; @@ -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 diff --git a/src/main/java/aquality/selenium/configuration/driversettings/DriverSettings.java b/src/main/java/aquality/selenium/configuration/driversettings/DriverSettings.java index 5c804fd..6ff04a8 100644 --- a/src/main/java/aquality/selenium/configuration/driversettings/DriverSettings.java +++ b/src/main/java/aquality/selenium/configuration/driversettings/DriverSettings.java @@ -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; @@ -25,6 +26,7 @@ abstract class DriverSettings implements IDriverSettings { private Map capabilities; private Map loggingPreferences; private List startArguments; + private List excludedArguments; protected DriverSettings(ISettingsFile settingsFile) { this.settingsFile = settingsFile; @@ -81,6 +83,23 @@ protected List getBrowserStartArguments() { return startArguments; } + protected List 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('%'); @@ -91,7 +110,7 @@ protected String getBinaryLocation(String defaultBinaryLocation) { } @SafeVarargs - private final void logCollection(String messageKey, final T... elements) { + private 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()))) { @@ -119,6 +138,10 @@ void setCapabilities(MutableCapabilities options) { getBrowserCapabilities().forEach(options::setCapability); } + > void setExcludedArguments(T chromiumOptions) { + chromiumOptions.setExperimentalOption("excludeSwitches", getExcludedArguments()); + } + void setLoggingPreferences(MutableCapabilities options, String capabilityKey) { if (!getLoggingPreferences().isEmpty()) { LoggingPreferences logs = new LoggingPreferences(); @@ -143,6 +166,7 @@ private enum CapabilityType { CAPABILITIES("capabilities"), OPTIONS("options"), START_ARGS("startArguments"), + EXCLUDED_ARGS("excludedArguments"), LOGGING_PREFERENCES("loggingPreferences"); private final String key; diff --git a/src/main/java/aquality/selenium/configuration/driversettings/EdgeSettings.java b/src/main/java/aquality/selenium/configuration/driversettings/EdgeSettings.java index c0e9f1a..2d299b1 100644 --- a/src/main/java/aquality/selenium/configuration/driversettings/EdgeSettings.java +++ b/src/main/java/aquality/selenium/configuration/driversettings/EdgeSettings.java @@ -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; diff --git a/src/main/resources/settings.json b/src/main/resources/settings.json index 7e13b26..6222213 100644 --- a/src/main/resources/settings.json +++ b/src/main/resources/settings.json @@ -20,7 +20,8 @@ "Performance": "All" }, "pageLoadStrategy": "Normal", - "startArguments": [] + "startArguments": ["--disable-search-engine-choice-screen"], + "excludedArguments": [] }, "edge": { "capabilities": { @@ -36,7 +37,8 @@ "loggingPreferences": { "Performance": "All" }, - "startArguments": [] + "startArguments": [], + "excludedArguments": [] }, "firefox": { "capabilities": { @@ -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", @@ -93,7 +96,8 @@ "download.prompt_for_download": "false", "download.default_directory": "./downloads" }, - "startArguments": [] + "startArguments": [], + "excludedArguments": [] } }, "timeouts": { diff --git a/src/test/resources/settings.json b/src/test/resources/settings.json index cf0ac09..966eaf2 100644 --- a/src/test/resources/settings.json +++ b/src/test/resources/settings.json @@ -19,7 +19,8 @@ "loggingPreferences": { "Performance": "All" }, - "startArguments": [] + "startArguments": ["--disable-search-engine-choice-screen"], + "excludedArguments": ["enable-automation"] }, "edge": { "capabilities": { @@ -35,7 +36,8 @@ "loggingPreferences": { "Performance": "All" }, - "startArguments": [] + "startArguments": [], + "excludedArguments": ["enable-automation"] }, "firefox": { "capabilities": { @@ -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", @@ -92,7 +95,8 @@ "download.prompt_for_download": "false", "download.default_directory": "./downloads" }, - "startArguments": [] + "startArguments": [], + "excludedArguments": ["enable-automation"] } }, "timeouts": { From fedfa8d044d72d298aaebeacaa2a4448d395a81f Mon Sep 17 00:00:00 2001 From: Aliaksej Mialeshka Date: Fri, 26 Jul 2024 01:23:12 +0200 Subject: [PATCH 2/2] Fix loc message for "excludedArguments" --- .../selenium/configuration/driversettings/DriverSettings.java | 2 +- src/main/resources/localization/be.json | 1 + src/main/resources/localization/en.json | 1 + src/main/resources/localization/pl.json | 1 + src/main/resources/localization/ru.json | 1 + src/main/resources/localization/uk.json | 1 + 6 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/aquality/selenium/configuration/driversettings/DriverSettings.java b/src/main/java/aquality/selenium/configuration/driversettings/DriverSettings.java index 6ff04a8..d6403a4 100644 --- a/src/main/java/aquality/selenium/configuration/driversettings/DriverSettings.java +++ b/src/main/java/aquality/selenium/configuration/driversettings/DriverSettings.java @@ -95,7 +95,7 @@ protected List getExcludedArguments() { isValuePresent = false; } excludedArguments = isValuePresent ? getSettingsFile().getList(path) : Collections.emptyList(); - logCollection("loc.browser.excludedArguments", startArguments); + logCollection("loc.browser.excludedArguments", excludedArguments); } return excludedArguments; } diff --git a/src/main/resources/localization/be.json b/src/main/resources/localization/be.json index b52cfab..c473f0a 100644 --- a/src/main/resources/localization/be.json +++ b/src/main/resources/localization/be.json @@ -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", diff --git a/src/main/resources/localization/en.json b/src/main/resources/localization/en.json index 53b7759..421444d 100644 --- a/src/main/resources/localization/en.json +++ b/src/main/resources/localization/en.json @@ -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", diff --git a/src/main/resources/localization/pl.json b/src/main/resources/localization/pl.json index f317ebc..e0bc88d 100644 --- a/src/main/resources/localization/pl.json +++ b/src/main/resources/localization/pl.json @@ -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", diff --git a/src/main/resources/localization/ru.json b/src/main/resources/localization/ru.json index 51f5822..bc7e2ad 100644 --- a/src/main/resources/localization/ru.json +++ b/src/main/resources/localization/ru.json @@ -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", diff --git a/src/main/resources/localization/uk.json b/src/main/resources/localization/uk.json index db4cb16..3e3bf07 100644 --- a/src/main/resources/localization/uk.json +++ b/src/main/resources/localization/uk.json @@ -1,5 +1,6 @@ { "loc.browser.arguments": "Отримані аргументи запуску браузера з файлу налаштувань: %s", + "loc.browser.excludedArguments": "Отримані виключені аргументи браузера з файлу налаштувань: %s", "loc.browser.back": "Повертання до попередньої сторінки", "loc.browser.forward": "Перехід до наступної сторінки", "loc.browser.capabilities": "Отримані можливості браузера з файлу налаштувань: %s",