|
| 1 | +package io.percy.selenium; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.AfterAll; |
| 9 | +import org.junit.jupiter.api.AfterEach; |
| 10 | +//import org.junit.Assert; |
| 11 | +import org.junit.jupiter.api.BeforeAll; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import static org.junit.jupiter.api.Assertions.*; |
| 14 | + |
| 15 | +import org.openqa.selenium.By; |
| 16 | +import org.openqa.selenium.JavascriptExecutor; |
| 17 | +import org.openqa.selenium.Keys; |
| 18 | +import org.openqa.selenium.WebDriver; |
| 19 | +import org.openqa.selenium.WebElement; |
| 20 | +import org.openqa.selenium.firefox.FirefoxDriver; |
| 21 | +import org.openqa.selenium.firefox.FirefoxOptions; |
| 22 | + |
| 23 | +import io.github.bonigarcia.wdm.WebDriverManager; |
| 24 | + |
| 25 | +import org.openqa.selenium.remote.*; |
| 26 | +import static org.mockito.Mockito.*; |
| 27 | +import java.net.URL; |
| 28 | +import java.util.concurrent.ConcurrentHashMap; |
| 29 | + |
| 30 | +public class CacheTest { |
| 31 | + private static RemoteWebDriver mockedDriver; |
| 32 | + |
| 33 | + private static Percy percy; |
| 34 | + |
| 35 | + @BeforeAll |
| 36 | + public static void testSetup() throws IOException { |
| 37 | + mockedDriver = mock(RemoteWebDriver.class); |
| 38 | + HttpCommandExecutor commandExecutor = mock(HttpCommandExecutor.class); |
| 39 | + try { |
| 40 | + when(commandExecutor.getAddressOfRemoteServer()).thenReturn(new URL("https://hub-cloud.browserstack.com/wd/hub")); |
| 41 | + } catch (Exception e) { |
| 42 | + } |
| 43 | + percy = spy(new Percy(mockedDriver)); |
| 44 | + when(mockedDriver.getSessionId()).thenReturn(new SessionId("123")); |
| 45 | + when(mockedDriver.getCommandExecutor()).thenReturn(commandExecutor); |
| 46 | + DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 47 | + capabilities.setCapability("browserName", "Chrome"); |
| 48 | + when(mockedDriver.getCapabilities()).thenReturn(capabilities); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testSessionId() { |
| 53 | + Cache.CACHE_MAP.clear(); |
| 54 | + DriverMetadata driverMetadata = new DriverMetadata((WebDriver) mockedDriver); |
| 55 | + assertEquals(driverMetadata.getSessionId(), "123"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testCapabilities() { |
| 60 | + Cache.CACHE_MAP.clear(); |
| 61 | + DriverMetadata driverMetadata = new DriverMetadata((WebDriver) mockedDriver); |
| 62 | + String key = "capabilities_"+driverMetadata.getSessionId(); |
| 63 | + assertNull(Cache.CACHE_MAP.get(key)); |
| 64 | + ConcurrentHashMap<String, String> caps = driverMetadata.getCapabilities(); |
| 65 | + assertEquals(Cache.CACHE_MAP.get(key), caps); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testCommandExecutorUrl() { |
| 70 | + Cache.CACHE_MAP.clear(); |
| 71 | + DriverMetadata driverMetadata = new DriverMetadata(mockedDriver); |
| 72 | + String key = "commandExecutorUrl_"+driverMetadata.getSessionId(); |
| 73 | + assertNull(Cache.CACHE_MAP.get(key)); |
| 74 | + String commandExecutorUrl = driverMetadata.getCommandExecutorUrl(); |
| 75 | + assertEquals(Cache.CACHE_MAP.get(key), commandExecutorUrl); |
| 76 | + } |
| 77 | +} |
0 commit comments