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 Browser Action Operation #4095

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ internal async Task HandleAsync()
string MessageBoxText = _act.GetInputParamCalculatedValue("Value");
await HandleSetMessageBoxTextOperationAsync(MessageBoxText);
break;

case ActBrowserElement.eControlAction.StartMonitoringNetworkLog:
await HandleStartMonitoringNetworkLogOperationAsync();
break;
Expand All @@ -170,6 +169,24 @@ internal async Task HandleAsync()
case ActBrowserElement.eControlAction.StopMonitoringNetworkLog:
await HandleStopMonitoringNetworkLogOperationAsync();
break;
case ActBrowserElement.eControlAction.InjectJS:
await HandleInjectJS();
break;
case ActBrowserElement.eControlAction.Maximize:
await HandleMaximizeWindow();
break;
case ActBrowserElement.eControlAction.SwitchToShadowDOM:
await HandleSwitchToShadowDOM();
break;
case ActBrowserElement.eControlAction.SwitchToDefaultDOM:
await HandleSwitchToDefaultDOM();
break;
case ActBrowserElement.eControlAction.SetBlockedUrls:
await HandleSetBlockedUrls();
break;
case ActBrowserElement.eControlAction.UnblockeUrls:
await HandleUnblockUrls();
break;
default:
string operationName = Common.GeneralLib.General.GetEnumValueDescription(typeof(ActBrowserElement.eControlAction), _act.ControlAction);
_act.Error = $"Operation '{operationName}' is not supported";
Expand All @@ -182,6 +199,139 @@ internal async Task HandleAsync()
}
}

/// <summary>
/// Unblocks previously blocked URLs in the current browser tab.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
private async Task HandleUnblockUrls()
{
try
{
if (!await _browser.CurrentWindow.CurrentTab.UnblockURLAsync())
{
_act.Error = "Failed to unblock the URLs";
}
}
catch (Exception ex)
{
_act.Error = $"Error unblocking URLs: {ex.Message}";
Reporter.ToLog(eLogLevel.ERROR, "Error: Failed to unblock the URLs", ex);
}
}

/// <summary>
/// Blocks specified URLs in the current browser tab.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
private async Task HandleSetBlockedUrls()
{
try
{
string sURL = _act.GetInputParamCalculatedValue("sBlockedUrls");
if (string.IsNullOrEmpty(sURL))
{
_act.Error = "Error: Provided URL is empty. Please provide valid URL.";
return;
}
if (!await _browser.CurrentWindow.CurrentTab.SetBlockedURLAsync(sURL))
{
_act.Error = "Failed to block the URLs";
}
}
catch (Exception ex)
{
_act.Error = $"Error blocking URLs: {ex.Message}";
Reporter.ToLog(eLogLevel.ERROR, "Error: Failed to block the URLs", ex);
}
}

/// <summary>
/// Switches to the default DOM in the current browser tab.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
private async Task HandleSwitchToDefaultDOM()
{
try
{
if (!await _browser.CurrentWindow.CurrentTab.SwitchToDefaultDomAsync())
{
_act.Error = "Failed to switch to default DOM";
}
}
catch (Exception ex)
{
_act.Error = $"Error switching to default DOM: {ex.Message}";
Reporter.ToLog(eLogLevel.ERROR, "Error: Failed to switch to default DOM", ex);
}
}

/// <summary>
/// Switches to the shadow DOM in the current browser tab.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
private async Task HandleSwitchToShadowDOM()
{
try
{
string locateValue = _act.LocateValueCalculated;
if (string.IsNullOrEmpty(locateValue))
{
_act.Error = "Error: Locate value is empty.";
return;
}
if (!await _browser.CurrentWindow.CurrentTab.SwitchToShadowDomAsync())
{
_act.Error = "Failed to switch to shadow DOM";
Reporter.ToLog(eLogLevel.ERROR, "Failed to switch to shadow DOM");
}
}
catch (Exception ex)
{
_act.Error = $"Error switching to shadow DOM: {ex.Message}";
Reporter.ToLog(eLogLevel.ERROR, "Error switching to shadow DOM", ex);
}
}

/// <summary>
/// Maximizes the current browser window.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
private async Task HandleMaximizeWindow()
{
try
{
await _browser.CurrentWindow.CurrentTab.MaximizeWindowAsync();
}
catch (Exception ex)
{
_act.Error = "Error: Failed to maximize the window";
Reporter.ToLog(eLogLevel.ERROR, "Error: Failed to maximize the window", ex);
}
}

/// <summary>
/// Injects JavaScript into the current browser tab.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
private async Task HandleInjectJS()
{
string script = _act.GetInputParamCalculatedValue("Value");
if (string.IsNullOrEmpty(script))
{
_act.Error = "Error: Script value is empty";
return;
}
try
{
await _browser.CurrentWindow.CurrentTab.InjectJavascriptAsync(script);
}
catch (Exception ex)
{
_act.Error = "Error: Failed to Inject the provided Javascript";
Reporter.ToLog(eLogLevel.ERROR, "Error: Failed to Inject the provided Javascript", ex);
}

}
private async Task HandleGotoUrlOperationAsync()
{
string url = GetTargetUrl();
Expand Down Expand Up @@ -656,7 +806,7 @@ private async Task HandleStartMonitoringNetworkLogOperationAsync()
{
try
{
await ((PlaywrightBrowserTab)_browser!.CurrentWindow.CurrentTab).StartCaptureNetworkLog(_act);
await ((PlaywrightBrowserTab)_browser!.CurrentWindow.CurrentTab).StartCaptureNetworkLog(_act);
}
catch (Exception ex)
{
Expand Down
8 changes: 6 additions & 2 deletions Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/IBrowserTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal interface IBrowserTab

public Task GoToURLAsync(string url);

public Task<string> TitleAsync();
public Task<string> TitleAsync();

public Task NavigateBackAsync();

Expand Down Expand Up @@ -110,6 +110,10 @@ internal interface IBrowserTab
public Task<bool> WaitForElementsPresenceAsync(eLocateBy locateBy, string locateValue, float timeout);
public Task<bool> WaitForElementsInvisibleAsync(eLocateBy locateBy, string locateValue, float timeout);
public Task<bool> WaitForElementsVisibleAsync(eLocateBy locateBy, string locateValue, float timeout);

public Task MaximizeWindowAsync();
public Task<bool> SetBlockedURLAsync(string urlPattern);
public Task<bool> UnblockURLAsync();
public Task<bool> SwitchToShadowDomAsync();
public Task<bool> SwitchToDefaultDomAsync();
GokulBothe99 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,127 @@ private void _playwrightPage_Dialog(object? sender, IDialog e)
alertDetected.TrySetResult(true);
}
}

/// <summary>
/// Maximizes the browser window to the screen's width and height.
/// </summary>
public async Task MaximizeWindowAsync()
{
await MaximizeWindowInternalAsync();
}
/// <summary>
/// Internal implementation of window maximization.
/// </summary>
private async Task MaximizeWindowInternalAsync()
{
try
{
var screenWidth = await _playwrightPage.EvaluateAsync<int>("window.screen.width");
var screenHeight = await _playwrightPage.EvaluateAsync<int>("window.screen.height");
await _playwrightPage.SetViewportSizeAsync(screenWidth, screenHeight);
}
catch (Exception ex)
{
Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {ex.Message}", ex);
throw;
}
}

/// <summary>
/// Sets the URLs to be blocked during the browser session.
/// </summary>
/// <param name="urls">Comma-separated list of URLs to be blocked.</param>
/// <returns>True if the URLs were successfully blocked, otherwise false.</returns>
public async Task<bool> SetBlockedURLAsync(string urls)
{
return await SetBlockedURLAsyncInternal(urls);
}
private async Task<bool> SetBlockedURLAsyncInternal(string urls)
{
try
{
var listURL = GetBlockedUrlsArray(urls);
foreach (var rawURL in listURL)
{
var url = rawURL?.Trim();
if (!string.IsNullOrEmpty(url))
{
if (!url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && !url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
url = "https://" + url;
}
await _playwrightPage.RouteAsync(url, async route => await route.AbortAsync());
}
}
await _playwrightPage.ReloadAsync();
return true;
}
catch (Exception ex)
{
Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {ex.Message}", ex);
throw;
}
}

GokulBothe99 marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// Splits comma-separated URLs into an array.
/// </summary>
private string[] GetBlockedUrlsArray(string urlsToBlock)
{
string[] arrBlockedUrls = Array.Empty<string>();
if (!string.IsNullOrEmpty(urlsToBlock))
{
arrBlockedUrls = urlsToBlock.Trim(',').Split(',', StringSplitOptions.RemoveEmptyEntries);
}
return arrBlockedUrls;
}

/// <summary>
/// Unblocks all previously blocked URLs during the browser session.
/// </summary>
/// <returns>True if the URLs were successfully unblocked, otherwise false.</returns>
public async Task<bool> UnblockURLAsync()
{
return await UnblockURLInternalAsync();
}

/// <summary>
/// Internal implementation of URL unblocking.
/// </summary>
private async Task<bool> UnblockURLInternalAsync()
{
try
{
await _playwrightPage.UnrouteAllAsync();
await _playwrightPage.ReloadAsync();
return true;
}
catch (Exception ex)
{
Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {ex.Message}", ex);
throw;
}
}

/// </summary>
/// <param name="locateBy">The method to locate the element.</param>
/// <param name="value">The value used to locate the element.</param>
/// <returns>True if the context was successfully switched, otherwise false.</returns>
public async Task<bool> SwitchToShadowDomAsync()
{
return await Task.FromResult(true);
}


/// <summary>
/// Switches the context back to the default DOM.
/// </summary>
/// <returns>True if the context was successfully switched, otherwise false.</returns>
public async Task<bool> SwitchToDefaultDomAsync()
{
return await Task.FromResult(true);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public bool IsActionSupported(Act act, out string message)
{
message = string.Empty;

if (act is ActWithoutDriver or ActScreenShot or ActGotoURL or ActAccessibilityTesting or ActSmartSync or ActWebSmartSync)
if (act is ActWithoutDriver or ActScreenShot or ActGotoURL or ActAccessibilityTesting or ActSmartSync or ActWebSmartSync or ActBrowserElement)
{
return true;
}
Expand Down
Loading