From fdd38c44b3ae03ddf228820dac78ad68ff5f28c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Barto=C5=A1?= Date: Thu, 4 Nov 2021 19:10:17 +0100 Subject: [PATCH] Refactoring - Change Browser Type in test (#110) Co-authored-by: RBRi --- .../htmlunit/BrowserVersionDeterminer.java | 23 ++++---- .../java/org/openqa/selenium/AlertsTest.java | 6 ++- .../openqa/selenium/ClickScrollingTest.java | 6 +-- .../java/org/openqa/selenium/TypingTest.java | 6 +-- .../openqa/selenium/WindowSwitchingTest.java | 8 +-- .../htmlunit/HtmlUnitCapabilitiesTest.java | 23 +++----- .../selenium/htmlunit/HtmlUnitDriverTest.java | 38 ++++++------- .../selenium/htmlunit/HtmlUnitProxyTest.java | 6 +-- .../selenium/htmlunit/WebDriverTestCase.java | 12 ++--- .../interactions/DragAndDropTest.java | 4 +- .../selenium/testing/JUnit4TestBase.java | 20 +++---- .../selenium/testing/SeleniumTestRunner.java | 10 ++-- .../drivers/BrowserToCapabilities.java | 53 ++++++++++--------- .../{Browser.java => BrowserType.java} | 48 +++++++++++------ .../testing/drivers/GridSupplier.java | 3 +- .../ReflectionBackedDriverSupplier.java | 16 +++--- .../selenium/testing/drivers/SauceDriver.java | 4 +- .../testing/drivers/TestChromeDriver.java | 2 +- .../testing/drivers/TestIgnorance.java | 50 ++++++++--------- .../drivers/TestInternetExplorerSupplier.java | 5 +- .../testing/drivers/WebDriverBuilder.java | 8 +-- 21 files changed, 177 insertions(+), 174 deletions(-) rename src/test/java/org/openqa/selenium/testing/drivers/{Browser.java => BrowserType.java} (53%) diff --git a/src/main/java/org/openqa/selenium/htmlunit/BrowserVersionDeterminer.java b/src/main/java/org/openqa/selenium/htmlunit/BrowserVersionDeterminer.java index 1212573e..bb37ef26 100644 --- a/src/main/java/org/openqa/selenium/htmlunit/BrowserVersionDeterminer.java +++ b/src/main/java/org/openqa/selenium/htmlunit/BrowserVersionDeterminer.java @@ -19,7 +19,7 @@ import com.gargoylesoftware.htmlunit.BrowserVersion; import org.openqa.selenium.Capabilities; -import org.openqa.selenium.remote.BrowserType; +import org.openqa.selenium.remote.Browser; import java.util.ArrayList; import java.util.List; @@ -41,10 +41,9 @@ public class BrowserVersionDeterminer { * Determine browser by its capabilities */ public static BrowserVersion determine(Capabilities capabilities) { - String capBrowserName = capabilities.getBrowserName(); - if (!BrowserType.HTMLUNIT.equals(capBrowserName)) { + if (!Browser.HTMLUNIT.is(capabilities)) { throw new IllegalArgumentException("When building an HtmlUntDriver, the capability browser name must be set to '" - + BrowserType.HTMLUNIT + "' but was '" + capBrowserName + "'."); + + Browser.HTMLUNIT.browserName() + "' but was '" + capabilities.getBrowserName() + "'."); } String browserName; @@ -66,7 +65,7 @@ public static BrowserVersion determine(Capabilities capabilities) { final BrowserVersion result = browsers.stream() .filter(Objects::nonNull) - .filter(item -> browserName.equals(item.getBrowserType())) + .filter(item -> item.getBrowser().is(browserName)) .findFirst() .map(item -> item.getBrowserVersion(browserVersion)) .orElse(BrowserVersion.getDefault()); @@ -84,7 +83,7 @@ public static BrowserVersion determine(Capabilities capabilities) { * Basic browser info */ protected interface BrowserInfo { - String getBrowserType(); + Browser getBrowser(); BrowserVersion getBrowserVersion(); @@ -95,8 +94,8 @@ default BrowserVersion getBrowserVersion(String versionNumeric) { protected static class Chrome implements BrowserInfo { @Override - public String getBrowserType() { - return BrowserType.CHROME; + public Browser getBrowser() { + return Browser.CHROME; } @Override @@ -107,8 +106,8 @@ public BrowserVersion getBrowserVersion() { protected static class IE implements BrowserInfo { @Override - public String getBrowserType() { - return BrowserType.IE; + public Browser getBrowser() { + return Browser.IE; } @Override @@ -119,8 +118,8 @@ public BrowserVersion getBrowserVersion() { protected static class Firefox implements BrowserInfo { @Override - public String getBrowserType() { - return BrowserType.FIREFOX; + public Browser getBrowser() { + return Browser.FIREFOX; } @Override diff --git a/src/test/java/org/openqa/selenium/AlertsTest.java b/src/test/java/org/openqa/selenium/AlertsTest.java index 18310006..b312c01c 100644 --- a/src/test/java/org/openqa/selenium/AlertsTest.java +++ b/src/test/java/org/openqa/selenium/AlertsTest.java @@ -43,7 +43,7 @@ import org.junit.Test; import org.openqa.selenium.environment.webserver.Page; import org.openqa.selenium.htmlunit.HtmlUnitDriver; -import org.openqa.selenium.remote.BrowserType; +import org.openqa.selenium.remote.Browser; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.testing.Ignore; @@ -54,6 +54,8 @@ import org.openqa.selenium.testing.SwitchToTopAfterTest; import com.gargoylesoftware.htmlunit.BrowserVersion; +import org.openqa.selenium.testing.drivers.BrowserToCapabilities; +import org.openqa.selenium.testing.drivers.BrowserType; @Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=1500") public class AlertsTest extends JUnit4TestBase { @@ -633,7 +635,7 @@ public void browserVersionAndBooleanConstructor() { @Test(expected = UnhandledAlertException.class) public void capabilitiesConstructor() { - HtmlUnitDriver driver = new HtmlUnitDriver(new DesiredCapabilities(BrowserType.HTMLUNIT, null, Platform.ANY)); + HtmlUnitDriver driver = new HtmlUnitDriver(BrowserToCapabilities.of(BrowserType.HTML_UNIT)); driver.get(alertPage("cheese")); driver.findElement(By.id("alert")).click(); diff --git a/src/test/java/org/openqa/selenium/ClickScrollingTest.java b/src/test/java/org/openqa/selenium/ClickScrollingTest.java index 30943b10..d5f080fe 100644 --- a/src/test/java/org/openqa/selenium/ClickScrollingTest.java +++ b/src/test/java/org/openqa/selenium/ClickScrollingTest.java @@ -41,7 +41,7 @@ import org.openqa.selenium.testing.JUnit4TestBase; import org.openqa.selenium.testing.NotYetImplemented; import org.openqa.selenium.testing.SwitchToTopAfterTest; -import org.openqa.selenium.testing.drivers.Browser; +import org.openqa.selenium.testing.drivers.BrowserType; @Ignore(value = HTMLUNIT, reason = "Scrolling requires rendering") public class ClickScrollingTest extends JUnit4TestBase { @@ -227,10 +227,10 @@ private void onlyPassIfNotOnMac(int mozIssue, Runnable toCheck) { toCheck.run(); assumeFalse( "It appears https://github.com/mozilla/geckodriver/issues/" + mozIssue + " is fixed", - Platform.getCurrent() == Platform.MAC && Browser.detect() == Browser.ff); + Platform.getCurrent() == Platform.MAC && BrowserType.detect() == BrowserType.FIREFOX); } catch (Throwable e) { // Swallow the exception, as this is expected for Firefox on OS X - if (!(Platform.getCurrent() == Platform.MAC && Browser.detect() == Browser.ff)) { + if (!(Platform.getCurrent() == Platform.MAC && BrowserType.detect() == BrowserType.FIREFOX)) { throw e; } } diff --git a/src/test/java/org/openqa/selenium/TypingTest.java b/src/test/java/org/openqa/selenium/TypingTest.java index c3b7f0f5..1869c7a2 100644 --- a/src/test/java/org/openqa/selenium/TypingTest.java +++ b/src/test/java/org/openqa/selenium/TypingTest.java @@ -40,7 +40,7 @@ import org.openqa.selenium.testing.Ignore; import org.openqa.selenium.testing.JUnit4TestBase; import org.openqa.selenium.testing.NotYetImplemented; -import org.openqa.selenium.testing.drivers.Browser; +import org.openqa.selenium.testing.drivers.BrowserType; public class TypingTest extends JUnit4TestBase { @@ -254,7 +254,7 @@ private static void checkRecordedKeySequence(WebElement element, int expectedKey @Test @Ignore(IE) public void testShouldReportKeyCodeOfArrowKeys() { - assumeFalse(Browser.detect() == Browser.opera && + assumeFalse(BrowserType.detect() == BrowserType.OPERA && getEffectivePlatform().is(Platform.WINDOWS)); driver.get(pages.javascriptPage); @@ -280,7 +280,7 @@ public void testShouldReportKeyCodeOfArrowKeys() { @Test public void testShouldReportKeyCodeOfArrowKeysUpDownEvents() { - assumeFalse(Browser.detect() == Browser.opera && + assumeFalse(BrowserType.detect() == BrowserType.OPERA && getEffectivePlatform().is(Platform.WINDOWS)); driver.get(pages.javascriptPage); diff --git a/src/test/java/org/openqa/selenium/WindowSwitchingTest.java b/src/test/java/org/openqa/selenium/WindowSwitchingTest.java index 633f2d57..dcbf1947 100644 --- a/src/test/java/org/openqa/selenium/WindowSwitchingTest.java +++ b/src/test/java/org/openqa/selenium/WindowSwitchingTest.java @@ -45,7 +45,7 @@ import org.openqa.selenium.testing.NotYetImplemented; import org.openqa.selenium.testing.SwitchToTopAfterTest; import org.openqa.selenium.testing.TestUtilities; -import org.openqa.selenium.testing.drivers.Browser; +import org.openqa.selenium.testing.drivers.BrowserType; import java.util.Set; import java.util.stream.Collectors; @@ -81,7 +81,7 @@ protected void finished(Description description) { @NoDriverAfterTest(failedOnly = true) @Test public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOperations() { - assumeFalse(Browser.detect() == Browser.opera && + assumeFalse(BrowserType.detect() == BrowserType.OPERA && TestUtilities.getEffectivePlatform().is(Platform.WINDOWS)); driver.get(pages.xhtmlTestPage); @@ -193,7 +193,7 @@ public void testShouldBeAbleToIterateOverAllOpenWindows() { @NotYetImplemented(SAFARI) public void testClickingOnAButtonThatClosesAnOpenWindowDoesNotCauseTheBrowserToHang() throws Exception { - assumeFalse(Browser.detect() == Browser.opera && + assumeFalse(BrowserType.detect() == BrowserType.OPERA && TestUtilities.getEffectivePlatform().is(Platform.WINDOWS)); driver.get(pages.xhtmlTestPage); @@ -225,7 +225,7 @@ public void testClickingOnAButtonThatClosesAnOpenWindowDoesNotCauseTheBrowserToH @Test @NotYetImplemented(SAFARI) public void testCanCallGetWindowHandlesAfterClosingAWindow() throws Exception { - assumeFalse(Browser.detect() == Browser.opera && + assumeFalse(BrowserType.detect() == BrowserType.OPERA && TestUtilities.getEffectivePlatform().is(Platform.WINDOWS)); driver.get(pages.xhtmlTestPage); diff --git a/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitCapabilitiesTest.java b/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitCapabilitiesTest.java index a1e362e3..a94e41b1 100644 --- a/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitCapabilitiesTest.java +++ b/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitCapabilitiesTest.java @@ -29,10 +29,9 @@ import org.junit.Test; import org.openqa.selenium.Capabilities; -import org.openqa.selenium.Platform; -import org.openqa.selenium.remote.BrowserType; import org.openqa.selenium.remote.DesiredCapabilities; - +import org.openqa.selenium.testing.drivers.BrowserToCapabilities; +import org.openqa.selenium.testing.drivers.BrowserType; import com.gargoylesoftware.htmlunit.BrowserVersion; /** @@ -42,7 +41,7 @@ public class HtmlUnitCapabilitiesTest { @Test public void configurationViaDirectCapabilities() { - DesiredCapabilities ieCapabilities = new DesiredCapabilities(BrowserType.IE, "", Platform.ANY); + DesiredCapabilities ieCapabilities = BrowserToCapabilities.of(BrowserType.IE); try { BrowserVersionDeterminer.determine(ieCapabilities); @@ -56,31 +55,25 @@ public void configurationViaDirectCapabilities() { @Test public void configurationOfFirefoxDefaultViaRemote() { - DesiredCapabilities firefoxCapabilities = - new DesiredCapabilities(BrowserType.HTMLUNIT, "firefox", Platform.ANY); - + DesiredCapabilities firefoxCapabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "firefox"); assertEquals(FIREFOX, BrowserVersionDeterminer.determine(firefoxCapabilities)); } @Test public void configurationOfFirefox78ViaRemote() { - DesiredCapabilities firefoxCapabilities = - new DesiredCapabilities(BrowserType.HTMLUNIT, "firefox-78", Platform.ANY); - + DesiredCapabilities firefoxCapabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "firefox-78"); assertEquals(FIREFOX_78, BrowserVersionDeterminer.determine(firefoxCapabilities)); } @Test public void configurationOfIEViaRemote() { - DesiredCapabilities ieCapabilities = - new DesiredCapabilities(BrowserType.HTMLUNIT, "internet explorer", Platform.ANY); - + DesiredCapabilities ieCapabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "internet explorer"); assertEquals(INTERNET_EXPLORER, BrowserVersionDeterminer.determine(ieCapabilities)); } @Test public void tetsDefautlBrowserVersion() { - DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT); assertEquals(BrowserVersion.getDefault(), BrowserVersionDeterminer.determine(capabilities)); } @@ -101,7 +94,7 @@ public void htmlUnitReportsCapabilities() { public void configurationOfBrowserLanguage() { String browserLanguage = "es-ES"; - DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT); capabilities.setCapability(BROWSER_LANGUAGE_CAPABILITY, browserLanguage); assertEquals(browserLanguage, BrowserVersionDeterminer.determine(capabilities).getBrowserLanguage()); diff --git a/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitDriverTest.java b/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitDriverTest.java index 593a2597..66338950 100644 --- a/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitDriverTest.java +++ b/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitDriverTest.java @@ -24,12 +24,14 @@ import org.junit.Test; import org.openqa.selenium.Proxy; import org.openqa.selenium.WebDriver; -import org.openqa.selenium.remote.BrowserType; +import org.openqa.selenium.remote.Browser; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.WebClient; +import org.openqa.selenium.testing.drivers.BrowserToCapabilities; +import org.openqa.selenium.testing.drivers.BrowserType; public class HtmlUnitDriverTest { @@ -228,7 +230,7 @@ protected WebClient modifyWebClient(WebClient client){ @Test public void ctorWebClientCapabilitiesJsTrue() { - DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT); capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true); new HtmlUnitDriver(capabilities){ @@ -247,7 +249,7 @@ protected WebClient modifyWebClient(WebClient client){ @Test public void ctorWebClientCapabilitiesJsFalse() { - DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT); capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, false); new HtmlUnitDriver(capabilities){ @@ -266,8 +268,7 @@ protected WebClient modifyWebClient(WebClient client){ @Test public void ctorWebClientCapabilitiesVersionString() { - DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); - capabilities.setVersion("firefox"); + DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "firefox"); new HtmlUnitDriver(capabilities){ @Override @@ -278,8 +279,7 @@ protected WebClient modifyWebClient(WebClient client){ } }; - capabilities = DesiredCapabilities.htmlUnit(); - capabilities.setVersion("googlechrome"); + capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "googlechrome"); new HtmlUnitDriver(capabilities){ @Override @@ -290,8 +290,7 @@ protected WebClient modifyWebClient(WebClient client){ } }; - capabilities = DesiredCapabilities.htmlUnit(); - capabilities.setVersion("MicrosoftEdge"); + capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "MicrosoftEdge"); new HtmlUnitDriver(capabilities){ @Override @@ -302,8 +301,7 @@ protected WebClient modifyWebClient(WebClient client){ } }; - capabilities = DesiredCapabilities.htmlUnit(); - capabilities.setVersion("firefox-78"); + capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "firefox-78"); new HtmlUnitDriver(capabilities){ @Override @@ -317,8 +315,7 @@ protected WebClient modifyWebClient(WebClient client){ @Test public void ctorWebClientCapabilitiesVersion() { - DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); - capabilities.setVersion(BrowserType.FIREFOX); + DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, Browser.FIREFOX.browserName()); new HtmlUnitDriver(capabilities){ @Override @@ -329,8 +326,7 @@ protected WebClient modifyWebClient(WebClient client){ } }; - capabilities = DesiredCapabilities.htmlUnit(); - capabilities.setVersion(BrowserType.CHROME); + capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, Browser.CHROME.browserName()); new HtmlUnitDriver(capabilities){ @Override @@ -341,8 +337,7 @@ protected WebClient modifyWebClient(WebClient client){ } }; - capabilities = DesiredCapabilities.htmlUnit(); - capabilities.setVersion(BrowserType.EDGE); + capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, Browser.EDGE.browserName()); new HtmlUnitDriver(capabilities){ @Override @@ -353,8 +348,7 @@ protected WebClient modifyWebClient(WebClient client){ } }; - capabilities = DesiredCapabilities.htmlUnit(); - capabilities.setVersion(BrowserType.FIREFOX + "-78"); + capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT, "firefox-78"); new HtmlUnitDriver(capabilities){ @Override @@ -368,7 +362,7 @@ protected WebClient modifyWebClient(WebClient client){ @Test public void ctorWebClientCapabilitiesJsEnabledTrue() { - DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT); capabilities.setJavascriptEnabled(true); new HtmlUnitDriver(capabilities){ @@ -387,7 +381,7 @@ protected WebClient modifyWebClient(WebClient client){ @Test public void ctorWebClientCapabilitiesJsEnabledFalse() { - DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT); capabilities.setJavascriptEnabled(false); new HtmlUnitDriver(capabilities){ @@ -409,7 +403,7 @@ public void ctorWebClientCapabilitiesProxy() { Proxy proxy = new Proxy(); proxy.setHttpProxy("hostname:1234"); - DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT); capabilities.setCapability(CapabilityType.PROXY, proxy); new HtmlUnitDriver(capabilities){ diff --git a/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitProxyTest.java b/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitProxyTest.java index 523baffa..fef9fca1 100644 --- a/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitProxyTest.java +++ b/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitProxyTest.java @@ -26,12 +26,12 @@ import java.util.List; import org.junit.Test; -import org.openqa.selenium.Platform; import org.openqa.selenium.Proxy; -import org.openqa.selenium.remote.BrowserType; import org.openqa.selenium.remote.DesiredCapabilities; import com.gargoylesoftware.htmlunit.ProxyConfig; +import org.openqa.selenium.testing.drivers.BrowserToCapabilities; +import org.openqa.selenium.testing.drivers.BrowserType; /** * Test the proxy setting. @@ -40,7 +40,7 @@ public class HtmlUnitProxyTest { @Test public void testProxyAsCapability() { - DesiredCapabilities capabilities = new DesiredCapabilities(BrowserType.HTMLUNIT, "", Platform.LINUX); + DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.HTML_UNIT); Proxy proxy = new Proxy().setHttpProxy("http.proxy"); capabilities.setCapability(PROXY, proxy); diff --git a/src/test/java/org/openqa/selenium/htmlunit/WebDriverTestCase.java b/src/test/java/org/openqa/selenium/htmlunit/WebDriverTestCase.java index 42e9da1c..ee3149e1 100644 --- a/src/test/java/org/openqa/selenium/htmlunit/WebDriverTestCase.java +++ b/src/test/java/org/openqa/selenium/htmlunit/WebDriverTestCase.java @@ -76,7 +76,7 @@ import org.openqa.selenium.ie.InternetExplorerOptions; import org.openqa.selenium.logging.LogType; import org.openqa.selenium.logging.LoggingPreferences; -import org.openqa.selenium.remote.BrowserType; +import org.openqa.selenium.remote.Browser; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.UnreachableBrowserException; @@ -426,7 +426,7 @@ protected WebDriver buildWebDriver() throws IOException { } if (webDriver_ == null) { final DesiredCapabilities capabilities = new DesiredCapabilities(); - capabilities.setBrowserName(BrowserType.HTMLUNIT); + capabilities.setBrowserName(Browser.HTMLUNIT.browserName()); capabilities.setVersion(getBrowserName(getBrowserVersion())); webDriver_ = new HtmlUnitDriver(capabilities); } @@ -455,15 +455,15 @@ private static FirefoxDriver createFirefoxDriver(final String geckodriverBinary, private static String getBrowserName(final BrowserVersion browserVersion) { if (browserVersion == BrowserVersion.FIREFOX) { - return BrowserType.FIREFOX + '-' + browserVersion.getBrowserVersionNumeric(); + return Browser.FIREFOX.browserName() + '-' + browserVersion.getBrowserVersionNumeric(); } if (browserVersion == BrowserVersion.FIREFOX_78) { - return BrowserType.FIREFOX + '-' + browserVersion.getBrowserVersionNumeric(); + return Browser.FIREFOX.browserName(); } if (browserVersion == BrowserVersion.INTERNET_EXPLORER) { - return BrowserType.IE; + return Browser.IE.browserName(); } - return BrowserType.CHROME; + return Browser.CHROME.browserName(); } /** diff --git a/src/test/java/org/openqa/selenium/interactions/DragAndDropTest.java b/src/test/java/org/openqa/selenium/interactions/DragAndDropTest.java index 5263e705..39c20fcd 100644 --- a/src/test/java/org/openqa/selenium/interactions/DragAndDropTest.java +++ b/src/test/java/org/openqa/selenium/interactions/DragAndDropTest.java @@ -44,7 +44,7 @@ import org.openqa.selenium.testing.NotYetImplemented; import org.openqa.selenium.testing.SwitchToTopAfterTest; import org.openqa.selenium.testing.TestUtilities; -import org.openqa.selenium.testing.drivers.Browser; +import org.openqa.selenium.testing.drivers.BrowserType; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -55,7 +55,7 @@ public class DragAndDropTest extends JUnit4TestBase { @Test public void testDragAndDropRelative() { assumeFalse("See issue 2281", TestUtilities.getEffectivePlatform().is(Platform.MAC)); - assumeFalse(Browser.detect() == Browser.opera && + assumeFalse(BrowserType.detect() == BrowserType.OPERA && TestUtilities.getEffectivePlatform().is(Platform.WINDOWS)); driver.get(pages.dragAndDropPage); diff --git a/src/test/java/org/openqa/selenium/testing/JUnit4TestBase.java b/src/test/java/org/openqa/selenium/testing/JUnit4TestBase.java index 37c091c2..4f16730b 100644 --- a/src/test/java/org/openqa/selenium/testing/JUnit4TestBase.java +++ b/src/test/java/org/openqa/selenium/testing/JUnit4TestBase.java @@ -45,7 +45,7 @@ import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.Wait; import org.openqa.selenium.support.ui.WebDriverWait; -import org.openqa.selenium.testing.drivers.Browser; +import org.openqa.selenium.testing.drivers.BrowserType; import org.openqa.selenium.testing.drivers.SauceDriver; import org.openqa.selenium.testing.drivers.WebDriverBuilder; @@ -56,7 +56,7 @@ public abstract class JUnit4TestBase implements WrapsDriver { private static final Logger logger = Logger.getLogger(JUnit4TestBase.class.getName()); - private Browser browser = Browser.detect(); + private final BrowserType browser = BrowserType.detect(); protected TestEnvironment environment; protected AppServer appServer; protected Pages pages; @@ -275,44 +275,44 @@ protected boolean isIeDriverTimedOutException(IllegalStateException e) { return e.getClass().getName().contains("TimedOutException"); } - private static boolean matches(Browser browser, Driver[] drivers) { + private static boolean matches(BrowserType browser, Driver[] drivers) { for (Driver driver : drivers) { switch (driver) { case ALL: return true; case CHROME: - if (browser == Browser.chrome) { + if (browser == BrowserType.CHROME) { return true; } break; case FIREFOX: - if (browser == Browser.ff && !Boolean.getBoolean("webdriver.firefox.marionette")) { + if (browser == BrowserType.FIREFOX && !Boolean.getBoolean("webdriver.firefox.marionette")) { return true; } break; case HTMLUNIT: - if (browser == Browser.htmlunit) { + if (browser == BrowserType.HTML_UNIT) { return true; } break; case IE: - if (browser == Browser.ie) { + if (browser == BrowserType.IE) { return true; } break; case EDGE: - if (browser == Browser.edge) { + if (browser == BrowserType.EDGE) { return true; } break; case MARIONETTE: - if (browser != Browser.ff) { + if (browser != BrowserType.FIREFOX) { return false; } if (System.getProperty("webdriver.firefox.marionette") == null || @@ -336,7 +336,7 @@ private static boolean matches(Browser browser, Driver[] drivers) { break; case SAFARI: - if (browser == Browser.safari) { + if (browser == BrowserType.SAFARI) { return true; } break; diff --git a/src/test/java/org/openqa/selenium/testing/SeleniumTestRunner.java b/src/test/java/org/openqa/selenium/testing/SeleniumTestRunner.java index 82ca1b77..4d83240e 100644 --- a/src/test/java/org/openqa/selenium/testing/SeleniumTestRunner.java +++ b/src/test/java/org/openqa/selenium/testing/SeleniumTestRunner.java @@ -21,7 +21,7 @@ import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.InitializationError; -import org.openqa.selenium.testing.drivers.Browser; +import org.openqa.selenium.testing.drivers.BrowserType; import org.openqa.selenium.testing.drivers.TestIgnorance; public class SeleniumTestRunner extends BlockJUnit4ClassRunner { @@ -37,11 +37,11 @@ public class SeleniumTestRunner extends BlockJUnit4ClassRunner { */ public SeleniumTestRunner(Class klass) throws InitializationError { super(klass); - Browser browser = Browser.detect(); - if (browser == null && DevMode.isInDevMode()) { - browser = Browser.chrome; + BrowserType browserType = BrowserType.detect(); + if (browserType == null && DevMode.isInDevMode()) { + browserType = BrowserType.CHROME; } - ignorance = new TestIgnorance(browser); + ignorance = new TestIgnorance(browserType); } @Override diff --git a/src/test/java/org/openqa/selenium/testing/drivers/BrowserToCapabilities.java b/src/test/java/org/openqa/selenium/testing/drivers/BrowserToCapabilities.java index c06cd724..ec0e1f45 100644 --- a/src/test/java/org/openqa/selenium/testing/drivers/BrowserToCapabilities.java +++ b/src/test/java/org/openqa/selenium/testing/drivers/BrowserToCapabilities.java @@ -21,57 +21,60 @@ import org.openqa.selenium.Platform; import org.openqa.selenium.firefox.FirefoxDriver; -import org.openqa.selenium.remote.BrowserType; +import org.openqa.selenium.remote.Browser; import org.openqa.selenium.remote.DesiredCapabilities; public class BrowserToCapabilities { - public static DesiredCapabilities of(Browser browser) { - if (browser == null) { + + public static DesiredCapabilities of(BrowserType browserType) { + return of(browserType, ""); + } + + public static DesiredCapabilities of(BrowserType browserType, String version) { + if (browserType == null) { return null; } DesiredCapabilities caps; - switch (browser) { - case chrome: - caps = new DesiredCapabilities(BrowserType.CHROME,"", Platform.ANY); + switch (browserType) { + case CHROME: + caps = new DesiredCapabilities(Browser.CHROME.browserName(), version, Platform.ANY); break; - case ff: - caps = new DesiredCapabilities(BrowserType.FIREFOX,"", Platform.ANY); - String property = - System.getProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE, "true"); - boolean useMarionette = property != null && Boolean.parseBoolean(property); - caps.setCapability(FirefoxDriver.MARIONETTE, useMarionette); + case FIREFOX: + caps = new DesiredCapabilities(Browser.FIREFOX.browserName(), version, Platform.ANY); + String property = System.getProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE, "true"); + boolean useMarionette = Boolean.parseBoolean(property); + caps.setCapability(FirefoxDriver.Capability.MARIONETTE, useMarionette); break; - case htmlunit: - caps = DesiredCapabilities.htmlUnit(); + case HTML_UNIT: + caps = new DesiredCapabilities(Browser.HTMLUNIT.browserName(), version, Platform.ANY); break; - case ie: - caps = new DesiredCapabilities(BrowserType.IE,"", Platform.WINDOWS); + case IE: + caps = new DesiredCapabilities(Browser.IE.browserName(), version, Platform.WINDOWS); break; - case operablink: - caps = new DesiredCapabilities(BrowserType.OPERA,"", Platform.ANY); + case OPERA_BLINK: + caps = new DesiredCapabilities(Browser.OPERA.browserName(), version, Platform.ANY); break; - case safari: - caps = new DesiredCapabilities(BrowserType.SAFARI,"", Platform.MAC); + case SAFARI: + caps = new DesiredCapabilities(Browser.SAFARI.browserName(), version, Platform.MAC); break; default: throw new RuntimeException("Cannot determine browser config to use"); } - String version = System.getProperty("selenium.browser.version"); - if (version != null) { - caps.setVersion(version); + final String systemVersion = System.getProperty("selenium.browser.version"); + if (systemVersion != null) { + caps.setVersion(systemVersion); } - caps.setCapability(HAS_NATIVE_EVENTS, - Boolean.getBoolean("selenium.browser.native_events")); + caps.setCapability(HAS_NATIVE_EVENTS, Boolean.getBoolean("selenium.browser.native_events")); return caps; } diff --git a/src/test/java/org/openqa/selenium/testing/drivers/Browser.java b/src/test/java/org/openqa/selenium/testing/drivers/BrowserType.java similarity index 53% rename from src/test/java/org/openqa/selenium/testing/drivers/Browser.java rename to src/test/java/org/openqa/selenium/testing/drivers/BrowserType.java index d5d9cedd..4fac1bd5 100644 --- a/src/test/java/org/openqa/selenium/testing/drivers/Browser.java +++ b/src/test/java/org/openqa/selenium/testing/drivers/BrowserType.java @@ -17,34 +17,52 @@ package org.openqa.selenium.testing.drivers; +import java.util.Arrays; import java.util.logging.Logger; -public enum Browser { +public enum BrowserType { - chrome, - edge, - ff, - htmlunit, - ie, - none, // For those cases where you don't actually want a browser - opera, - operablink, - safari; + CHROME("chrome"), + EDGE("edge"), + FIREFOX("ff"), + HTML_UNIT("htmlunit"), + IE("ie"), + NONE("none"), // For those cases where you don't actually want a browser + OPERA("opera"), + OPERA_BLINK("operablink"), + SAFARI("safari"); - private static final Logger log = Logger.getLogger(Browser.class.getName()); + private static final Logger log = Logger.getLogger(BrowserType.class.getName()); - public static Browser detect() { + private final String name; + + BrowserType(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public static BrowserType detect() { String browserName = System.getProperty("selenium.browser"); if (browserName == null) { - return htmlunit; + return HTML_UNIT; } try { - return Browser.valueOf(browserName); + return BrowserType.getByName(browserName); } catch (IllegalArgumentException e) { - log.severe("Cannot locate matching browser for: " + browserName); + log.severe(e.getMessage()); return null; } } + public static BrowserType getByName(String name) { + return Arrays.stream(BrowserType.values()) + .filter(f -> f.getName().equals(name)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("Cannot locate matching browser for: " + name)); + } + } diff --git a/src/test/java/org/openqa/selenium/testing/drivers/GridSupplier.java b/src/test/java/org/openqa/selenium/testing/drivers/GridSupplier.java index 1a0b52e3..9756980c 100644 --- a/src/test/java/org/openqa/selenium/testing/drivers/GridSupplier.java +++ b/src/test/java/org/openqa/selenium/testing/drivers/GridSupplier.java @@ -26,7 +26,6 @@ import org.openqa.selenium.Capabilities; import org.openqa.selenium.WebDriver; import org.openqa.selenium.json.Json; -import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.LocalFileDetector; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.remote.http.HttpClient; @@ -104,7 +103,7 @@ private synchronized void startServers() { public static void main(String[] args) { System.setProperty("selenium.browser.grid", "true"); - WebDriver driver = new GridSupplier(BrowserToCapabilities.of(Browser.ff)).get(); + WebDriver driver = new GridSupplier(BrowserToCapabilities.of(BrowserType.FIREFOX)).get(); driver.get("http://www.google.com"); } } diff --git a/src/test/java/org/openqa/selenium/testing/drivers/ReflectionBackedDriverSupplier.java b/src/test/java/org/openqa/selenium/testing/drivers/ReflectionBackedDriverSupplier.java index 1788acb4..6cf26647 100644 --- a/src/test/java/org/openqa/selenium/testing/drivers/ReflectionBackedDriverSupplier.java +++ b/src/test/java/org/openqa/selenium/testing/drivers/ReflectionBackedDriverSupplier.java @@ -25,7 +25,7 @@ import org.openqa.selenium.Capabilities; import org.openqa.selenium.WebDriver; -import org.openqa.selenium.remote.BrowserType; +import org.openqa.selenium.remote.Browser; public class ReflectionBackedDriverSupplier implements Supplier { @@ -57,21 +57,21 @@ private Class mapToClass(Capabilities caps) { String name = caps == null ? "" : caps.getBrowserName(); String className; - if (BrowserType.CHROME.equals(name)) { + if (Browser.CHROME.browserName().equals(name)) { className = "org.openqa.selenium.testing.drivers.TestChromeDriver"; - } else if (BrowserType.OPERA_BLINK.equals(name)) { + } else if (Browser.OPERA.browserName().equals(name)) { className = "org.openqa.selenium.testing.drivers.TestOperaBlinkDriver"; - } else if (BrowserType.FIREFOX.equals(name)) { + } else if (Browser.FIREFOX.browserName().equals(name)) { if (isInDevMode()) { className = "org.openqa.selenium.testing.drivers.SynthesizedFirefoxDriver"; } else { className = "org.openqa.selenium.firefox.FirefoxDriver"; } - } else if (BrowserType.HTMLUNIT.equals(name)) { - className = "org.openqa.selenium.htmlunit.HtmlUnitDriver"; - } else if (BrowserType.IE.equals(name)) { + } else if (Browser.HTMLUNIT.browserName().equals(name)) { + className = "org.openqa.selenium.htmlunit.HtmlUnitDriver"; + } else if (Browser.IE.browserName().equals(name)) { className = "org.openqa.selenium.ie.InternetExplorerDriver"; - } else if (BrowserType.SAFARI.equals(name)) { + } else if (Browser.SAFARI.browserName().equals(name)) { className = "org.openqa.selenium.safari.SafariDriver"; } else { // The last chance saloon. diff --git a/src/test/java/org/openqa/selenium/testing/drivers/SauceDriver.java b/src/test/java/org/openqa/selenium/testing/drivers/SauceDriver.java index e7217115..658ac847 100644 --- a/src/test/java/org/openqa/selenium/testing/drivers/SauceDriver.java +++ b/src/test/java/org/openqa/selenium/testing/drivers/SauceDriver.java @@ -122,7 +122,7 @@ private static Capabilities munge(Capabilities desiredCapabilities, String selen mungedCapabilities.setCapability("name", jobName); } - if (BrowserToCapabilities.of(Browser.ie).getBrowserName().equals(desiredCapabilities.getBrowserName())) { + if (BrowserToCapabilities.of(BrowserType.IE).getBrowserName().equals(desiredCapabilities.getBrowserName())) { String ieDriverVersion = System.getenv(SELENIUM_IEDRIVER_ENV_NAME); if (ieDriverVersion != null) { mungedCapabilities.setCapability("iedriver-version", ieDriverVersion); @@ -130,7 +130,7 @@ private static Capabilities munge(Capabilities desiredCapabilities, String selen mungedCapabilities.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true); } - if (BrowserToCapabilities.of(Browser.chrome).getBrowserName().equals(desiredCapabilities.getBrowserName())) { + if (BrowserToCapabilities.of(BrowserType.CHROME).getBrowserName().equals(desiredCapabilities.getBrowserName())) { String chromeDriverVersion = System.getenv(SELENIUM_CHROMEDRIVER_ENV_NAME); if (chromeDriverVersion != null) { System.out.println("Setting chromedriver-version capability to " + chromeDriverVersion); diff --git a/src/test/java/org/openqa/selenium/testing/drivers/TestChromeDriver.java b/src/test/java/org/openqa/selenium/testing/drivers/TestChromeDriver.java index d9b14272..8cd7f32c 100644 --- a/src/test/java/org/openqa/selenium/testing/drivers/TestChromeDriver.java +++ b/src/test/java/org/openqa/selenium/testing/drivers/TestChromeDriver.java @@ -90,7 +90,7 @@ private static DesiredCapabilities chromeWithCustomCapabilities( options.setBinary(new File(chromePath)); } - DesiredCapabilities capabilities = BrowserToCapabilities.of(Browser.chrome); + DesiredCapabilities capabilities = BrowserToCapabilities.of(BrowserType.CHROME); capabilities.setCapability(ChromeOptions.CAPABILITY, options); if (originalCapabilities != null) { diff --git a/src/test/java/org/openqa/selenium/testing/drivers/TestIgnorance.java b/src/test/java/org/openqa/selenium/testing/drivers/TestIgnorance.java index 5f07fb46..19c69dfc 100644 --- a/src/test/java/org/openqa/selenium/testing/drivers/TestIgnorance.java +++ b/src/test/java/org/openqa/selenium/testing/drivers/TestIgnorance.java @@ -29,10 +29,6 @@ import static org.openqa.selenium.testing.Driver.MARIONETTE; import static org.openqa.selenium.testing.Driver.REMOTE; import static org.openqa.selenium.testing.Driver.SAFARI; -import static org.openqa.selenium.testing.drivers.Browser.chrome; -import static org.openqa.selenium.testing.drivers.Browser.htmlunit; -import static org.openqa.selenium.testing.drivers.Browser.ie; -import static org.openqa.selenium.testing.drivers.Browser.opera; import java.util.Arrays; import java.util.Set; @@ -52,16 +48,16 @@ */ public class TestIgnorance { - private Set alwaysNativeEvents = ImmutableSet.of(chrome, ie, opera); - private Set neverNativeEvents = ImmutableSet.of(htmlunit); - private IgnoreComparator ignoreComparator = new IgnoreComparator(); - private Set methods = Sets.newHashSet(); - private Set only = Sets.newHashSet(); - private Set ignoreMethods = Sets.newHashSet(); - private Set ignoreClasses = Sets.newHashSet(); - private Browser browser; + private final Set alwaysNativeEvents = ImmutableSet.of(BrowserType.CHROME, BrowserType.IE, BrowserType.OPERA); + private final Set neverNativeEvents = ImmutableSet.of(BrowserType.HTML_UNIT); + private final IgnoreComparator ignoreComparator = new IgnoreComparator(); + private final Set methods = Sets.newHashSet(); + private final Set only = Sets.newHashSet(); + private final Set ignoreMethods = Sets.newHashSet(); + private final Set ignoreClasses = Sets.newHashSet(); + private BrowserType browser; - public TestIgnorance(Browser browser) { + public TestIgnorance(BrowserType browser) { setBrowser(browser); String onlyRun = System.getProperty("only_run"); @@ -157,28 +153,28 @@ private boolean isIgnoredDueToEnvironmentVariables(Description method) { ignoreMethods.contains(method.getMethodName()); } - public void setBrowser(Browser browser) { + public void setBrowser(BrowserType browserType) { this.browser = checkNotNull( - browser, + browserType, "Browser to use must be set. Do this by setting the 'selenium.browser' system property"); - addIgnoresForBrowser(browser, ignoreComparator); + addIgnoresForBrowser(browserType, ignoreComparator); } - private void addIgnoresForBrowser(Browser browser, IgnoreComparator comparator) { + private void addIgnoresForBrowser(BrowserType browserType, IgnoreComparator comparator) { if (Boolean.getBoolean("selenium.browser.remote") || SauceDriver.shouldUseSauce()) { comparator.addDriver(REMOTE); } - switch (browser) { - case chrome: + switch (browserType) { + case CHROME: comparator.addDriver(CHROME); break; - case edge: + case EDGE: comparator.addDriver(EDGE); break; - case ff: + case FIREFOX: if (Boolean.getBoolean("webdriver.firefox.marionette")) { comparator.addDriver(MARIONETTE); } else { @@ -186,26 +182,26 @@ private void addIgnoresForBrowser(Browser browser, IgnoreComparator comparator) } break; - case htmlunit: + case HTML_UNIT: comparator.addDriver(HTMLUNIT); break; - case ie: + case IE: comparator.addDriver(IE); break; - case none: + case NONE: comparator.addDriver(ALL); break; - case opera: + case OPERA: break; - case operablink: + case OPERA_BLINK: comparator.addDriver(CHROME); break; - case safari: + case SAFARI: comparator.addDriver(SAFARI); break; diff --git a/src/test/java/org/openqa/selenium/testing/drivers/TestInternetExplorerSupplier.java b/src/test/java/org/openqa/selenium/testing/drivers/TestInternetExplorerSupplier.java index 92be5362..72e0a0fa 100644 --- a/src/test/java/org/openqa/selenium/testing/drivers/TestInternetExplorerSupplier.java +++ b/src/test/java/org/openqa/selenium/testing/drivers/TestInternetExplorerSupplier.java @@ -22,10 +22,9 @@ import org.openqa.selenium.Capabilities; import org.openqa.selenium.WebDriver; -import org.openqa.selenium.remote.DesiredCapabilities; public class TestInternetExplorerSupplier implements Supplier { - private Capabilities caps; + private final Capabilities caps; public TestInternetExplorerSupplier(Capabilities caps) { this.caps = caps; @@ -36,7 +35,7 @@ public WebDriver get() { return null; } - if (!BrowserToCapabilities.of(Browser.ie).getBrowserName().equals(caps.getBrowserName())) { + if (!BrowserToCapabilities.of(BrowserType.IE).getBrowserName().equals(caps.getBrowserName())) { return null; } diff --git a/src/test/java/org/openqa/selenium/testing/drivers/WebDriverBuilder.java b/src/test/java/org/openqa/selenium/testing/drivers/WebDriverBuilder.java index 5a61bc09..261b4ccf 100644 --- a/src/test/java/org/openqa/selenium/testing/drivers/WebDriverBuilder.java +++ b/src/test/java/org/openqa/selenium/testing/drivers/WebDriverBuilder.java @@ -31,15 +31,15 @@ public class WebDriverBuilder implements Supplier { private Capabilities desiredCapabilities; - private final Browser browser; + private final BrowserType browser; public WebDriverBuilder() { - this(Browser.detect()); + this(BrowserType.detect()); } - public WebDriverBuilder(Browser browser) { + public WebDriverBuilder(BrowserType browser) { if (browser == null) { - this.browser = Browser.chrome; + this.browser = BrowserType.CHROME; } else { this.browser = browser; }