-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Caching for PoA Commands (#119)
* Adding Caching for PoA Commands * Making test changes * Adding tests for driver metadata --------- Co-authored-by: Abel Christopher <[email protected]>
- Loading branch information
1 parent
7eb2a8f
commit bd7d5d7
Showing
4 changed files
with
127 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/io/percy/appium/metadata/DriverMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.percy.appium.metadata; | ||
|
||
import io.appium.java_client.AppiumDriver; | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import io.percy.appium.lib.Cache; | ||
|
||
public class DriverMetadata { | ||
private String sessionId; | ||
private AppiumDriver driver; | ||
public DriverMetadata(AppiumDriver driver) { | ||
this.driver = driver; | ||
this.sessionId = driver.getSessionId().toString(); | ||
} | ||
|
||
public String getSessionId() { | ||
return this.sessionId; | ||
} | ||
|
||
public Map<String, Object> getCapabilities() { | ||
String key = "capabilities_" + this.sessionId; | ||
if (Cache.CACHE_MAP.get(key) == null) { | ||
Map<String, Object> capabilities = driver.getCapabilities().asMap(); | ||
Cache.CACHE_MAP.put(key, capabilities); | ||
} | ||
return (Map<String, Object>) Cache.CACHE_MAP.get(key); | ||
} | ||
|
||
public String getCommandExecutorUrl() { | ||
String key = "commandExecutorUrl_" + this.sessionId; | ||
if (Cache.CACHE_MAP.get(key) == null) { | ||
String commandExecutorUrl = driver.getRemoteAddress().toString(); | ||
Cache.CACHE_MAP.put(key, commandExecutorUrl); | ||
} | ||
return (String) Cache.CACHE_MAP.get(key); | ||
} | ||
|
||
protected void finalize() throws Throwable { | ||
Set<String> set = new HashSet<>(); | ||
set.add("capabilities_" + this.sessionId); | ||
set.add("commandExecutorUrl_" + this.sessionId); | ||
Cache.CACHE_MAP.keySet().removeAll(set); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/test/java/io/percy/appium/metadata/DriverMetadataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.percy.appium.metadata; | ||
|
||
import io.appium.java_client.android.AndroidDriver; | ||
import io.percy.appium.lib.Cache; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.openqa.selenium.Capabilities; | ||
import org.openqa.selenium.remote.DesiredCapabilities; | ||
import org.openqa.selenium.remote.Response; | ||
import org.openqa.selenium.remote.SessionId; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(org.mockito.junit.MockitoJUnitRunner.class) | ||
public class DriverMetadataTest { | ||
@Mock | ||
AndroidDriver driver; | ||
|
||
private DesiredCapabilities capabilities; | ||
|
||
DriverMetadata metadata; | ||
|
||
Response session = new Response(new SessionId("abc")); | ||
|
||
|
||
@Before | ||
public void setup() { | ||
Map<String, Object> caps = new HashMap<>(); | ||
capabilities = new DesiredCapabilities(); | ||
capabilities.setCapability("deviceName", "Samsung Galaxy S22"); | ||
capabilities.setCapability("platform", "chrome_android"); | ||
capabilities.setCapability("osName", "android"); | ||
when(driver.getCapabilities()).thenReturn(capabilities); | ||
when(driver.getSessionId()).thenReturn(new SessionId("abc")); | ||
metadata = new DriverMetadata(driver); | ||
} | ||
|
||
|
||
@After | ||
public void clearCache() { | ||
Cache.CACHE_MAP.clear(); | ||
} | ||
|
||
@Test | ||
public void testSessionId() { | ||
Assert.assertEquals(metadata.getSessionId(), "abc"); | ||
} | ||
|
||
@Test | ||
public void testCommandExecutorUrl() throws MalformedURLException { | ||
when(driver.getRemoteAddress()).thenReturn(new URL("https://localhost:4444/wd/hub")); | ||
Assert.assertEquals(metadata.getCommandExecutorUrl(), "https://localhost:4444/wd/hub"); | ||
} | ||
|
||
@Test | ||
public void testGetCapabilities(){ | ||
Map<String, Object> caps = new HashMap<>(); | ||
caps.put("deviceName", "Samsung Galaxy S22"); | ||
caps.put("platform", "chrome_android"); | ||
caps.put("osName", "android"); | ||
Assert.assertEquals(metadata.getCapabilities(), caps); | ||
} | ||
} |