Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playwright Extended Support for Network Log ,Alert message and interp… #4010

Merged
merged 9 commits into from
Nov 29, 2024
72 changes: 36 additions & 36 deletions Ginger/GingerCoreNET/AnalyzerLib/AnalyzeAction.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ limitations under the License.
using Amdocs.Ginger.Common;
using Amdocs.Ginger.Common.UIElement;
using Amdocs.Ginger.CoreNET.Drivers.CoreDrivers.Web.Exceptions;
using Amdocs.Ginger.CoreNET.Drivers.CoreDrivers.Web.Playwright;
using Amdocs.Ginger.Repository;
using GingerCore;
using GingerCore.Actions;
using GingerCore.Environments;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
Expand Down Expand Up @@ -145,6 +147,29 @@ internal async Task HandleAsync()
case ActBrowserElement.eControlAction.SwitchToDefaultWindow:
await HandleSwitchToDefaultWindowOperationAsync();
break;
case ActBrowserElement.eControlAction.AcceptMessageBox:
await HandleAcceptMessageBoxOperationAsync();
break;
case ActBrowserElement.eControlAction.DismissMessageBox:
await HandleDismissMessageBoxOperationAsync();
break;
case ActBrowserElement.eControlAction.GetMessageBoxText:
_act.AddOrUpdateReturnParamActual("Actual", HandleGetMessageBoxTextOperation());
break;
case ActBrowserElement.eControlAction.SetAlertBoxText:
string MessageBoxText = _act.GetInputParamCalculatedValue("Value");
await HandleSetMessageBoxTextOperationAsync(MessageBoxText);
break;

case ActBrowserElement.eControlAction.StartMonitoringNetworkLog:
await HandleStartMonitoringNetworkLogOperationAsync();
break;
case ActBrowserElement.eControlAction.GetNetworkLog:
await HandleGetNetworkLogOperationAsync();
break;
case ActBrowserElement.eControlAction.StopMonitoringNetworkLog:
await HandleStopMonitoringNetworkLogOperationAsync();
break;
default:
string operationName = Common.GeneralLib.General.GetEnumValueDescription(typeof(ActBrowserElement.eControlAction), _act.ControlAction);
_act.Error = $"Operation '{operationName}' is not supported";
Expand Down Expand Up @@ -538,5 +563,132 @@ private async Task HandleSwitchToDefaultWindowOperationAsync()

await _browser.CurrentWindow.SetTabAsync(firstTab);
}
/// <summary>
/// This asynchronous method accepts a message box (such as a dialog box) in the current browser tab.
/// If an error occurs, it logs the error and updates the Error property.
/// </summary>
/// <returns></returns>
private async Task HandleAcceptMessageBoxOperationAsync()
{
try
{
await ((PlaywrightBrowserTab)_browser!.CurrentWindow.CurrentTab).AcceptMessageBox();
prashelke marked this conversation as resolved.
Show resolved Hide resolved
}
catch (Exception ex)
{
_act.Error = $"Error when handling message box operation.";
Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {ex.Message}", ex);
return;
}
}
/// <summary>
/// This asynchronous method dismisses a message box (closes it) in the current browser tab.
/// If an error occurs, it logs the error and updates the Error property.
/// </summary>
/// <returns></returns>
private async Task HandleDismissMessageBoxOperationAsync()
{
try
{
await ((PlaywrightBrowserTab)_browser!.CurrentWindow.CurrentTab).DismissMessageBox();
}
catch (Exception ex)
{
_act.Error = $"Error when handling message box operation.";
Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {ex.Message}", ex);
return;
}
}
/// <summary>
/// This method retrieves the text of the message box from the current browser tab.
/// If an error occurs, it logs the error and returns an empty string.

/// </summary>
/// <returns></returns>
private string HandleGetMessageBoxTextOperation()
{
try
{
return ((PlaywrightBrowserTab)_browser!.CurrentWindow.CurrentTab).GetMessageBoxText();
}
catch (Exception ex)
{
_act.Error = $"Error when handling message box operation.";
Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {ex.Message}", ex);
return string.Empty;
}
}
/// <summary>
/// This asynchronous method sets the text of the message box in the current browser tab.
/// If an error occurs, it logs the error.
/// </summary>
/// <param name="MessageBoxText"></param>
/// <returns></returns>
private async Task HandleSetMessageBoxTextOperationAsync(string MessageBoxText)
{
try
{
await ((PlaywrightBrowserTab)_browser!.CurrentWindow.CurrentTab).SetMessageBoxText(MessageBoxText);
}
catch (Exception ex)
{
_act.Error = $"Error when handling message box operation.";
Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {ex.Message}", ex);
}
}
/// <summary>
/// This asynchronous method starts monitoring and capturing network logs in the current browser tab. If an error occurs,
/// it logs the error and updates the Error property.
/// </summary>
/// <returns></returns>
private async Task HandleStartMonitoringNetworkLogOperationAsync()
{
try
{
await ((PlaywrightBrowserTab)_browser!.CurrentWindow.CurrentTab).StartCaptureNetworkLog(_act);
}
catch (Exception ex)
{
_act.Error = $"Error when handling Network log operation.";
Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {ex.Message}", ex);
return;
}
}
/// <summary>
/// This asynchronous method retrieves the captured network logs from the current browser tab.
/// If an error occurs, it logs the error and updates the Error property.
/// </summary>
/// <returns></returns>
private async Task HandleGetNetworkLogOperationAsync()
{
try
{
await ((PlaywrightBrowserTab)_browser!.CurrentWindow.CurrentTab).GetCaptureNetworkLog(_act);
}
catch (Exception ex)
{
_act.Error = $"Error when handling Network log operation.";
Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {ex.Message}", ex);
return;
}
}
/// <summary>
/// This asynchronous method stops monitoring and capturing network logs in the current browser tab.
/// If an error occurs, it logs the error and updates the Error property.
/// </summary>
/// <returns></returns>
private async Task HandleStopMonitoringNetworkLogOperationAsync()
{
try
{
await ((PlaywrightBrowserTab)_browser!.CurrentWindow.CurrentTab).StopCaptureNetworkLog(_act);
}
catch (Exception ex)
{
_act.Error = $"Error when handling Network log operation.";
Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {ex.Message}", ex);
return;
}
}
}
}
75 changes: 75 additions & 0 deletions Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/BrowserHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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
{
private readonly ActBrowserElement _act;
public BrowserHelper(ActBrowserElement act) {
_act = act;
}

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

public bool ShouldMonitorUrl(string requestUrl)
{
return _act.GetOrCreateInputParam(nameof(ActBrowserElement.eMonitorUrl)).Value == nameof(ActBrowserElement.eMonitorUrl.SelectedUrl)
&& _act.UpdateOperationInputValues != null
&& _act.UpdateOperationInputValues.Any(x => !string.IsNullOrEmpty(x.ValueForDriver) && requestUrl.ToLower().Contains(x.ValueForDriver.ToLower()));
}

public string CreateNetworkLogFile(string Filename, List<Tuple<string, object>> networkLogList)
{
if (string.IsNullOrEmpty(Filename))
{
Reporter.ToLog(eLogLevel.INFO, $"Method - {MethodBase.GetCurrentMethod().Name}, Filename should not be empty");
}

if (networkLogList == null)
{
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))
{
System.IO.Directory.CreateDirectory(FullDirectoryPath);
}

FullFilePath = $"FullDirectoryPath{Path.PathSeparator}{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;


}
}
}
Loading
Loading