Skip to content

Commit aa484e9

Browse files
committed
[dotnet] add async navigation methods to interface and implement in event firing webdriver
1 parent 8872c88 commit aa484e9

File tree

3 files changed

+102
-24
lines changed

3 files changed

+102
-24
lines changed

dotnet/src/support/Events/EventFiringWebDriver.cs

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Collections.Generic;
2121
using System.Collections.ObjectModel;
2222
using System.Drawing;
23+
using System.Threading.Tasks;
2324

2425
namespace OpenQA.Selenium.Support.Events
2526
{
@@ -845,12 +846,21 @@ public EventFiringNavigation(EventFiringWebDriver driver)
845846
/// Move the browser back
846847
/// </summary>
847848
public void Back()
849+
{
850+
Task.Run(this.BackAsync).GetAwaiter().GetResult();
851+
}
852+
853+
/// <summary>
854+
/// Move the browser back as an asynchronous task.
855+
/// </summary>
856+
/// <returns>A task object representing the asynchronous operation</returns>
857+
public async Task BackAsync()
848858
{
849859
try
850860
{
851861
WebDriverNavigationEventArgs e = new WebDriverNavigationEventArgs(this.parentDriver);
852862
this.parentDriver.OnNavigatingBack(e);
853-
this.wrappedNavigation.Back();
863+
await this.wrappedNavigation.BackAsync().ConfigureAwait(false);
854864
this.parentDriver.OnNavigatedBack(e);
855865
}
856866
catch (Exception ex)
@@ -861,15 +871,24 @@ public void Back()
861871
}
862872

863873
/// <summary>
864-
/// Move the browser forward
874+
/// Move a single "item" forward in the browser's history.
865875
/// </summary>
866876
public void Forward()
877+
{
878+
Task.Run(this.ForwardAsync).GetAwaiter().GetResult();
879+
}
880+
881+
/// <summary>
882+
/// Move a single "item" forward in the browser's history as an asynchronous task.
883+
/// </summary>
884+
/// <returns>A task object representing the asynchronous operation.</returns>
885+
public async Task ForwardAsync()
867886
{
868887
try
869888
{
870889
WebDriverNavigationEventArgs e = new WebDriverNavigationEventArgs(this.parentDriver);
871890
this.parentDriver.OnNavigatingForward(e);
872-
this.wrappedNavigation.Forward();
891+
await this.wrappedNavigation.ForwardAsync().ConfigureAwait(false);
873892
this.parentDriver.OnNavigatedForward(e);
874893
}
875894
catch (Exception ex)
@@ -880,16 +899,31 @@ public void Forward()
880899
}
881900

882901
/// <summary>
883-
/// Navigate to a url for your test
902+
/// Navigate to a url.
884903
/// </summary>
885904
/// <param name="url">String of where you want the browser to go to</param>
886905
public void GoToUrl(string url)
887906
{
907+
Task.Run(() => this.GoToUrlAsync(url)).GetAwaiter().GetResult();
908+
}
909+
910+
/// <summary>
911+
/// Navigate to a url as an asynchronous task.
912+
/// </summary>
913+
/// <param name="url">String of where you want the browser to go.</param>
914+
/// <returns>A task object representing the asynchronous operation.</returns>
915+
public async Task GoToUrlAsync(string url)
916+
{
917+
if (url == null)
918+
{
919+
throw new ArgumentNullException(nameof(url), "url cannot be null");
920+
}
921+
888922
try
889923
{
890924
WebDriverNavigationEventArgs e = new WebDriverNavigationEventArgs(this.parentDriver, url);
891925
this.parentDriver.OnNavigating(e);
892-
this.wrappedNavigation.GoToUrl(url);
926+
await this.wrappedNavigation.GoToUrlAsync(url).ConfigureAwait(false);
893927
this.parentDriver.OnNavigated(e);
894928
}
895929
catch (Exception ex)
@@ -900,38 +934,46 @@ public void GoToUrl(string url)
900934
}
901935

902936
/// <summary>
903-
/// Navigate to a url for your test
937+
/// Navigate to a url.
904938
/// </summary>
905939
/// <param name="url">Uri object of where you want the browser to go to</param>
906940
public void GoToUrl(Uri url)
941+
{
942+
Task.Run(() => this.GoToUrlAsync(url)).GetAwaiter().GetResult();
943+
}
944+
945+
/// <summary>
946+
/// Navigate to a url as an asynchronous task.
947+
/// </summary>
948+
/// <param name="url">Uri object of where you want the browser to go.</param>
949+
/// <returns>A task object representing the asynchronous operation.</returns>
950+
public async Task GoToUrlAsync(Uri url)
907951
{
908952
if (url == null)
909953
{
910954
throw new ArgumentNullException(nameof(url), "url cannot be null");
911955
}
912956

913-
try
914-
{
915-
WebDriverNavigationEventArgs e = new WebDriverNavigationEventArgs(this.parentDriver, url.ToString());
916-
this.parentDriver.OnNavigating(e);
917-
this.wrappedNavigation.GoToUrl(url);
918-
this.parentDriver.OnNavigated(e);
919-
}
920-
catch (Exception ex)
921-
{
922-
this.parentDriver.OnException(new WebDriverExceptionEventArgs(this.parentDriver, ex));
923-
throw;
924-
}
957+
await this.GoToUrlAsync(url.ToString()).ConfigureAwait(false);
925958
}
926959

927960
/// <summary>
928-
/// Refresh the browser
961+
/// Reload the current page.
929962
/// </summary>
930963
public void Refresh()
964+
{
965+
Task.Run(this.RefreshAsync).GetAwaiter().GetResult();
966+
}
967+
968+
/// <summary>
969+
/// Reload the current page as an asynchronous task.
970+
/// </summary>
971+
/// <returns>A task object representing the asynchronous operation.</returns>
972+
public async Task RefreshAsync()
931973
{
932974
try
933975
{
934-
this.wrappedNavigation.Refresh();
976+
await this.wrappedNavigation.RefreshAsync().ConfigureAwait(false);
935977
}
936978
catch (Exception ex)
937979
{

dotnet/src/webdriver/INavigation.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// </copyright>
1818

1919
using System;
20+
using System.Threading.Tasks;
2021

2122
namespace OpenQA.Selenium
2223
{
@@ -31,12 +32,24 @@ public interface INavigation
3132
/// </summary>
3233
void Back();
3334

35+
/// <summary>
36+
/// Move back a single entry in the browser's history as an asynchronous task.
37+
/// </summary>
38+
/// <returns>A task object representing the asynchronous operation.</returns>
39+
Task BackAsync();
40+
3441
/// <summary>
3542
/// Move a single "item" forward in the browser's history.
3643
/// </summary>
3744
/// <remarks>Does nothing if we are on the latest page viewed.</remarks>
3845
void Forward();
3946

47+
/// <summary>
48+
/// Move a single "item" forward in the browser's history as an asynchronous task.
49+
/// </summary>
50+
/// <returns>A task object representing the asynchronous operation.</returns>
51+
Task ForwardAsync();
52+
4053
/// <summary>
4154
/// Load a new web page in the current browser window.
4255
/// </summary>
@@ -52,6 +65,13 @@ public interface INavigation
5265
/// </remarks>
5366
void GoToUrl(string url);
5467

68+
/// <summary>
69+
/// Navigate to a url as an asynchronous task.
70+
/// </summary>
71+
/// <param name="url">String of where you want the browser to go.</param>
72+
/// <returns>A task object representing the asynchronous operation.</returns>
73+
Task GoToUrlAsync(string url);
74+
5575
/// <summary>
5676
/// Load a new web page in the current browser window.
5777
/// </summary>
@@ -67,9 +87,22 @@ public interface INavigation
6787
/// </remarks>
6888
void GoToUrl(Uri url);
6989

90+
/// <summary>
91+
/// Navigate to a url as an asynchronous task.
92+
/// </summary>
93+
/// <param name="url">Uri object of where you want the browser to go.</param>
94+
/// <returns>A task object representing the asynchronous operation.</returns>
95+
Task GoToUrlAsync(Uri url);
96+
7097
/// <summary>
7198
/// Refreshes the current page.
7299
/// </summary>
73100
void Refresh();
101+
102+
/// <summary>
103+
/// Reload the current page as an asynchronous task.
104+
/// </summary>
105+
/// <returns>A task object representing the asynchronous operation.</returns>
106+
Task RefreshAsync();
74107
}
75108
}

dotnet/test/support/Events/EventFiringWebDriverTest.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,15 @@ Navigated back
5858
Navigating forward
5959
Navigated forward
6060
";
61+
string normalizedExpectedLog = expectedLog.Replace("\r\n", "\n").Replace("\r", "\n");
6162
mockDriver.VerifySet(x => x.Url = "http://www.get.com", Times.Once);
6263
mockDriver.Verify(x => x.Navigate(), Times.Exactly(3));
63-
mockNavigation.Verify(x => x.GoToUrl("http://www.navigate-to.com"), Times.Once);
64-
mockNavigation.Verify(x => x.Back(), Times.Once);
65-
mockNavigation.Verify(x => x.Forward(), Times.Once);
66-
Assert.AreEqual(expectedLog, log.ToString());
64+
mockNavigation.Verify(x => x.GoToUrlAsync("http://www.navigate-to.com"), Times.Once);
65+
mockNavigation.Verify(x => x.BackAsync(), Times.Once);
66+
mockNavigation.Verify(x => x.ForwardAsync(), Times.Once);
67+
68+
string normalizedActualLog = log.ToString().Replace("\r\n", "\n").Replace("\r", "\n");
69+
Assert.AreEqual(normalizedExpectedLog, normalizedActualLog);
6770
}
6871

6972
[Test]

0 commit comments

Comments
 (0)