From e5c88437dc2a5677bf4f919a665a8c4df93a5232 Mon Sep 17 00:00:00 2001 From: Chinmay Maheshwari Date: Mon, 12 Jun 2023 19:25:19 +0530 Subject: [PATCH] IgnoreRegion Options for POA --- Percy.Test/PercyDriver.Test.cs | 4 ++-- Percy/Percy.cs | 29 +++++++++++++++++++++++++++-- Percy/PercyDriver.cs | 2 +- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Percy.Test/PercyDriver.Test.cs b/Percy.Test/PercyDriver.Test.cs index 5025a96..3c6e320 100644 --- a/Percy.Test/PercyDriver.Test.cs +++ b/Percy.Test/PercyDriver.Test.cs @@ -19,7 +19,7 @@ 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); @@ -27,7 +27,7 @@ public void getDriverDetails() Dictionary expectedResponse = new Dictionary() { { "sessionId", "123" }, - { "commandExecutorUrl", url }, + { "commandExecutorUrl", "http://hub-cloud.browserstack.com/wd/hub" }, { "capabilities", capabilities.Object } }; var percyDriverMock = new Mock(_remoteDriver.Object); diff --git a/Percy/Percy.cs b/Percy/Percy.cs index c42523c..619d2c3 100644 --- a/Percy/Percy.cs +++ b/Percy/Percy.cs @@ -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 message) { string label = DEBUG ? "percy:dotnet" : "percy"; @@ -130,6 +132,17 @@ public static bool isDriverValid(WebDriver driver) public class Options : Dictionary {} + private static List GetElementIdFromElements(List elements) { + List ignoredElementsArray = new List(); + 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>? options = null) @@ -195,8 +208,20 @@ public class Options : Dictionary {} } } - if (options != null) - screenshotOptions.Add("options", options); + if (options != null) { + Dictionary userOptions = options.ToDictionary(kv => kv.Key, kv => kv.Value); + + if (userOptions.ContainsKey(ignoreElementKey)) { + var ignoreElements = userOptions[ignoreElementKey] as List; + + if (ignoreElements != null) + { + List elementIds = GetElementIdFromElements(ignoreElements); + userOptions[ignoreElementKey] = elementIds; + } + } + screenshotOptions.Add("options", userOptions); + } dynamic res = Request("/percy/automateScreenshot", JObject.FromObject(screenshotOptions), true); dynamic data = JsonSerializer.Deserialize(res.content); diff --git a/Percy/PercyDriver.cs b/Percy/PercyDriver.cs index d94bc11..2265238 100644 --- a/Percy/PercyDriver.cs +++ b/Percy/PercyDriver.cs @@ -19,7 +19,7 @@ internal void setValues(IPercySeleniumDriver percySeleniumDriver) Dictionary payload = new Dictionary() { { "sessionId", this.sessionId }, - { "commandExecutorUrl", this.percySeleniumDriver.GetHost() }, + { "commandExecutorUrl", this.percySeleniumDriver.GetHost().TrimEnd('/') }, { "capabilities", this.percySeleniumDriver.GetCapabilities() }, }; return payload;