Skip to content

Commit

Permalink
[dotnet] add asynchronous methods to Navigation class (#14051)
Browse files Browse the repository at this point in the history
* allow driver to execute commands asynchronously

*  implement navigation commands with async tasks

* dd async navigation methods to interface and implement in event firing webdriver

* add explicit tests for navigation async methods

* use async delegate with Task.Run instead of ConfigureAwait
  • Loading branch information
titusfortner committed Jun 6, 2024
1 parent 3597ecd commit 55f7a1d
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 42 deletions.
97 changes: 77 additions & 20 deletions dotnet/src/support/Events/EventFiringWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Threading.Tasks;

namespace OpenQA.Selenium.Support.Events
{
Expand Down Expand Up @@ -845,12 +846,24 @@ public EventFiringNavigation(EventFiringWebDriver driver)
/// Move the browser back
/// </summary>
public void Back()
{
Task.Run(async delegate
{
await this.BackAsync();
}).GetAwaiter().GetResult();
}

/// <summary>
/// Move the browser back as an asynchronous task.
/// </summary>
/// <returns>A task object representing the asynchronous operation</returns>
public async Task BackAsync()
{
try
{
WebDriverNavigationEventArgs e = new WebDriverNavigationEventArgs(this.parentDriver);
this.parentDriver.OnNavigatingBack(e);
this.wrappedNavigation.Back();
await this.wrappedNavigation.BackAsync().ConfigureAwait(false);
this.parentDriver.OnNavigatedBack(e);
}
catch (Exception ex)
Expand All @@ -861,15 +874,27 @@ public void Back()
}

/// <summary>
/// Move the browser forward
/// Move a single "item" forward in the browser's history.
/// </summary>
public void Forward()
{
Task.Run(async delegate
{
await this.ForwardAsync();
}).GetAwaiter().GetResult();
}

/// <summary>
/// Move a single "item" forward in the browser's history as an asynchronous task.
/// </summary>
/// <returns>A task object representing the asynchronous operation.</returns>
public async Task ForwardAsync()
{
try
{
WebDriverNavigationEventArgs e = new WebDriverNavigationEventArgs(this.parentDriver);
this.parentDriver.OnNavigatingForward(e);
this.wrappedNavigation.Forward();
await this.wrappedNavigation.ForwardAsync().ConfigureAwait(false);
this.parentDriver.OnNavigatedForward(e);
}
catch (Exception ex)
Expand All @@ -880,16 +905,34 @@ public void Forward()
}

/// <summary>
/// Navigate to a url for your test
/// Navigate to a url.
/// </summary>
/// <param name="url">String of where you want the browser to go to</param>
public void GoToUrl(string url)
{
Task.Run(async delegate
{
await this.GoToUrlAsync(url);
}).GetAwaiter().GetResult();
}

/// <summary>
/// Navigate to a url as an asynchronous task.
/// </summary>
/// <param name="url">String of where you want the browser to go.</param>
/// <returns>A task object representing the asynchronous operation.</returns>
public async Task GoToUrlAsync(string url)
{
if (url == null)
{
throw new ArgumentNullException(nameof(url), "url cannot be null");
}

try
{
WebDriverNavigationEventArgs e = new WebDriverNavigationEventArgs(this.parentDriver, url);
this.parentDriver.OnNavigating(e);
this.wrappedNavigation.GoToUrl(url);
await this.wrappedNavigation.GoToUrlAsync(url).ConfigureAwait(false);
this.parentDriver.OnNavigated(e);
}
catch (Exception ex)
Expand All @@ -900,38 +943,52 @@ public void GoToUrl(string url)
}

/// <summary>
/// Navigate to a url for your test
/// Navigate to a url.
/// </summary>
/// <param name="url">Uri object of where you want the browser to go to</param>
public void GoToUrl(Uri url)
{
Task.Run(async delegate
{
await this.GoToUrlAsync(url);
}).GetAwaiter().GetResult();
}

/// <summary>
/// Navigate to a url as an asynchronous task.
/// </summary>
/// <param name="url">Uri object of where you want the browser to go.</param>
/// <returns>A task object representing the asynchronous operation.</returns>
public async Task GoToUrlAsync(Uri url)
{
if (url == null)
{
throw new ArgumentNullException(nameof(url), "url cannot be null");
}

try
{
WebDriverNavigationEventArgs e = new WebDriverNavigationEventArgs(this.parentDriver, url.ToString());
this.parentDriver.OnNavigating(e);
this.wrappedNavigation.GoToUrl(url);
this.parentDriver.OnNavigated(e);
}
catch (Exception ex)
{
this.parentDriver.OnException(new WebDriverExceptionEventArgs(this.parentDriver, ex));
throw;
}
await this.GoToUrlAsync(url.ToString()).ConfigureAwait(false);
}

/// <summary>
/// Refresh the browser
/// Reload the current page.
/// </summary>
public void Refresh()
{
Task.Run(async delegate
{
await this.RefreshAsync();
}).GetAwaiter().GetResult();
}

/// <summary>
/// Reload the current page as an asynchronous task.
/// </summary>
/// <returns>A task object representing the asynchronous operation.</returns>
public async Task RefreshAsync()
{
try
{
this.wrappedNavigation.Refresh();
await this.wrappedNavigation.RefreshAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
Expand Down
9 changes: 9 additions & 0 deletions dotnet/src/webdriver/ICommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// </copyright>

using System;
using System.Threading.Tasks;

namespace OpenQA.Selenium
{
Expand All @@ -39,5 +40,13 @@ public interface ICommandExecutor : IDisposable
/// <param name="commandToExecute">The command you wish to execute</param>
/// <returns>A response from the browser</returns>
Response Execute(Command commandToExecute);


/// <summary>
/// Executes a command as an asynchronous task.
/// </summary>
/// <param name="commandToExecute">The command you wish to execute</param>
/// <returns>A task object representing the asynchronous operation</returns>
Task<Response> ExecuteAsync(Command commandToExecute);
}
}
33 changes: 33 additions & 0 deletions dotnet/src/webdriver/INavigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// </copyright>

using System;
using System.Threading.Tasks;

namespace OpenQA.Selenium
{
Expand All @@ -31,12 +32,24 @@ public interface INavigation
/// </summary>
void Back();

/// <summary>
/// Move back a single entry in the browser's history as an asynchronous task.
/// </summary>
/// <returns>A task object representing the asynchronous operation.</returns>
Task BackAsync();

/// <summary>
/// Move a single "item" forward in the browser's history.
/// </summary>
/// <remarks>Does nothing if we are on the latest page viewed.</remarks>
void Forward();

/// <summary>
/// Move a single "item" forward in the browser's history as an asynchronous task.
/// </summary>
/// <returns>A task object representing the asynchronous operation.</returns>
Task ForwardAsync();

/// <summary>
/// Load a new web page in the current browser window.
/// </summary>
Expand All @@ -52,6 +65,13 @@ public interface INavigation
/// </remarks>
void GoToUrl(string url);

/// <summary>
/// Navigate to a url as an asynchronous task.
/// </summary>
/// <param name="url">String of where you want the browser to go.</param>
/// <returns>A task object representing the asynchronous operation.</returns>
Task GoToUrlAsync(string url);

/// <summary>
/// Load a new web page in the current browser window.
/// </summary>
Expand All @@ -67,9 +87,22 @@ public interface INavigation
/// </remarks>
void GoToUrl(Uri url);

/// <summary>
/// Navigate to a url as an asynchronous task.
/// </summary>
/// <param name="url">Uri object of where you want the browser to go.</param>
/// <returns>A task object representing the asynchronous operation.</returns>
Task GoToUrlAsync(Uri url);

/// <summary>
/// Refreshes the current page.
/// </summary>
void Refresh();

/// <summary>
/// Reload the current page as an asynchronous task.
/// </summary>
/// <returns>A task object representing the asynchronous operation.</returns>
Task RefreshAsync();
}
}
Loading

0 comments on commit 55f7a1d

Please sign in to comment.