Skip to content

Commit

Permalink
POA Support for ignoreRegion (#220)
Browse files Browse the repository at this point in the history
* Added logic for ignore region

* added test case

* Added validation in tests

* Possible concurrent thread fix 1

* Possible concurrent thread fix 2

* Possible concurrent thread fix 3

* Possible concurrent thread fix 4

* Possible concurrent thread fix 5
  • Loading branch information
chinmay-browserstack authored Jun 19, 2023
1 parent c8cf55b commit 3b78466
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
36 changes: 21 additions & 15 deletions src/main/java/io/percy/selenium/Percy.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,16 @@
import org.json.JSONObject;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Nullable;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.TracedCommandExecutor;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.*;

import java.lang.reflect.Field;

/**
Expand Down Expand Up @@ -64,6 +54,7 @@ public class Percy {

// Fetch following properties from capabilities
private final List<String> capsNeeded = new ArrayList<>(Arrays.asList("browserName", "platform", "version", "osVersion", "proxy"));
private final String ignoreElementKey = "ignore_region_selenium_elements";
/**
* @param driver The Selenium WebDriver object that will hold the browser
* session to snapshot.
Expand Down Expand Up @@ -229,14 +220,20 @@ public void screenshot(String name, Map<String, Object> options) throws Unsuppor
}
}

if (options.containsKey(ignoreElementKey)) {
List<String> ignoreElementIds = getElementIdFromElement((List<RemoteWebElement>) options.get(ignoreElementKey));
options.replace(ignoreElementKey, ignoreElementIds);
}

// Build a JSON object to POST back to the agent node process
JSONObject json = new JSONObject(options);
JSONObject json = new JSONObject();
json.put("sessionId", sessionId);
json.put("commandExecutorUrl", remoteWebAddress);
json.put("capabilities", capabilities);
json.put("snapshotName", name);
json.put("clientInfo", env.getClientInfo());
json.put("environmentInfo", env.getEnvironmentInfo());
json.put("options", options);

request("/percy/automateScreenshot", json, name);
}
Expand Down Expand Up @@ -378,6 +375,15 @@ private String buildSnapshotJS(Map<String, Object> options) {
return jsBuilder.toString();
}

private List<String> getElementIdFromElement(List<RemoteWebElement> elements) {
List<String> ignoredElementsArray = new ArrayList<>();
for (int index = 0; index < elements.size(); index++) {
String elementId = elements.get(index).getId();
ignoredElementsArray.add(elementId);
}
return ignoredElementsArray;
}

private void log(String message) {
System.out.println(LABEL + " " + message);
}
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/io/percy/selenium/SdkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
Expand Down Expand Up @@ -153,8 +153,10 @@ public void takeScreenshotWithOptions() {
capabilities.setCapability("browserName", "Chrome");
when(mockedDriver.getCapabilities()).thenReturn(capabilities);
Map<String, Object> options = new HashMap<String, Object>();
options.put("scope", "div");
RemoteWebElement mockedElement = mock(RemoteWebElement.class);
when(mockedElement.getId()).thenReturn("1234");
options.put("ignore_region_selenium_elements", Arrays.asList(mockedElement));
percy.screenshot("Test", options);
verify(percy).request(eq("/percy/automateScreenshot"), any(), eq("Test"));
verify(percy).request(eq("/percy/automateScreenshot"), any() , eq("Test"));
}
}

0 comments on commit 3b78466

Please sign in to comment.