Skip to content

Commit bbe6e85

Browse files
authored
Pplt 2089/java sdk cache (#237)
* Making changes to make sdk sel 3 compatible * Making changes more simpler * SDK Caching for Percy Screenshot * Upadting ReadMe for PercyScreenshot * Few Typo fixes * Fixing readme * Resolving comments
1 parent fc5aa02 commit bbe6e85

File tree

5 files changed

+164
-31
lines changed

5 files changed

+164
-31
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ public class Example {
192192
`right` (int): Right coordinate of the ignore region.
193193
- Raises:ValueError: If top, bottom, left, or right is less than 0 or top is greater than or equal to bottom or left is greater than or equal to right.
194194
- valid: Ignore region should be within the boundaries of the screen.
195+
195196
### Creating Percy on automate build
196197
Note: Automate Percy Token starts with `auto` keyword. The command can be triggered using `exec` keyword.
197198
```sh-session
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.percy.selenium;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
public class Cache {
6+
public static final Map CACHE_MAP = new HashMap();
7+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package io.percy.selenium;
2+
3+
import org.openqa.selenium.Capabilities;
4+
import org.openqa.selenium.WebDriver;
5+
6+
import java.lang.reflect.Field;
7+
import java.util.*;
8+
9+
import java.util.concurrent.ConcurrentHashMap;
10+
11+
import org.openqa.selenium.remote.CommandExecutor;
12+
import org.openqa.selenium.remote.HttpCommandExecutor;
13+
import org.openqa.selenium.remote.RemoteWebDriver;
14+
15+
16+
public class DriverMetadata {
17+
18+
private String sessionId;
19+
private WebDriver driver;
20+
private ConcurrentHashMap<String, String> capabilities = new ConcurrentHashMap<String, String>();
21+
private final List<String> capsNeeded = new ArrayList<>(Arrays.asList("browserName", "platform", "platformName", "version", "osVersion", "proxy"));
22+
public DriverMetadata(WebDriver driver) {
23+
this.driver = driver;
24+
this.sessionId = ((RemoteWebDriver) driver).getSessionId().toString();
25+
}
26+
27+
public String getSessionId() {
28+
return this.sessionId;
29+
}
30+
31+
public ConcurrentHashMap<String, String> getCapabilities() {
32+
String key = "capabilities_" + this.sessionId;
33+
if (Cache.CACHE_MAP.get(key) == null) {
34+
Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();
35+
ConcurrentHashMap<String, String> capabilities = new ConcurrentHashMap<String, String>();
36+
37+
Iterator<String> iterator = capsNeeded.iterator();
38+
while (iterator.hasNext()) {
39+
String cap = iterator.next();
40+
if (caps.getCapability(cap) != null) {
41+
capabilities.put(cap, caps.getCapability(cap).toString());
42+
}
43+
}
44+
Cache.CACHE_MAP.put(key, capabilities);
45+
}
46+
return (ConcurrentHashMap<String, String>) Cache.CACHE_MAP.get(key);
47+
}
48+
49+
public String getCommandExecutorUrl() {
50+
String key = "commandExecutorUrl_" + this.sessionId;
51+
if (Cache.CACHE_MAP.get(key) == null) {
52+
CommandExecutor executor = ((RemoteWebDriver) driver).getCommandExecutor();
53+
54+
// Get HttpCommandExecutor From TracedCommandExecutor
55+
if (executor.getClass().toString().contains("TracedCommandExecutor")) {
56+
Class className = executor.getClass();
57+
try {
58+
Field field = className.getDeclaredField("delegate");
59+
// make private field accessible
60+
field.setAccessible(true);
61+
executor = (HttpCommandExecutor) field.get(executor);
62+
} catch (Exception e) {
63+
Percy.log(e.toString());
64+
return e.toString();
65+
}
66+
}
67+
String remoteWebAddress = ((HttpCommandExecutor) executor).getAddressOfRemoteServer().toString();
68+
Cache.CACHE_MAP.put(key, remoteWebAddress);
69+
}
70+
return (String) Cache.CACHE_MAP.get(key);
71+
}
72+
}

src/main/java/io/percy/selenium/Percy.java

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public class Percy {
4141
private String PERCY_SERVER_ADDRESS = System.getenv().getOrDefault("PERCY_SERVER_ADDRESS", "http://localhost:5338");
4242

4343
// Determine if we're debug logging
44-
private boolean PERCY_DEBUG = System.getenv().getOrDefault("PERCY_LOGLEVEL", "info").equals("debug");
44+
private static boolean PERCY_DEBUG = System.getenv().getOrDefault("PERCY_LOGLEVEL", "info").equals("debug");
4545

4646
// for logging
47-
private String LABEL = "[\u001b[35m" + (PERCY_DEBUG ? "percy:java" : "percy") + "\u001b[39m]";
47+
private static String LABEL = "[\u001b[35m" + (PERCY_DEBUG ? "percy:java" : "percy") + "\u001b[39m]";
4848

4949
// Is the Percy server running or not
5050
private boolean isPercyEnabled = healthcheck();
@@ -191,34 +191,10 @@ public void screenshot(String name, Map<String, Object> options) throws Unsuppor
191191
Iterator<String> driverIterator = driverArray.iterator();
192192
String driverClass = driverIterator.next();
193193

194-
String sessionId = ((RemoteWebDriver) driver).getSessionId().toString();
195-
CommandExecutor executor = ((RemoteWebDriver) driver).getCommandExecutor();
196-
197-
// Get HttpCommandExecutor From TracedCommandExecutor
198-
if (executor.getClass().toString().contains("TracedCommandExecutor")) {
199-
Class className = executor.getClass();
200-
try {
201-
Field field = className.getDeclaredField("delegate");
202-
// make private field accessible
203-
field.setAccessible(true);
204-
executor = (HttpCommandExecutor)field.get(executor);
205-
} catch (Exception e) {
206-
log(e.toString());
207-
return;
208-
}
209-
}
210-
String remoteWebAddress = ((HttpCommandExecutor) executor).getAddressOfRemoteServer().toString();
211-
212-
Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();
213-
ConcurrentHashMap<String, String> capabilities = new ConcurrentHashMap<String, String>();
214-
215-
Iterator<String> iterator = capsNeeded.iterator();
216-
while (iterator.hasNext()) {
217-
String cap = iterator.next();
218-
if (caps.getCapability(cap) != null) {
219-
capabilities.put(cap, caps.getCapability(cap).toString());
220-
}
221-
}
194+
DriverMetadata driverMetadata = new DriverMetadata(driver);
195+
String sessionId = driverMetadata.getSessionId();
196+
String remoteWebAddress = driverMetadata.getCommandExecutorUrl();
197+
ConcurrentHashMap<String, String> capabilities = driverMetadata.getCapabilities();
222198

223199
if (options.containsKey(ignoreElementAltKey)) {
224200
options.put(ignoreElementKey, options.get(ignoreElementAltKey));
@@ -401,7 +377,7 @@ private List<String> getElementIdFromElement(List<RemoteWebElement> elements) {
401377
return ignoredElementsArray;
402378
}
403379

404-
private void log(String message) {
380+
protected static void log(String message) {
405381
System.out.println(LABEL + " " + message);
406382
}
407383
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)