From 552f955c117d76d5fb0df2b11c3e55e53c1a2911 Mon Sep 17 00:00:00 2001 From: Diego Molina Date: Tue, 21 Mar 2017 18:21:41 +0100 Subject: [PATCH] [BUA-925] Adding and fixing unit tests. --- .../DockerSeleniumStarterRemoteProxy.java | 8 +- .../proxy/DockerSeleniumRemoteProxyTest.java | 2 +- .../DockerSeleniumStarterRemoteProxyTest.java | 98 +++++++++++++++++++ .../zalando/tip/zalenium/util/TestUtils.java | 23 +++++ 4 files changed, 126 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/zalando/tip/zalenium/proxy/DockerSeleniumStarterRemoteProxy.java b/src/main/java/de/zalando/tip/zalenium/proxy/DockerSeleniumStarterRemoteProxy.java index 8479f580dc..dc36f278d4 100644 --- a/src/main/java/de/zalando/tip/zalenium/proxy/DockerSeleniumStarterRemoteProxy.java +++ b/src/main/java/de/zalando/tip/zalenium/proxy/DockerSeleniumStarterRemoteProxy.java @@ -268,7 +268,7 @@ protected static void setScreenHeight(int screenHeight) { } public static int getConfiguredScreenWidth() { - return configuredScreenWidth; + return configuredScreenWidth <= 0 ? DEFAULT_SCREEN_WIDTH : configuredScreenWidth; } public static void setConfiguredScreenWidth(int configuredScreenWidth) { @@ -276,7 +276,7 @@ public static void setConfiguredScreenWidth(int configuredScreenWidth) { } public static int getConfiguredScreenHeight() { - return configuredScreenHeight; + return configuredScreenHeight <= 0 ? DEFAULT_SCREEN_HEIGHT : configuredScreenHeight; } public static void setConfiguredScreenHeight(int configuredScreenHeight) { @@ -523,8 +523,8 @@ private void configureScreenResolutionFromCapabilities(Map reque "defaults will be used. Passed value -> " + screenResolution); } } catch (Exception e) { - setScreenWidth(configuredScreenWidth); - setScreenHeight(configuredScreenHeight); + setScreenWidth(getConfiguredScreenWidth()); + setScreenHeight(getConfiguredScreenHeight()); LOGGER.log(Level.FINE, "Values provided for screenResolution are not valid integers or " + "either the width or the height is missing, defaults will be used. Passed value -> " + screenResolution); diff --git a/src/test/java/de/zalando/tip/zalenium/proxy/DockerSeleniumRemoteProxyTest.java b/src/test/java/de/zalando/tip/zalenium/proxy/DockerSeleniumRemoteProxyTest.java index dd83d9b8ef..a0474e3edc 100644 --- a/src/test/java/de/zalando/tip/zalenium/proxy/DockerSeleniumRemoteProxyTest.java +++ b/src/test/java/de/zalando/tip/zalenium/proxy/DockerSeleniumRemoteProxyTest.java @@ -49,7 +49,7 @@ public void setUp() throws DockerException, InterruptedException, IOException { RegistrationRequest request = TestUtils.getRegistrationRequestForTesting(40000, DockerSeleniumRemoteProxy.class.getCanonicalName()); request.getConfiguration().capabilities.clear(); - request.getConfiguration().capabilities.addAll(DockerSeleniumStarterRemoteProxy.getCapabilities()); + request.getConfiguration().capabilities.addAll(TestUtils.getDockerSeleniumCapabilitiesForTesting()); // Creating the proxy proxy = DockerSeleniumRemoteProxy.getNewInstance(request, registry); diff --git a/src/test/java/de/zalando/tip/zalenium/proxy/DockerSeleniumStarterRemoteProxyTest.java b/src/test/java/de/zalando/tip/zalenium/proxy/DockerSeleniumStarterRemoteProxyTest.java index 5c3c1ea572..32111eaaba 100644 --- a/src/test/java/de/zalando/tip/zalenium/proxy/DockerSeleniumStarterRemoteProxyTest.java +++ b/src/test/java/de/zalando/tip/zalenium/proxy/DockerSeleniumStarterRemoteProxyTest.java @@ -26,6 +26,7 @@ import static java.util.concurrent.TimeUnit.SECONDS; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.Mockito.times; @@ -127,6 +128,80 @@ public void containerIsStartedWhenBrowserIsSupportedAndLatestIsUsedAsBrowserVers verify(spyProxy, times(1)).startDockerSeleniumContainer(BrowserType.CHROME); } + @Test + public void containerIsStartedWhenScreenResolutionIsProvided() { + // Supported desired capability for the test session + Map supportedCapability = new HashMap<>(); + supportedCapability.put(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX); + supportedCapability.put(CapabilityType.PLATFORM, Platform.ANY); + supportedCapability.put("screenResolution", "1280x760"); + TestSession testSession = spyProxy.getNewSession(supportedCapability); + Assert.assertNull(testSession); + verify(spyProxy, times(1)).startDockerSeleniumContainer(BrowserType.FIREFOX); + Assert.assertEquals(1280, DockerSeleniumStarterRemoteProxy.getScreenWidth()); + Assert.assertEquals(760, DockerSeleniumStarterRemoteProxy.getScreenHeight()); + } + + @Test + public void containerIsStartedWhenResolutionIsProvided() { + // Supported desired capability for the test session + Map supportedCapability = new HashMap<>(); + supportedCapability.put(CapabilityType.BROWSER_NAME, BrowserType.CHROME); + supportedCapability.put(CapabilityType.PLATFORM, Platform.ANY); + supportedCapability.put("resolution", "1300x900"); + TestSession testSession = spyProxy.getNewSession(supportedCapability); + Assert.assertNull(testSession); + verify(spyProxy, times(1)).startDockerSeleniumContainer(BrowserType.CHROME); + Assert.assertEquals(1300, DockerSeleniumStarterRemoteProxy.getScreenWidth()); + Assert.assertEquals(900, DockerSeleniumStarterRemoteProxy.getScreenHeight()); + } + + @Test + public void containerIsStartedWhenCustomScreenResolutionIsProvided() { + // Supported desired capability for the test session + Map supportedCapability = new HashMap<>(); + supportedCapability.put(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX); + supportedCapability.put(CapabilityType.PLATFORM, Platform.ANY); + supportedCapability.put("screen-resolution", "1500x1000"); + TestSession testSession = spyProxy.getNewSession(supportedCapability); + Assert.assertNull(testSession); + verify(spyProxy, times(1)).startDockerSeleniumContainer(BrowserType.FIREFOX); + Assert.assertEquals(1500, DockerSeleniumStarterRemoteProxy.getScreenWidth()); + Assert.assertEquals(1000, DockerSeleniumStarterRemoteProxy.getScreenHeight()); + } + + @Test + public void containerIsStartedWhenNegativeResolutionIsProvidedUsingDefaults() { + // Supported desired capability for the test session + Map supportedCapability = new HashMap<>(); + supportedCapability.put(CapabilityType.BROWSER_NAME, BrowserType.CHROME); + supportedCapability.put(CapabilityType.PLATFORM, Platform.ANY); + supportedCapability.put("resolution", "-1300x800"); + TestSession testSession = spyProxy.getNewSession(supportedCapability); + Assert.assertNull(testSession); + verify(spyProxy, times(1)).startDockerSeleniumContainer(BrowserType.CHROME); + Assert.assertEquals(DockerSeleniumStarterRemoteProxy.getConfiguredScreenWidth(), + DockerSeleniumStarterRemoteProxy.getScreenWidth()); + Assert.assertEquals(DockerSeleniumStarterRemoteProxy.getConfiguredScreenHeight(), + DockerSeleniumStarterRemoteProxy.getScreenHeight()); + } + + @Test + public void containerIsStartedWhenAnInvalidResolutionIsProvidedUsingDefaults() { + // Supported desired capability for the test session + Map supportedCapability = new HashMap<>(); + supportedCapability.put(CapabilityType.BROWSER_NAME, BrowserType.CHROME); + supportedCapability.put(CapabilityType.PLATFORM, Platform.ANY); + supportedCapability.put("screenResolution", "notAValidScreenResolution"); + TestSession testSession = spyProxy.getNewSession(supportedCapability); + Assert.assertNull(testSession); + verify(spyProxy, times(1)).startDockerSeleniumContainer(BrowserType.CHROME); + Assert.assertEquals(DockerSeleniumStarterRemoteProxy.getConfiguredScreenWidth(), + DockerSeleniumStarterRemoteProxy.getScreenWidth()); + Assert.assertEquals(DockerSeleniumStarterRemoteProxy.getConfiguredScreenHeight(), + DockerSeleniumStarterRemoteProxy.getScreenHeight()); + } + @Test public void containerIsStartedWhenFirefoxCapabilitiesAreSupported() { @@ -151,6 +226,29 @@ public void noContainerIsStartedWhenBrowserCapabilityIsAbsent() { verify(spyProxy, never()).startDockerSeleniumContainer(anyString()); } + @Test + public void noContainerIsStartedForAlreadyProcessedRequest() { + Map requestedCapability = new HashMap<>(); + requestedCapability.put(CapabilityType.BROWSER_NAME, BrowserType.CHROME); + requestedCapability.put(CapabilityType.PLATFORM, Platform.LINUX); + requestedCapability.put("waitingFor_CHROME_Node", 1); + TestSession testSession = spyProxy.getNewSession(requestedCapability); + Assert.assertNull(testSession); + verify(spyProxy, times(0)).startDockerSeleniumContainer(anyString()); + } + + @Test + public void containerIsStartedForRequestProcessedMoreThan20Times() { + Map requestedCapability = new HashMap<>(); + requestedCapability.put(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX); + requestedCapability.put(CapabilityType.PLATFORM, Platform.LINUX); + requestedCapability.put("waitingFor_FIREFOX_Node", 21); + TestSession testSession = spyProxy.getNewSession(requestedCapability); + Assert.assertNull(testSession); + verify(spyProxy, times(1)).startDockerSeleniumContainer(anyString(), eq(true)); + } + + /* Tests checking the environment variables setup to have a given number of containers on startup */ diff --git a/src/test/java/de/zalando/tip/zalenium/util/TestUtils.java b/src/test/java/de/zalando/tip/zalenium/util/TestUtils.java index 6e49b4dbac..3f87ba6b1e 100644 --- a/src/test/java/de/zalando/tip/zalenium/util/TestUtils.java +++ b/src/test/java/de/zalando/tip/zalenium/util/TestUtils.java @@ -3,6 +3,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import de.zalando.tip.zalenium.proxy.DockerSeleniumStarterRemoteProxy; import org.apache.commons.io.FileUtils; import org.openqa.grid.common.RegistrationRequest; import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration; @@ -12,13 +13,16 @@ import org.openqa.selenium.Platform; import org.openqa.selenium.remote.BrowserType; import org.openqa.selenium.remote.CapabilityType; +import org.openqa.selenium.remote.DesiredCapabilities; import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; +import java.util.List; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -55,6 +59,25 @@ public static WebDriverRequest getMockedWebDriverRequestStartSession() { return request; } + public static List getDockerSeleniumCapabilitiesForTesting() { + String screenResolution = String.format("%sx%s", DockerSeleniumStarterRemoteProxy.getConfiguredScreenWidth(), + DockerSeleniumStarterRemoteProxy.getConfiguredScreenHeight()); + List dsCapabilities = new ArrayList<>(); + DesiredCapabilities firefoxCapabilities = new DesiredCapabilities(); + firefoxCapabilities.setBrowserName(BrowserType.FIREFOX); + firefoxCapabilities.setPlatform(Platform.LINUX); + firefoxCapabilities.setCapability(RegistrationRequest.MAX_INSTANCES, 1); + firefoxCapabilities.setCapability("screenResolution", screenResolution); + dsCapabilities.add(firefoxCapabilities); + DesiredCapabilities chromeCapabilities = new DesiredCapabilities(); + chromeCapabilities.setBrowserName(BrowserType.CHROME); + chromeCapabilities.setPlatform(Platform.LINUX); + chromeCapabilities.setCapability(RegistrationRequest.MAX_INSTANCES, 1); + chromeCapabilities.setCapability("screenResolution", screenResolution); + dsCapabilities.add(chromeCapabilities); + return dsCapabilities; + } + @SuppressWarnings("ConstantConditions") public static JsonElement getTestInformationSample(String fileName) throws IOException { URL testInfoLocation = TestUtils.class.getClassLoader().getResource(fileName);