Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
[BUA-925] Adding and fixing unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
diemol committed Mar 21, 2017
1 parent 0b12a99 commit 552f955
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,15 @@ 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) {
DockerSeleniumStarterRemoteProxy.configuredScreenWidth = configuredScreenWidth;
}

public static int getConfiguredScreenHeight() {
return configuredScreenHeight;
return configuredScreenHeight <= 0 ? DEFAULT_SCREEN_HEIGHT : configuredScreenHeight;
}

public static void setConfiguredScreenHeight(int configuredScreenHeight) {
Expand Down Expand Up @@ -523,8 +523,8 @@ private void configureScreenResolutionFromCapabilities(Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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() {

Expand All @@ -151,6 +226,29 @@ public void noContainerIsStartedWhenBrowserCapabilityIsAbsent() {
verify(spyProxy, never()).startDockerSeleniumContainer(anyString());
}

@Test
public void noContainerIsStartedForAlreadyProcessedRequest() {
Map<String, Object> 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<String, Object> 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
*/
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/de/zalando/tip/zalenium/util/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -55,6 +59,25 @@ public static WebDriverRequest getMockedWebDriverRequestStartSession() {
return request;
}

public static List<DesiredCapabilities> getDockerSeleniumCapabilitiesForTesting() {
String screenResolution = String.format("%sx%s", DockerSeleniumStarterRemoteProxy.getConfiguredScreenWidth(),
DockerSeleniumStarterRemoteProxy.getConfiguredScreenHeight());
List<DesiredCapabilities> 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);
Expand Down

0 comments on commit 552f955

Please sign in to comment.