Skip to content

Commit

Permalink
Adding Caching for PoA Commands (#119)
Browse files Browse the repository at this point in the history
* Adding Caching for PoA Commands

* Making test changes

* Adding tests for driver metadata

---------

Co-authored-by: Abel Christopher <[email protected]>
  • Loading branch information
Amit3200 and christopher-001 authored Sep 27, 2023
1 parent 7eb2a8f commit bd7d5d7
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/main/java/io/percy/appium/PercyOnAutomate.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import io.appium.java_client.MobileElement;
import io.percy.appium.lib.CliWrapper;
import io.percy.appium.lib.PercyOptions;
import io.percy.appium.metadata.DriverMetadata;

import java.util.Map;
import java.util.List;
import java.util.ArrayList;

public class PercyOnAutomate extends IPercy {
private final AppiumDriver driver;
private final DriverMetadata driverMetadata;
private CliWrapper cliWrapper;
private final PercyOptions percyOptions;
/**
Expand All @@ -25,6 +27,7 @@ public class PercyOnAutomate extends IPercy {
*/
public PercyOnAutomate(AppiumDriver driver) {
this.driver = driver;
this.driverMetadata = new DriverMetadata(driver);
this.cliWrapper = new CliWrapper(driver);
this.percyOptions = new PercyOptions(driver);
ignoreErrors = percyOptions.setPercyIgnoreErrors();
Expand Down Expand Up @@ -58,9 +61,9 @@ public void screenshot(String name, Map<String, Object> options) {
return;
}

String sessionId = driver.getSessionId().toString();
String remoteWebAddress = driver.getRemoteAddress().toString();
Map<String, Object> capabilities = driver.getCapabilities().asMap();
String sessionId = this.driverMetadata.getSessionId();
String remoteWebAddress = this.driverMetadata.getCommandExecutorUrl();
Map<String, Object> capabilities = this.driverMetadata.getCapabilities();

String ignoreElementKey = "ignore_region_appium_elements";
String ignoreElementAltKey = "ignoreRegionAppiumElements";
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/io/percy/appium/metadata/DriverMetadata.java
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);
}
}
6 changes: 3 additions & 3 deletions src/test/java/io/percy/appium/PercyOnAutomateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public void takeScreenshot() {

@Test
public void takeScreenshotWithOptions() {
when(androidDriver.getSessionId()).thenReturn(new SessionId("123"));
when(androidDriver.getCapabilities()).thenReturn(capabilities);
lenient().when(androidDriver.getSessionId()).thenReturn(new SessionId("123"));
lenient().when(androidDriver.getCapabilities()).thenReturn(capabilities);
try {
when(androidDriver.getRemoteAddress()).thenReturn(new URL("https://hub.browserstack.com/wd/hub"));
lenient(). when(androidDriver.getRemoteAddress()).thenReturn(new URL("https://hub.browserstack.com/wd/hub"));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
Expand Down
72 changes: 72 additions & 0 deletions src/test/java/io/percy/appium/metadata/DriverMetadataTest.java
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);
}
}

0 comments on commit bd7d5d7

Please sign in to comment.