Skip to content

Commit

Permalink
code re factor and updated chrome devtool protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
prashelke committed Nov 29, 2024
1 parent bffff0d commit 0dd78e1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 33 deletions.
52 changes: 35 additions & 17 deletions Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/BrowserHelper.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
using amdocs.ginger.GingerCoreNET;
using Amdocs.Ginger.Common;
using GingerCore.Actions;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Amdocs.Ginger.CoreNET.Drivers.CoreDrivers.Web
{
internal class BrowserHelper
{
ActBrowserElement _act;
public BrowserHelper() {

private readonly ActBrowserElement _act;
public BrowserHelper(ActBrowserElement act) {
_act = act;
}

public bool IsToMonitorAllUrls(ActBrowserElement act)
public bool ShouldMonitorAllUrls()
{
_act = act;
return _act.GetOrCreateInputParam(nameof(ActBrowserElement.eMonitorUrl)).Value == nameof(ActBrowserElement.eMonitorUrl.AllUrl);
}

public bool IsToMonitorOnlySelectedUrls(ActBrowserElement act,string requestUrl)
public bool ShouldMonitorUrl(string requestUrl)
{
return _act.GetOrCreateInputParam(nameof(ActBrowserElement.eMonitorUrl)).Value == nameof(ActBrowserElement.eMonitorUrl.SelectedUrl)
&& _act.UpdateOperationInputValues != null
Expand All @@ -32,26 +33,43 @@ public bool IsToMonitorOnlySelectedUrls(ActBrowserElement act,string requestUrl)

public string CreateNetworkLogFile(string Filename, List<Tuple<string, object>> networkLogList)
{
string FullFilePath = string.Empty;
string FullDirectoryPath = System.IO.Path.Combine(WorkSpace.Instance.Solution.Folder, "Documents", "NetworkLog");
if (!System.IO.Directory.Exists(FullDirectoryPath))
if (string.IsNullOrEmpty(Filename))
{
System.IO.Directory.CreateDirectory(FullDirectoryPath);
Reporter.ToLog(eLogLevel.INFO, $"Method - {MethodBase.GetCurrentMethod().Name}, Filename should not be empty");
}

FullFilePath = FullDirectoryPath + @"\" + Filename + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Millisecond.ToString() + ".har";
if (!System.IO.File.Exists(FullFilePath))
if (networkLogList == null)
{
string FileContent = JsonConvert.SerializeObject(networkLogList.Select(x => x.Item2).ToList());

using (Stream fileStream = System.IO.File.Create(FullFilePath))
Reporter.ToLog(eLogLevel.INFO, $"Method - {MethodBase.GetCurrentMethod().Name}, networkLogList should not be empty");
}
string FullFilePath = string.Empty;
try
{
string FullDirectoryPath = System.IO.Path.Combine(WorkSpace.Instance.Solution.Folder, "Documents", "NetworkLog");
if (!System.IO.Directory.Exists(FullDirectoryPath))
{
fileStream.Close();
System.IO.Directory.CreateDirectory(FullDirectoryPath);
}
System.IO.File.WriteAllText(FullFilePath, FileContent);

FullFilePath = FullDirectoryPath + @"\" + Filename + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Millisecond.ToString() + ".har";
if (!System.IO.File.Exists(FullFilePath))
{
string FileContent = JsonConvert.SerializeObject(networkLogList.Select(x => x.Item2).ToList());

using (Stream fileStream = System.IO.File.Create(FullFilePath))
{
fileStream.Close();
}
System.IO.File.WriteAllText(FullFilePath, FileContent);
}
}
catch(Exception ex)
{
Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name} Error: {ex.Message}", ex);
}
return FullFilePath;


}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ internal PlaywrightBrowserTab(IPlaywrightPage playwrightPage, IBrowserTab.OnTabC
_playwrightPage.Console += OnConsoleMessage;
_playwrightPage.Close += OnPlaywrightPageClose;
_playwrightPage.Dialog += OnPlaywrightDialog;
_BrowserHelper = new BrowserHelper();
}

private void RemoveEventHandlers()
Expand Down Expand Up @@ -630,11 +629,12 @@ public static bool IsFrameLocatorSupported(eLocateBy locateBy)
/// <returns></returns>
public async Task StartCaptureNetworkLog(ActBrowserElement act)
{
_act = act;
_BrowserHelper = new BrowserHelper(act);
try
{
await Task.Run(() =>
{
_act = act;
networkRequestLogList = new List<Tuple<string, object>>();
networkResponseLogList = new List<Tuple<string, object>>();
NetworkResponseList = new ObservableList<IResponse>();
Expand All @@ -657,11 +657,12 @@ await Task.Run(() =>
/// <returns></returns>
public async Task GetCaptureNetworkLog(ActBrowserElement act)
{
_act = act;
_BrowserHelper = new BrowserHelper(act);
try
{
await Task.Run(() =>
{
_act = act;
if (isNetworkLogMonitoringStarted)
{
act.AddOrUpdateReturnParamActual("Raw Request", Newtonsoft.Json.JsonConvert.SerializeObject(networkRequestLogList.Select(x => x.Item2).ToList(), Formatting.Indented));
Expand Down Expand Up @@ -696,11 +697,12 @@ await Task.Run(() =>

public async Task StopCaptureNetworkLog(ActBrowserElement act)
{
_act = act;
_BrowserHelper = new BrowserHelper(act);
try
{
await Task.Run(() =>
{
_act = act;
_playwrightPage.Request -= OnNetworkRequestSent;
_playwrightPage.Response -= OnNetworkResponseReceived;
isNetworkLogMonitoringStarted = false;
Expand Down Expand Up @@ -745,7 +747,7 @@ private async void OnNetworkRequestSent(object? sender, IRequest request)
{
try
{
if (_BrowserHelper.IsToMonitorAllUrls(_act) || _BrowserHelper.IsToMonitorOnlySelectedUrls(_act, request.Url))
if (_BrowserHelper.ShouldMonitorAllUrls() || _BrowserHelper.ShouldMonitorUrl( request.Url))
{
networkRequestLogList.Add(new Tuple<string, object>($"RequestUrl:{ request.Url}", JsonConvert.SerializeObject(request, Formatting.Indented,
new JsonSerializerSettings
Expand Down Expand Up @@ -774,7 +776,7 @@ private async void OnNetworkResponseReceived(object? sender, IResponse response)
if (response != null)
{
string monitorType = _act.GetOrCreateInputParam(nameof(ActBrowserElement.eMonitorUrl)).Value;
if (_BrowserHelper.IsToMonitorAllUrls(_act) || _BrowserHelper.IsToMonitorOnlySelectedUrls(_act,response.Url))
if (_BrowserHelper.ShouldMonitorAllUrls() || _BrowserHelper.ShouldMonitorUrl(response.Url))
{
if (_act.GetOrCreateInputParam(nameof(ActBrowserElement.eRequestTypes)).Value == ActBrowserElement.eRequestTypes.FetchOrXHR.ToString())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ limitations under the License.
using System.Threading;
using System.Threading.Tasks;
using static GingerCoreNET.GeneralLib.General;
using DevToolsDomains = OpenQA.Selenium.DevTools.V125.DevToolsSessionDomains;
using DevToolsDomains = OpenQA.Selenium.DevTools.V127.DevToolsSessionDomains; //.V125.DevToolsSessionDomains;



Expand All @@ -88,7 +88,7 @@ public class SeleniumDriver : GingerWebDriver, IVirtualDriver, IWindowExplorer,
INetwork interceptor;
public bool isNetworkLogMonitoringStarted = false;
ActBrowserElement mAct;
BrowserHelper _BrowserHelper = new BrowserHelper();
BrowserHelper _BrowserHelper;
private int mDriverProcessId = 0;
private readonly ShadowDOM shadowDOM = new();
private const string CHROME_DRIVER_NAME = "chromedriver";
Expand Down Expand Up @@ -8391,6 +8391,7 @@ private void HandleSwitchToDefaultDOM()

public async void ActBrowserElementHandler(ActBrowserElement act)
{
_BrowserHelper = new BrowserHelper(act);
try
{
string AgentType = GetAgentAppName();
Expand Down Expand Up @@ -10892,9 +10893,9 @@ private void SetUPDevTools(IWebDriver webDriver)
try
{
//DevTool Session
devToolsSession = devTools.GetDevToolsSession(125);
devToolsSession = devTools.GetDevToolsSession(127);
devToolsDomains = devToolsSession.GetVersionSpecificDomains<DevToolsDomains>();
devToolsDomains.Network.Enable(new OpenQA.Selenium.DevTools.V125.Network.EnableCommandSettings());
devToolsDomains.Network.Enable(new OpenQA.Selenium.DevTools.V127.Network.EnableCommandSettings());
blockOrUnblockUrls();
}
catch (Exception ex)
Expand All @@ -10920,11 +10921,11 @@ private void blockOrUnblockUrls()
{
if (mAct.ControlAction == ActBrowserElement.eControlAction.SetBlockedUrls)
{
devToolsDomains.Network.SetBlockedURLs(new OpenQA.Selenium.DevTools.V125.Network.SetBlockedURLsCommandSettings() { Urls = getBlockedUrlsArray(mAct.GetInputParamCalculatedValue("sBlockedUrls")) });
devToolsDomains.Network.SetBlockedURLs(new OpenQA.Selenium.DevTools.V127.Network.SetBlockedURLsCommandSettings() { Urls = getBlockedUrlsArray(mAct.GetInputParamCalculatedValue("sBlockedUrls")) });
}
else if (mAct.ControlAction == ActBrowserElement.eControlAction.UnblockeUrls)
{
devToolsDomains.Network.SetBlockedURLs(new OpenQA.Selenium.DevTools.V125.Network.SetBlockedURLsCommandSettings() { Urls = new string[] { } });
devToolsDomains.Network.SetBlockedURLs(new OpenQA.Selenium.DevTools.V127.Network.SetBlockedURLsCommandSettings() { Urls = new string[] { } });
}
Thread.Sleep(300);
}
Expand Down Expand Up @@ -11028,7 +11029,7 @@ public async Task StopMonitoringNetworkLog(ActBrowserElement act)
act.AddOrUpdateReturnParamActual(nameof(act.ControlAction) + " " + val.Item1.ToString(), Convert.ToString(val.Item2));
}

await devToolsDomains.Network.Disable(new OpenQA.Selenium.DevTools.V125.Network.DisableCommandSettings());
await devToolsDomains.Network.Disable(new OpenQA.Selenium.DevTools.V127.Network.DisableCommandSettings());
devToolsSession.Dispose();
devTools.CloseDevToolsSession();

Expand Down Expand Up @@ -11092,7 +11093,7 @@ private void OnNetworkRequestSent(object sender, NetworkRequestSentEventArgs e)
{
try
{
if (_BrowserHelper.IsToMonitorAllUrls(mAct) || _BrowserHelper.IsToMonitorOnlySelectedUrls(mAct, e.RequestUrl))
if (_BrowserHelper.ShouldMonitorAllUrls() || _BrowserHelper.ShouldMonitorUrl( e.RequestUrl))
{
networkRequestLogList.Add(new Tuple<string, object>($"RequestUrl: {e.RequestUrl}", JsonConvert.SerializeObject(e, Formatting.Indented)));
}
Expand All @@ -11111,7 +11112,7 @@ private void OnNetworkResponseReceived(object sender, NetworkResponseReceivedEve
{
string monitorType = mAct.GetOrCreateInputParam(nameof(ActBrowserElement.eMonitorUrl)).Value;

if (_BrowserHelper.IsToMonitorAllUrls(mAct) || _BrowserHelper.IsToMonitorOnlySelectedUrls(mAct, e.ResponseUrl))
if (_BrowserHelper.ShouldMonitorAllUrls() || _BrowserHelper.ShouldMonitorUrl(e.ResponseUrl))
{
if (mAct.GetOrCreateInputParam(nameof(ActBrowserElement.eRequestTypes)).Value == ActBrowserElement.eRequestTypes.FetchOrXHR.ToString())
{
Expand All @@ -11122,7 +11123,7 @@ private void OnNetworkResponseReceived(object sender, NetworkResponseReceivedEve
}
else
{
networkResponseLogList.Add(new Tuple<string, object>($"ResponseUrl:{e.ResponseUrl}", JsonConvert.SerializeObject(e, Formatting.Indented)));
networkResponseLogList.Add(new Tuple<string, object>($"ResponseUrl:{e.ResponseUrl}", JsonConvert.SerializeObject(e, Formatting.Indented)));
}
}
}
Expand Down

0 comments on commit 0dd78e1

Please sign in to comment.