From a6700fe5d14fa14968756ffc1d928e762f01a154 Mon Sep 17 00:00:00 2001 From: titusfortner Date: Wed, 29 May 2024 09:48:05 -0500 Subject: [PATCH 1/6] [dotnet] allow driver to execute commands asynchronously --- dotnet/src/webdriver/ICommandExecutor.cs | 9 +++++++ .../Remote/DriverServiceCommandExecutor.cs | 13 +++++++++- .../webdriver/Remote/HttpCommandExecutor.cs | 12 ++++++++- dotnet/src/webdriver/WebDriver.cs | 25 ++++++++++++++++--- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/dotnet/src/webdriver/ICommandExecutor.cs b/dotnet/src/webdriver/ICommandExecutor.cs index 68cfe200391f4..3bcfb520166d3 100644 --- a/dotnet/src/webdriver/ICommandExecutor.cs +++ b/dotnet/src/webdriver/ICommandExecutor.cs @@ -17,6 +17,7 @@ // using System; +using System.Threading.Tasks; namespace OpenQA.Selenium { @@ -39,5 +40,13 @@ public interface ICommandExecutor : IDisposable /// The command you wish to execute /// A response from the browser Response Execute(Command commandToExecute); + + + /// + /// Executes a command as an asynchronous task. + /// + /// The command you wish to execute + /// A task object representing the asynchronous operation + Task ExecuteAsync(Command commandToExecute); } } diff --git a/dotnet/src/webdriver/Remote/DriverServiceCommandExecutor.cs b/dotnet/src/webdriver/Remote/DriverServiceCommandExecutor.cs index 4b0fcb27ed7fc..1e486ad96f98d 100644 --- a/dotnet/src/webdriver/Remote/DriverServiceCommandExecutor.cs +++ b/dotnet/src/webdriver/Remote/DriverServiceCommandExecutor.cs @@ -17,6 +17,7 @@ // using System; +using System.Threading.Tasks; namespace OpenQA.Selenium.Remote { @@ -92,6 +93,16 @@ public HttpCommandExecutor HttpExecutor /// The command you wish to execute /// A response from the browser public Response Execute(Command commandToExecute) + { + return Task.Run(() => this.ExecuteAsync(commandToExecute)).GetAwaiter().GetResult(); + } + + /// + /// Executes a command as an asynchronous task. + /// + /// The command you wish to execute + /// A task object representing the asynchronous operation + public async Task ExecuteAsync(Command commandToExecute) { if (commandToExecute == null) { @@ -108,7 +119,7 @@ public Response Execute(Command commandToExecute) // command, so that we can get the finally block. try { - toReturn = this.internalExecutor.Execute(commandToExecute); + toReturn = await this.internalExecutor.ExecuteAsync(commandToExecute).ConfigureAwait(false); } finally { diff --git a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs index 3dbb5e4efd179..1872e418a7888 100644 --- a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs +++ b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs @@ -158,6 +158,16 @@ public bool TryAddCommand(string commandName, CommandInfo info) /// The command you wish to execute /// A response from the browser public virtual Response Execute(Command commandToExecute) + { + return Task.Run(() => this.ExecuteAsync(commandToExecute)).GetAwaiter().GetResult(); + } + + /// + /// Executes a command as an asynchronous task. + /// + /// The command you wish to execute + /// A task object representing the asynchronous operation + public virtual async Task ExecuteAsync(Command commandToExecute) { if (commandToExecute == null) { @@ -184,7 +194,7 @@ public virtual Response Execute(Command commandToExecute) HttpResponseInfo responseInfo = null; try { - responseInfo = Task.Run(async () => await this.MakeHttpRequest(requestInfo)).GetAwaiter().GetResult(); + responseInfo = await this.MakeHttpRequest(requestInfo).ConfigureAwait(false); } catch (HttpRequestException ex) { diff --git a/dotnet/src/webdriver/WebDriver.cs b/dotnet/src/webdriver/WebDriver.cs index 3de43e25c452f..d063e8f5905ea 100644 --- a/dotnet/src/webdriver/WebDriver.cs +++ b/dotnet/src/webdriver/WebDriver.cs @@ -24,6 +24,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; +using System.Threading.Tasks; namespace OpenQA.Selenium { @@ -556,7 +557,25 @@ internal ReadOnlyCollection GetElementsFromResponse(Response respon /// WebDriver Response internal Response InternalExecute(string driverCommandToExecute, Dictionary parameters) { - return this.Execute(driverCommandToExecute, parameters); + return Task.Run(() => this.InternalExecuteAsync(driverCommandToExecute, parameters)).GetAwaiter().GetResult(); + } + + /// + /// Executes commands with the driver asynchronously + /// + /// Command that needs executing + /// Parameters needed for the command + /// A task object representing the asynchronous operation + internal Task InternalExecuteAsync(string driverCommandToExecute, + Dictionary parameters) + { + return this.ExecuteAsync(driverCommandToExecute, parameters); + } + + internal Response Execute(string driverCommandToExecute, + Dictionary parameters) + { + return Task.Run(() => this.ExecuteAsync(driverCommandToExecute, parameters)).GetAwaiter().GetResult(); } /// @@ -565,7 +584,7 @@ internal Response InternalExecute(string driverCommandToExecute, DictionaryA value representing the command to execute. /// A containing the names and values of the parameters of the command. /// A containing information about the success or failure of the command and any data returned by the command. - protected virtual Response Execute(string driverCommandToExecute, Dictionary parameters) + protected virtual async Task ExecuteAsync(string driverCommandToExecute, Dictionary parameters) { Command commandToExecute = new Command(this.sessionId, driverCommandToExecute, parameters); @@ -573,7 +592,7 @@ protected virtual Response Execute(string driverCommandToExecute, Dictionary Date: Wed, 29 May 2024 10:54:45 -0500 Subject: [PATCH 2/6] [dotnet] implement navigation commands with async tasks --- dotnet/src/webdriver/Navigator.cs | 71 +++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/dotnet/src/webdriver/Navigator.cs b/dotnet/src/webdriver/Navigator.cs index f8f6047e91f5a..7c9bf0bbfe148 100644 --- a/dotnet/src/webdriver/Navigator.cs +++ b/dotnet/src/webdriver/Navigator.cs @@ -18,6 +18,7 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; namespace OpenQA.Selenium { @@ -38,26 +39,54 @@ public Navigator(WebDriver driver) } /// - /// Move the browser back + /// Move back a single entry in the browser's history. /// public void Back() { - this.driver.InternalExecute(DriverCommand.GoBack, null); + Task.Run(this.BackAsync).GetAwaiter().GetResult(); } /// - /// Move the browser forward + /// Move back a single entry in the browser's history as an asynchronous task. + /// + /// A task object representing the asynchronous operation. + public async Task BackAsync() + { + await this.driver.InternalExecuteAsync(DriverCommand.GoBack, null).ConfigureAwait(false); + } + + /// + /// Move a single "item" forward in the browser's history. /// public void Forward() { - this.driver.InternalExecute(DriverCommand.GoForward, null); + Task.Run(this.ForwardAsync).GetAwaiter().GetResult(); } /// - /// Navigate to a url for your test + /// Move a single "item" forward in the browser's history as an asynchronous task. + /// + /// A task object representing the asynchronous operation. + public async Task ForwardAsync() + { + await this.driver.InternalExecuteAsync(DriverCommand.GoForward, null).ConfigureAwait(false); + } + + /// + /// Navigate to a url. /// /// String of where you want the browser to go to public void GoToUrl(string url) + { + Task.Run(() => this.GoToUrlAsync(url)).GetAwaiter().GetResult(); + } + + /// + /// Navigate to a url as an asynchronous task. + /// + /// String of where you want the browser to go. + /// A task object representing the asynchronous operation. + public async Task GoToUrlAsync(string url) { if (url == null) { @@ -68,31 +97,49 @@ public void GoToUrl(string url) { { "url", url } }; - this.driver.InternalExecute(DriverCommand.Get, parameters); - + await this.driver.InternalExecuteAsync(DriverCommand.Get, parameters).ConfigureAwait(false); } /// - /// Navigate to a url for your test + /// Navigate to a url. /// - /// Uri object of where you want the browser to go to + /// Uri object of where you want the browser to go. public void GoToUrl(Uri url) + { + Task.Run(() => this.GoToUrlAsync(url)).GetAwaiter().GetResult(); + } + + /// + /// Navigate to a url as an asynchronous task. + /// + /// Uri object of where you want the browser to go. + /// A task object representing the asynchronous operation. + public async Task GoToUrlAsync(Uri url) { if (url == null) { throw new ArgumentNullException(nameof(url), "URL cannot be null."); } - this.GoToUrl(url.ToString()); + await this.GoToUrlAsync(url.ToString()).ConfigureAwait(false); } /// - /// Refresh the browser + /// Reload the current page. /// public void Refresh() + { + Task.Run(this.RefreshAsync).GetAwaiter().GetResult(); + } + + /// + /// Reload the current page as an asynchronous task. + /// + /// A task object representing the asynchronous operation. + public async Task RefreshAsync() { // driver.SwitchTo().DefaultContent(); - this.driver.InternalExecute(DriverCommand.Refresh, null); + await this.driver.InternalExecuteAsync(DriverCommand.Refresh, null).ConfigureAwait(false); } } } From aa484e97d4d0482835d78010e31fac052109b2a7 Mon Sep 17 00:00:00 2001 From: titusfortner Date: Wed, 29 May 2024 12:14:33 -0500 Subject: [PATCH 3/6] [dotnet] add async navigation methods to interface and implement in event firing webdriver --- .../support/Events/EventFiringWebDriver.cs | 82 ++++++++++++++----- dotnet/src/webdriver/INavigation.cs | 33 ++++++++ .../Events/EventFiringWebDriverTest.cs | 11 ++- 3 files changed, 102 insertions(+), 24 deletions(-) diff --git a/dotnet/src/support/Events/EventFiringWebDriver.cs b/dotnet/src/support/Events/EventFiringWebDriver.cs index ac428cf2830b5..879b4e1e57503 100644 --- a/dotnet/src/support/Events/EventFiringWebDriver.cs +++ b/dotnet/src/support/Events/EventFiringWebDriver.cs @@ -20,6 +20,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Drawing; +using System.Threading.Tasks; namespace OpenQA.Selenium.Support.Events { @@ -845,12 +846,21 @@ public EventFiringNavigation(EventFiringWebDriver driver) /// Move the browser back /// public void Back() + { + Task.Run(this.BackAsync).GetAwaiter().GetResult(); + } + + /// + /// Move the browser back as an asynchronous task. + /// + /// A task object representing the asynchronous operation + 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) @@ -861,15 +871,24 @@ public void Back() } /// - /// Move the browser forward + /// Move a single "item" forward in the browser's history. /// public void Forward() + { + Task.Run(this.ForwardAsync).GetAwaiter().GetResult(); + } + + /// + /// Move a single "item" forward in the browser's history as an asynchronous task. + /// + /// A task object representing the asynchronous operation. + 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) @@ -880,16 +899,31 @@ public void Forward() } /// - /// Navigate to a url for your test + /// Navigate to a url. /// /// String of where you want the browser to go to public void GoToUrl(string url) { + Task.Run(() => this.GoToUrlAsync(url)).GetAwaiter().GetResult(); + } + + /// + /// Navigate to a url as an asynchronous task. + /// + /// String of where you want the browser to go. + /// A task object representing the asynchronous operation. + 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) @@ -900,38 +934,46 @@ public void GoToUrl(string url) } /// - /// Navigate to a url for your test + /// Navigate to a url. /// /// Uri object of where you want the browser to go to public void GoToUrl(Uri url) + { + Task.Run(() => this.GoToUrlAsync(url)).GetAwaiter().GetResult(); + } + + /// + /// Navigate to a url as an asynchronous task. + /// + /// Uri object of where you want the browser to go. + /// A task object representing the asynchronous operation. + 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); } /// - /// Refresh the browser + /// Reload the current page. /// public void Refresh() + { + Task.Run(this.RefreshAsync).GetAwaiter().GetResult(); + } + + /// + /// Reload the current page as an asynchronous task. + /// + /// A task object representing the asynchronous operation. + public async Task RefreshAsync() { try { - this.wrappedNavigation.Refresh(); + await this.wrappedNavigation.RefreshAsync().ConfigureAwait(false); } catch (Exception ex) { diff --git a/dotnet/src/webdriver/INavigation.cs b/dotnet/src/webdriver/INavigation.cs index bff75d4743e6b..a55b4dfda12bf 100644 --- a/dotnet/src/webdriver/INavigation.cs +++ b/dotnet/src/webdriver/INavigation.cs @@ -17,6 +17,7 @@ // using System; +using System.Threading.Tasks; namespace OpenQA.Selenium { @@ -31,12 +32,24 @@ public interface INavigation /// void Back(); + /// + /// Move back a single entry in the browser's history as an asynchronous task. + /// + /// A task object representing the asynchronous operation. + Task BackAsync(); + /// /// Move a single "item" forward in the browser's history. /// /// Does nothing if we are on the latest page viewed. void Forward(); + /// + /// Move a single "item" forward in the browser's history as an asynchronous task. + /// + /// A task object representing the asynchronous operation. + Task ForwardAsync(); + /// /// Load a new web page in the current browser window. /// @@ -52,6 +65,13 @@ public interface INavigation /// void GoToUrl(string url); + /// + /// Navigate to a url as an asynchronous task. + /// + /// String of where you want the browser to go. + /// A task object representing the asynchronous operation. + Task GoToUrlAsync(string url); + /// /// Load a new web page in the current browser window. /// @@ -67,9 +87,22 @@ public interface INavigation /// void GoToUrl(Uri url); + /// + /// Navigate to a url as an asynchronous task. + /// + /// Uri object of where you want the browser to go. + /// A task object representing the asynchronous operation. + Task GoToUrlAsync(Uri url); + /// /// Refreshes the current page. /// void Refresh(); + + /// + /// Reload the current page as an asynchronous task. + /// + /// A task object representing the asynchronous operation. + Task RefreshAsync(); } } diff --git a/dotnet/test/support/Events/EventFiringWebDriverTest.cs b/dotnet/test/support/Events/EventFiringWebDriverTest.cs index 972be725adfa9..3d59f056f3ee8 100644 --- a/dotnet/test/support/Events/EventFiringWebDriverTest.cs +++ b/dotnet/test/support/Events/EventFiringWebDriverTest.cs @@ -58,12 +58,15 @@ Navigated back Navigating forward Navigated forward "; + string normalizedExpectedLog = expectedLog.Replace("\r\n", "\n").Replace("\r", "\n"); mockDriver.VerifySet(x => x.Url = "http://www.get.com", Times.Once); mockDriver.Verify(x => x.Navigate(), Times.Exactly(3)); - mockNavigation.Verify(x => x.GoToUrl("http://www.navigate-to.com"), Times.Once); - mockNavigation.Verify(x => x.Back(), Times.Once); - mockNavigation.Verify(x => x.Forward(), Times.Once); - Assert.AreEqual(expectedLog, log.ToString()); + mockNavigation.Verify(x => x.GoToUrlAsync("http://www.navigate-to.com"), Times.Once); + mockNavigation.Verify(x => x.BackAsync(), Times.Once); + mockNavigation.Verify(x => x.ForwardAsync(), Times.Once); + + string normalizedActualLog = log.ToString().Replace("\r\n", "\n").Replace("\r", "\n"); + Assert.AreEqual(normalizedExpectedLog, normalizedActualLog); } [Test] From 2153ba2ff0d388b1199c9045bd76ef7a98757487 Mon Sep 17 00:00:00 2001 From: titusfortner Date: Mon, 3 Jun 2024 10:21:17 -0500 Subject: [PATCH 4/6] all async executions inside Task need to set configure await to false --- dotnet/src/support/Events/EventFiringWebDriver.cs | 10 +++++----- dotnet/src/webdriver/Navigator.cs | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/dotnet/src/support/Events/EventFiringWebDriver.cs b/dotnet/src/support/Events/EventFiringWebDriver.cs index 879b4e1e57503..6098b5b041a0e 100644 --- a/dotnet/src/support/Events/EventFiringWebDriver.cs +++ b/dotnet/src/support/Events/EventFiringWebDriver.cs @@ -847,7 +847,7 @@ public EventFiringNavigation(EventFiringWebDriver driver) /// public void Back() { - Task.Run(this.BackAsync).GetAwaiter().GetResult(); + Task.Run(async () => await this.BackAsync().ConfigureAwait(false)).GetAwaiter().GetResult(); } /// @@ -875,7 +875,7 @@ public async Task BackAsync() /// public void Forward() { - Task.Run(this.ForwardAsync).GetAwaiter().GetResult(); + Task.Run(async () => await this.ForwardAsync().ConfigureAwait(false)).GetAwaiter().GetResult(); } /// @@ -904,7 +904,7 @@ public async Task ForwardAsync() /// String of where you want the browser to go to public void GoToUrl(string url) { - Task.Run(() => this.GoToUrlAsync(url)).GetAwaiter().GetResult(); + Task.Run(() => this.GoToUrlAsync(url).ConfigureAwait(false)).GetAwaiter().GetResult(); } /// @@ -939,7 +939,7 @@ public async Task GoToUrlAsync(string url) /// Uri object of where you want the browser to go to public void GoToUrl(Uri url) { - Task.Run(() => this.GoToUrlAsync(url)).GetAwaiter().GetResult(); + Task.Run(() => this.GoToUrlAsync(url).ConfigureAwait(false)).GetAwaiter().GetResult(); } /// @@ -962,7 +962,7 @@ public async Task GoToUrlAsync(Uri url) /// public void Refresh() { - Task.Run(this.RefreshAsync).GetAwaiter().GetResult(); + Task.Run(async () => await this.RefreshAsync().ConfigureAwait(false)).GetAwaiter().GetResult(); } /// diff --git a/dotnet/src/webdriver/Navigator.cs b/dotnet/src/webdriver/Navigator.cs index 7c9bf0bbfe148..83de0c6a71831 100644 --- a/dotnet/src/webdriver/Navigator.cs +++ b/dotnet/src/webdriver/Navigator.cs @@ -43,7 +43,7 @@ public Navigator(WebDriver driver) /// public void Back() { - Task.Run(this.BackAsync).GetAwaiter().GetResult(); + Task.Run(async () => await this.BackAsync().ConfigureAwait(false)).GetAwaiter().GetResult(); } /// @@ -60,7 +60,7 @@ public async Task BackAsync() /// public void Forward() { - Task.Run(this.ForwardAsync).GetAwaiter().GetResult(); + Task.Run(async () => await this.ForwardAsync().ConfigureAwait(false)).GetAwaiter().GetResult(); } /// @@ -78,7 +78,7 @@ public async Task ForwardAsync() /// String of where you want the browser to go to public void GoToUrl(string url) { - Task.Run(() => this.GoToUrlAsync(url)).GetAwaiter().GetResult(); + Task.Run(async () => await this.GoToUrlAsync(url).ConfigureAwait(false)).GetAwaiter().GetResult(); } /// @@ -106,7 +106,7 @@ public async Task GoToUrlAsync(string url) /// Uri object of where you want the browser to go. public void GoToUrl(Uri url) { - Task.Run(() => this.GoToUrlAsync(url)).GetAwaiter().GetResult(); + Task.Run(async () => await this.GoToUrlAsync(url).ConfigureAwait(false)).GetAwaiter().GetResult(); } /// @@ -129,7 +129,7 @@ public async Task GoToUrlAsync(Uri url) /// public void Refresh() { - Task.Run(this.RefreshAsync).GetAwaiter().GetResult(); + Task.Run(async () => await this.RefreshAsync().ConfigureAwait(false)).GetAwaiter().GetResult(); } /// From f50fd80e37c0edffac3502b6d8d28d99d63093bf Mon Sep 17 00:00:00 2001 From: titusfortner Date: Mon, 3 Jun 2024 10:21:47 -0500 Subject: [PATCH 5/6] add explicit tests for navigation async methods --- dotnet/test/common/NavigationTest.cs | 71 +++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/dotnet/test/common/NavigationTest.cs b/dotnet/test/common/NavigationTest.cs index ee40894aa047c..e1b514bf04cb9 100644 --- a/dotnet/test/common/NavigationTest.cs +++ b/dotnet/test/common/NavigationTest.cs @@ -1,5 +1,6 @@ using NUnit.Framework; using System; +using System.Threading.Tasks; namespace OpenQA.Selenium { @@ -40,7 +41,7 @@ public void ShouldAcceptInvalidUrlsUsingUris() INavigation navigation; navigation = driver.Navigate(); Assert.That(() => navigation.GoToUrl((Uri)null), Throws.InstanceOf()); - // new Uri("") and new Uri("isidsji30342??éåµñ©æ") + // new Uri("") and new Uri("isidsji30342??éåµñ©æ") // throw an exception, so we needn't worry about them. } @@ -90,5 +91,73 @@ public void ShouldRefreshPage() Assert.AreEqual("What's for dinner?", changedDiv.Text); } + [Test] + [NeedsFreshDriver(IsCreatedBeforeTest = true)] + public Task ShouldNotHaveProblemNavigatingWithNoPagesBrowsedAsync() + { + var navigation = driver.Navigate(); + Assert.DoesNotThrowAsync(async () => await navigation.BackAsync()); + Assert.DoesNotThrowAsync(async () => await navigation.ForwardAsync()); + return Task.CompletedTask; + } + + [Test] + public async Task ShouldGoBackAndForwardAsync() + { + INavigation navigation = driver.Navigate(); + + await navigation.GoToUrlAsync(macbethPage); + await navigation.GoToUrlAsync(simpleTestPage); + + await navigation.BackAsync(); + Assert.AreEqual(macbethTitle, driver.Title); + + await navigation.ForwardAsync(); + Assert.AreEqual(simpleTestTitle, driver.Title); + } + + [Test] + public void ShouldAcceptInvalidUrlsUsingUrisAsync() + { + INavigation navigation = driver.Navigate(); + Assert.That(async () => await navigation.GoToUrlAsync((Uri)null), Throws.InstanceOf()); + } + + [Test] + public async Task ShouldGoToUrlUsingStringAsync() + { + var navigation = driver.Navigate(); + + await navigation.GoToUrlAsync(macbethPage); + Assert.AreEqual(macbethTitle, driver.Title); + + await navigation.GoToUrlAsync(simpleTestPage); + Assert.AreEqual(simpleTestTitle, driver.Title); + } + + [Test] + public void ShouldGoToUrlUsingUriAsync() + { + var navigation = driver.Navigate(); + + navigation.GoToUrlAsync(new Uri(macbethPage)); + Assert.AreEqual(driver.Title, macbethTitle); + navigation.GoToUrl(new Uri(simpleTestPage)); + Assert.AreEqual(simpleTestTitle, driver.Title); + } + + [Test] + public async Task ShouldRefreshPageAsync() + { + await driver.Navigate().GoToUrlAsync(javascriptPage); + IWebElement changedDiv = driver.FindElement(By.Id("dynamo")); + driver.FindElement(By.Id("updatediv")).Click(); + + Assert.AreEqual("Fish and chips!", changedDiv.Text); + await driver.Navigate().RefreshAsync(); + + changedDiv = driver.FindElement(By.Id("dynamo")); + Assert.AreEqual("What's for dinner?", changedDiv.Text); + } } } From 44f94bc1fce08672609d7cdc4b39f355c580022a Mon Sep 17 00:00:00 2001 From: titusfortner Date: Thu, 6 Jun 2024 12:10:23 -0500 Subject: [PATCH 6/6] use async delegate with Task.Run instead of ConfigureAwait --- .../support/Events/EventFiringWebDriver.cs | 25 +++++++++++++++---- dotnet/src/webdriver/Navigator.cs | 25 +++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/dotnet/src/support/Events/EventFiringWebDriver.cs b/dotnet/src/support/Events/EventFiringWebDriver.cs index 6098b5b041a0e..77a3b5f3b50a5 100644 --- a/dotnet/src/support/Events/EventFiringWebDriver.cs +++ b/dotnet/src/support/Events/EventFiringWebDriver.cs @@ -847,7 +847,10 @@ public EventFiringNavigation(EventFiringWebDriver driver) /// public void Back() { - Task.Run(async () => await this.BackAsync().ConfigureAwait(false)).GetAwaiter().GetResult(); + Task.Run(async delegate + { + await this.BackAsync(); + }).GetAwaiter().GetResult(); } /// @@ -875,7 +878,10 @@ public async Task BackAsync() /// public void Forward() { - Task.Run(async () => await this.ForwardAsync().ConfigureAwait(false)).GetAwaiter().GetResult(); + Task.Run(async delegate + { + await this.ForwardAsync(); + }).GetAwaiter().GetResult(); } /// @@ -904,7 +910,10 @@ public async Task ForwardAsync() /// String of where you want the browser to go to public void GoToUrl(string url) { - Task.Run(() => this.GoToUrlAsync(url).ConfigureAwait(false)).GetAwaiter().GetResult(); + Task.Run(async delegate + { + await this.GoToUrlAsync(url); + }).GetAwaiter().GetResult(); } /// @@ -939,7 +948,10 @@ public async Task GoToUrlAsync(string url) /// Uri object of where you want the browser to go to public void GoToUrl(Uri url) { - Task.Run(() => this.GoToUrlAsync(url).ConfigureAwait(false)).GetAwaiter().GetResult(); + Task.Run(async delegate + { + await this.GoToUrlAsync(url); + }).GetAwaiter().GetResult(); } /// @@ -962,7 +974,10 @@ public async Task GoToUrlAsync(Uri url) /// public void Refresh() { - Task.Run(async () => await this.RefreshAsync().ConfigureAwait(false)).GetAwaiter().GetResult(); + Task.Run(async delegate + { + await this.RefreshAsync(); + }).GetAwaiter().GetResult(); } /// diff --git a/dotnet/src/webdriver/Navigator.cs b/dotnet/src/webdriver/Navigator.cs index 83de0c6a71831..48683a03c9fe1 100644 --- a/dotnet/src/webdriver/Navigator.cs +++ b/dotnet/src/webdriver/Navigator.cs @@ -43,7 +43,10 @@ public Navigator(WebDriver driver) /// public void Back() { - Task.Run(async () => await this.BackAsync().ConfigureAwait(false)).GetAwaiter().GetResult(); + Task.Run(async delegate + { + await this.BackAsync(); + }).GetAwaiter().GetResult(); } /// @@ -60,7 +63,10 @@ public async Task BackAsync() /// public void Forward() { - Task.Run(async () => await this.ForwardAsync().ConfigureAwait(false)).GetAwaiter().GetResult(); + Task.Run(async delegate + { + await this.ForwardAsync(); + }).GetAwaiter().GetResult(); } /// @@ -78,7 +84,10 @@ public async Task ForwardAsync() /// String of where you want the browser to go to public void GoToUrl(string url) { - Task.Run(async () => await this.GoToUrlAsync(url).ConfigureAwait(false)).GetAwaiter().GetResult(); + Task.Run(async delegate + { + await this.GoToUrlAsync(url); + }).GetAwaiter().GetResult(); } /// @@ -106,7 +115,10 @@ public async Task GoToUrlAsync(string url) /// Uri object of where you want the browser to go. public void GoToUrl(Uri url) { - Task.Run(async () => await this.GoToUrlAsync(url).ConfigureAwait(false)).GetAwaiter().GetResult(); + Task.Run(async delegate + { + await this.GoToUrlAsync(url); + }).GetAwaiter().GetResult(); } /// @@ -129,7 +141,10 @@ public async Task GoToUrlAsync(Uri url) /// public void Refresh() { - Task.Run(async () => await this.RefreshAsync().ConfigureAwait(false)).GetAwaiter().GetResult(); + Task.Run(async delegate + { + await this.RefreshAsync(); + }).GetAwaiter().GetResult(); } ///