Skip to content

Commit

Permalink
IgnoreRegion Options for POA
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmay-browserstack committed Jun 12, 2023
1 parent 3559053 commit e5c8843
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Percy.Test/PercyDriver.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public void getDriverDetails()
capabilities.Setup(x => x.GetCapability("platformName")).Returns("win");
capabilities.Setup(x => x.GetCapability("osVersion")).Returns("111.0");
capabilities.Setup(x => x.GetCapability("browserName")).Returns("firefox");
string url = "http://hub-cloud.browserstack.com/wd/hub";
string url = "http://hub-cloud.browserstack.com/wd/hub/";
_remoteDriver.Setup(x => x.sessionId()).Returns("123");
_remoteDriver.Setup(x => x.GetHost()).Returns(url);
_remoteDriver.Setup(x => x.GetCapabilities()).Returns(capabilities.Object);
// Setting Expectation
Dictionary<string, object> expectedResponse = new Dictionary<string, object>()
{
{ "sessionId", "123" },
{ "commandExecutorUrl", url },
{ "commandExecutorUrl", "http://hub-cloud.browserstack.com/wd/hub" },
{ "capabilities", capabilities.Object }
};
var percyDriverMock = new Mock<PercyDriver>(_remoteDriver.Object);
Expand Down
29 changes: 27 additions & 2 deletions Percy/Percy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public static class Percy
Regex.Replace(RuntimeInformation.FrameworkDescription, @"\s+", "-"),
@"-([\d\.]+).*$", "/$1").Trim().ToLower();

public static readonly string ignoreElementKey = "ignore_region_selenium_elements";

private static void Log<T>(T message)
{
string label = DEBUG ? "percy:dotnet" : "percy";
Expand Down Expand Up @@ -130,6 +132,17 @@ public static bool isDriverValid(WebDriver driver)

public class Options : Dictionary<string, object> {}

private static List<String> GetElementIdFromElements(List<IWebElement> elements) {
List<string> ignoredElementsArray = new List<string>();
for (int index = 0; index < elements.Count; index++)
{
PropertyInfo idProperty = typeof(WebElement).GetProperty("Id", BindingFlags.Instance | BindingFlags.NonPublic);
string elementId = (string)idProperty.GetValue(elements[index]);
ignoredElementsArray.Add(elementId);
}
return ignoredElementsArray;
}

public static void Snapshot(
WebDriver driver, string name,
IEnumerable<KeyValuePair<string, object>>? options = null)
Expand Down Expand Up @@ -195,8 +208,20 @@ public class Options : Dictionary<string, object> {}
}
}

if (options != null)
screenshotOptions.Add("options", options);
if (options != null) {
Dictionary<string, object> userOptions = options.ToDictionary(kv => kv.Key, kv => kv.Value);

if (userOptions.ContainsKey(ignoreElementKey)) {
var ignoreElements = userOptions[ignoreElementKey] as List<IWebElement>;

if (ignoreElements != null)
{
List<string> elementIds = GetElementIdFromElements(ignoreElements);
userOptions[ignoreElementKey] = elementIds;
}
}
screenshotOptions.Add("options", userOptions);
}

dynamic res = Request("/percy/automateScreenshot", JObject.FromObject(screenshotOptions), true);
dynamic data = JsonSerializer.Deserialize<object>(res.content);
Expand Down
2 changes: 1 addition & 1 deletion Percy/PercyDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal void setValues(IPercySeleniumDriver percySeleniumDriver)
Dictionary<string, object> payload = new Dictionary<string, object>()
{
{ "sessionId", this.sessionId },
{ "commandExecutorUrl", this.percySeleniumDriver.GetHost() },
{ "commandExecutorUrl", this.percySeleniumDriver.GetHost().TrimEnd('/') },
{ "capabilities", this.percySeleniumDriver.GetCapabilities() },
};
return payload;
Expand Down

0 comments on commit e5c8843

Please sign in to comment.