From f7489835322d7d3121c21787c486c5295b222d6f Mon Sep 17 00:00:00 2001 From: Gokul Bothe Date: Thu, 26 Dec 2024 13:52:37 +0530 Subject: [PATCH 1/4] Smart sync Action Playwright support --- .../Web/ActionHandlers/ActSmartSyncHandler.cs | 86 +++++++++++++++++++ .../Web/Playwright/PlaywrightDriver.cs | 17 +++- Ginger/GingerCoreNET/RunLib/DriverBase.cs | 6 +- 3 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/ActionHandlers/ActSmartSyncHandler.cs diff --git a/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/ActionHandlers/ActSmartSyncHandler.cs b/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/ActionHandlers/ActSmartSyncHandler.cs new file mode 100644 index 0000000000..231077eaab --- /dev/null +++ b/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/ActionHandlers/ActSmartSyncHandler.cs @@ -0,0 +1,86 @@ +using GingerCore.Actions; +using GingerCore.Drivers; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; + +namespace Amdocs.Ginger.CoreNET.Drivers.CoreDrivers.Web.ActionHandlers +{ + internal class ActSmartSyncHandler + { + private ActSmartSync _actSmartSync; + private IBrowserTab _browserTab; + private IBrowserElementLocator _elementLocator; + private Act mAct; + + internal ActSmartSyncHandler(ActSmartSync actSmartSync, IBrowserTab browserTab, IBrowserElementLocator elementLocator) + { + _actSmartSync = actSmartSync; + _browserTab = browserTab; + _elementLocator = elementLocator; + } + + /// + /// Handles the smart synchronization action asynchronously. + /// + /// The action to be handled. + /// A task representing the asynchronous operation. + internal async Task HandleAsync(Act act) + { + mAct = act; + + int MaxTimeout = DriverBase.GetMaxTimeout(_actSmartSync); + Stopwatch st = new Stopwatch(); + try + { + st.Start(); + IBrowserElement element; + + switch (_actSmartSync.SmartSyncAction) + { + case ActSmartSync.eSmartSyncAction.WaitUntilDisplay: + do + { + if (st.ElapsedMilliseconds > MaxTimeout * 1000) + { + mAct.Error = "Smart Sync of WaitUntilDisplay is timeout"; + break; + } + await Task.Delay(100); + element = await GetFirstMatchingElementAsync(); + } while (element == null || !await element.IsVisibleAsync() || !await element.IsEnabledAsync()); + break; + + case ActSmartSync.eSmartSyncAction.WaitUntilDisapear: + do + { + if (st.ElapsedMilliseconds > MaxTimeout * 1000) + { + mAct.Error = "Smart Sync of WaitUntilDisapear is timeout"; + break; + } + await Task.Delay(100); + element = await GetFirstMatchingElementAsync(); + } while (element != null); + break; + } + + } + finally + { + st.Stop(); + } + } + /// + /// Gets the first matching browser element asynchronously. + /// + /// The first matching browser element. + private async Task GetFirstMatchingElementAsync() + { + IEnumerable elements = await _elementLocator.FindMatchingElements(mAct.LocateBy, mAct.LocateValue); + IBrowserElement? firstElement = elements.FirstOrDefault(); + return firstElement; + } + } +} diff --git a/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs b/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs index ee99b6bc5d..c0b22c73f6 100644 --- a/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs +++ b/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs @@ -251,6 +251,21 @@ public override void RunAction(Act act) } actAccessibilityTestingHandler.HandleAsync().Wait(); break; + case ActSmartSync actSmartSync: + ActSmartSyncHandler actSmartSyncHandler = new( + actSmartSync, + _browser.CurrentWindow.CurrentTab, + new BrowserElementLocator( + _browser.CurrentWindow.CurrentTab, + new() + { + BusinessFlow = BusinessFlow, + Environment = Environment, + POMExecutionUtils = new POMExecutionUtils(act, act.LocateValue), + Agent = BusinessFlow.CurrentActivity.CurrentAgent, + })); + actSmartSyncHandler.HandleAsync(act).Wait(); + break; default: act.Error = $"This Action is not supported for Playwright driver"; break; @@ -262,7 +277,7 @@ public bool IsActionSupported(Act act, out string message) { message = string.Empty; - if (act is ActWithoutDriver or ActScreenShot or ActGotoURL or ActAccessibilityTesting) + if (act is ActWithoutDriver or ActScreenShot or ActGotoURL or ActAccessibilityTesting or ActSmartSync) { return true; } diff --git a/Ginger/GingerCoreNET/RunLib/DriverBase.cs b/Ginger/GingerCoreNET/RunLib/DriverBase.cs index 7d14cbee50..f23103c2b4 100644 --- a/Ginger/GingerCoreNET/RunLib/DriverBase.cs +++ b/Ginger/GingerCoreNET/RunLib/DriverBase.cs @@ -300,8 +300,8 @@ public virtual double ScreenShotInitialZoom() return 0.5; } - Regex AttRegexWeb = new Regex("@[a-zA-Z]*", RegexOptions.Compiled); - Regex AttRegexMobile = new Regex("{[a-zA-Z]*}", RegexOptions.Compiled); + Regex AttRegexWeb = new("@[a-zA-Z]*", RegexOptions.Compiled); + Regex AttRegexMobile = new("{[a-zA-Z]*}", RegexOptions.Compiled); public ElementLocator GetUserDefinedCustomLocatorFromTemplates(string locatorTemplate, eLocateBy locateBy, List elementProperties) { try @@ -379,7 +379,7 @@ public ElementLocator GetUserDefinedCustomLocatorFromTemplates(string locatorTem } } - protected static int GetMaxTimeout(ActSmartSync act) + public static int GetMaxTimeout(ActSmartSync act) { try { From 86e1aa42de41ae6d77fa5f10c83fb289e0fe7632 Mon Sep 17 00:00:00 2001 From: Gokul Bothe Date: Thu, 26 Dec 2024 15:34:04 +0530 Subject: [PATCH 2/4] added if else insted switch --- .../Web/ActionHandlers/ActSmartSyncHandler.cs | 47 +++++++++---------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/ActionHandlers/ActSmartSyncHandler.cs b/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/ActionHandlers/ActSmartSyncHandler.cs index 231077eaab..6d0ab8e646 100644 --- a/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/ActionHandlers/ActSmartSyncHandler.cs +++ b/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/ActionHandlers/ActSmartSyncHandler.cs @@ -37,35 +37,32 @@ internal async Task HandleAsync(Act act) st.Start(); IBrowserElement element; - switch (_actSmartSync.SmartSyncAction) + if (_actSmartSync.SmartSyncAction == ActSmartSync.eSmartSyncAction.WaitUntilDisplay) { - case ActSmartSync.eSmartSyncAction.WaitUntilDisplay: - do + do + { + if (st.ElapsedMilliseconds > MaxTimeout * 1000) { - if (st.ElapsedMilliseconds > MaxTimeout * 1000) - { - mAct.Error = "Smart Sync of WaitUntilDisplay is timeout"; - break; - } - await Task.Delay(100); - element = await GetFirstMatchingElementAsync(); - } while (element == null || !await element.IsVisibleAsync() || !await element.IsEnabledAsync()); - break; - - case ActSmartSync.eSmartSyncAction.WaitUntilDisapear: - do + mAct.Error = "Smart Sync of WaitUntilDisplay is timeout"; + break; + } + await Task.Delay(100); + element = await GetFirstMatchingElementAsync(); + } while (element == null || !await element.IsVisibleAsync() || !await element.IsEnabledAsync()); + } + else if (_actSmartSync.SmartSyncAction == ActSmartSync.eSmartSyncAction.WaitUntilDisapear) + { + do + { + if (st.ElapsedMilliseconds > MaxTimeout * 1000) { - if (st.ElapsedMilliseconds > MaxTimeout * 1000) - { - mAct.Error = "Smart Sync of WaitUntilDisapear is timeout"; - break; - } - await Task.Delay(100); - element = await GetFirstMatchingElementAsync(); - } while (element != null); - break; + mAct.Error = "Smart Sync of WaitUntilDisapear is timeout"; + break; + } + await Task.Delay(100); + element = await GetFirstMatchingElementAsync(); + } while (element != null); } - } finally { From c161a9c0503be1862484154f64910bc52ccb5fc1 Mon Sep 17 00:00:00 2001 From: Gokul Bothe Date: Thu, 26 Dec 2024 18:40:02 +0530 Subject: [PATCH 3/4] Handled the driver time out --- .../Web/ActionHandlers/ActSmartSyncHandler.cs | 36 ++++++++++++++----- .../Web/Playwright/PlaywrightDriver.cs | 23 ++++++++++-- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/ActionHandlers/ActSmartSyncHandler.cs b/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/ActionHandlers/ActSmartSyncHandler.cs index 6d0ab8e646..9b26aaad81 100644 --- a/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/ActionHandlers/ActSmartSyncHandler.cs +++ b/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/ActionHandlers/ActSmartSyncHandler.cs @@ -1,5 +1,23 @@ -using GingerCore.Actions; -using GingerCore.Drivers; +#region License +/* +Copyright © 2014-2024 European Support Limited + +Licensed under the Apache License, Version 2.0 (the "License") +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +#endregion +using Amdocs.Ginger.Common; +using GingerCore.Actions; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -26,11 +44,9 @@ internal ActSmartSyncHandler(ActSmartSync actSmartSync, IBrowserTab browserTab, /// /// The action to be handled. /// A task representing the asynchronous operation. - internal async Task HandleAsync(Act act) + internal async Task HandleAsync(Act act, int timeout) { mAct = act; - - int MaxTimeout = DriverBase.GetMaxTimeout(_actSmartSync); Stopwatch st = new Stopwatch(); try { @@ -41,7 +57,7 @@ internal async Task HandleAsync(Act act) { do { - if (st.ElapsedMilliseconds > MaxTimeout * 1000) + if (st.ElapsedMilliseconds > timeout) { mAct.Error = "Smart Sync of WaitUntilDisplay is timeout"; break; @@ -54,16 +70,20 @@ internal async Task HandleAsync(Act act) { do { - if (st.ElapsedMilliseconds > MaxTimeout * 1000) + if (st.ElapsedMilliseconds > timeout) { mAct.Error = "Smart Sync of WaitUntilDisapear is timeout"; break; } await Task.Delay(100); element = await GetFirstMatchingElementAsync(); - } while (element != null); + } while (element != null || (element != null && !await element.IsVisibleAsync())); } } + catch (Exception ex) + { + Reporter.ToLog(eLogLevel.ERROR, "Failed to handle Smart Sync", ex); + } finally { st.Stop(); diff --git a/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs b/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs index c0b22c73f6..afe11ade0d 100644 --- a/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs +++ b/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs @@ -31,6 +31,7 @@ limitations under the License. using GingerCore.Actions; using GingerCore.Actions.Common; using GingerCore.Actions.VisualTesting; +using GingerCore.Drivers; using GingerCore.Drivers.Common; using GingerCoreNET.SolutionRepositoryLib.RepositoryObjectsLib.PlatformsLib; using HtmlAgilityPack; @@ -82,12 +83,13 @@ public override string GetDriverConfigsEditPageName(Agent.eDriverType driverSubT return null; } } + PlaywrightBrowser.Options browserOptions; public override void StartDriver() { ValidateBrowserTypeSupport(BrowserType); IPlaywright playwright = Microsoft.Playwright.Playwright.CreateAsync().Result; - PlaywrightBrowser.Options browserOptions = BuildPlaywrightBrowserOptions(); + browserOptions = BuildPlaywrightBrowserOptions(); if (BrowserPrivateMode) { _browser = new PlaywrightNonPersistentBrowser(playwright, BrowserType, browserOptions, OnBrowserClose); @@ -264,7 +266,24 @@ public override void RunAction(Act act) POMExecutionUtils = new POMExecutionUtils(act, act.LocateValue), Agent = BusinessFlow.CurrentActivity.CurrentAgent, })); - actSmartSyncHandler.HandleAsync(act).Wait(); + float? driverDefaultTimeout = browserOptions.Timeout; + try + { + int smartSyncTimeout = DriverBase.GetMaxTimeout(actSmartSync) * 1000; + if (smartSyncTimeout != browserOptions.Timeout) + { + browserOptions.Timeout = smartSyncTimeout; + } + actSmartSyncHandler.HandleAsync(act, smartSyncTimeout).Wait(); + } + catch (Exception ex) + { + act.Error = ex.Message; + } + finally + { + browserOptions.Timeout = driverDefaultTimeout; + } break; default: act.Error = $"This Action is not supported for Playwright driver"; From cad2dff84317611e476352780ac075f4f56ce6e9 Mon Sep 17 00:00:00 2001 From: Gokul Bothe Date: Thu, 26 Dec 2024 19:31:06 +0530 Subject: [PATCH 4/4] changes --- .../Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs b/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs index afe11ade0d..73ce81f4e2 100644 --- a/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs +++ b/Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs @@ -270,10 +270,7 @@ public override void RunAction(Act act) try { int smartSyncTimeout = DriverBase.GetMaxTimeout(actSmartSync) * 1000; - if (smartSyncTimeout != browserOptions.Timeout) - { - browserOptions.Timeout = smartSyncTimeout; - } + browserOptions.Timeout = smartSyncTimeout; actSmartSyncHandler.HandleAsync(act, smartSyncTimeout).Wait(); } catch (Exception ex)