Skip to content

Commit a6700fe

Browse files
committed
[dotnet] allow driver to execute commands asynchronously
1 parent 1b827c2 commit a6700fe

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

dotnet/src/webdriver/ICommandExecutor.cs

Lines changed: 9 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
{
@@ -39,5 +40,13 @@ public interface ICommandExecutor : IDisposable
3940
/// <param name="commandToExecute">The command you wish to execute</param>
4041
/// <returns>A response from the browser</returns>
4142
Response Execute(Command commandToExecute);
43+
44+
45+
/// <summary>
46+
/// Executes a command as an asynchronous task.
47+
/// </summary>
48+
/// <param name="commandToExecute">The command you wish to execute</param>
49+
/// <returns>A task object representing the asynchronous operation</returns>
50+
Task<Response> ExecuteAsync(Command commandToExecute);
4251
}
4352
}

dotnet/src/webdriver/Remote/DriverServiceCommandExecutor.cs

Lines changed: 12 additions & 1 deletion
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.Remote
2223
{
@@ -92,6 +93,16 @@ public HttpCommandExecutor HttpExecutor
9293
/// <param name="commandToExecute">The command you wish to execute</param>
9394
/// <returns>A response from the browser</returns>
9495
public Response Execute(Command commandToExecute)
96+
{
97+
return Task.Run(() => this.ExecuteAsync(commandToExecute)).GetAwaiter().GetResult();
98+
}
99+
100+
/// <summary>
101+
/// Executes a command as an asynchronous task.
102+
/// </summary>
103+
/// <param name="commandToExecute">The command you wish to execute</param>
104+
/// <returns>A task object representing the asynchronous operation</returns>
105+
public async Task<Response> ExecuteAsync(Command commandToExecute)
95106
{
96107
if (commandToExecute == null)
97108
{
@@ -108,7 +119,7 @@ public Response Execute(Command commandToExecute)
108119
// command, so that we can get the finally block.
109120
try
110121
{
111-
toReturn = this.internalExecutor.Execute(commandToExecute);
122+
toReturn = await this.internalExecutor.ExecuteAsync(commandToExecute).ConfigureAwait(false);
112123
}
113124
finally
114125
{

dotnet/src/webdriver/Remote/HttpCommandExecutor.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ public bool TryAddCommand(string commandName, CommandInfo info)
158158
/// <param name="commandToExecute">The command you wish to execute</param>
159159
/// <returns>A response from the browser</returns>
160160
public virtual Response Execute(Command commandToExecute)
161+
{
162+
return Task.Run(() => this.ExecuteAsync(commandToExecute)).GetAwaiter().GetResult();
163+
}
164+
165+
/// <summary>
166+
/// Executes a command as an asynchronous task.
167+
/// </summary>
168+
/// <param name="commandToExecute">The command you wish to execute</param>
169+
/// <returns>A task object representing the asynchronous operation</returns>
170+
public virtual async Task<Response> ExecuteAsync(Command commandToExecute)
161171
{
162172
if (commandToExecute == null)
163173
{
@@ -184,7 +194,7 @@ public virtual Response Execute(Command commandToExecute)
184194
HttpResponseInfo responseInfo = null;
185195
try
186196
{
187-
responseInfo = Task.Run(async () => await this.MakeHttpRequest(requestInfo)).GetAwaiter().GetResult();
197+
responseInfo = await this.MakeHttpRequest(requestInfo).ConfigureAwait(false);
188198
}
189199
catch (HttpRequestException ex)
190200
{

dotnet/src/webdriver/WebDriver.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System.Collections.Generic;
2525
using System.Collections.ObjectModel;
2626
using System.Globalization;
27+
using System.Threading.Tasks;
2728

2829
namespace OpenQA.Selenium
2930
{
@@ -556,7 +557,25 @@ internal ReadOnlyCollection<IWebElement> GetElementsFromResponse(Response respon
556557
/// <returns>WebDriver Response</returns>
557558
internal Response InternalExecute(string driverCommandToExecute, Dictionary<string, object> parameters)
558559
{
559-
return this.Execute(driverCommandToExecute, parameters);
560+
return Task.Run(() => this.InternalExecuteAsync(driverCommandToExecute, parameters)).GetAwaiter().GetResult();
561+
}
562+
563+
/// <summary>
564+
/// Executes commands with the driver asynchronously
565+
/// </summary>
566+
/// <param name="driverCommandToExecute">Command that needs executing</param>
567+
/// <param name="parameters">Parameters needed for the command</param>
568+
/// <returns>A task object representing the asynchronous operation</returns>
569+
internal Task<Response> InternalExecuteAsync(string driverCommandToExecute,
570+
Dictionary<string, object> parameters)
571+
{
572+
return this.ExecuteAsync(driverCommandToExecute, parameters);
573+
}
574+
575+
internal Response Execute(string driverCommandToExecute,
576+
Dictionary<string, object> parameters)
577+
{
578+
return Task.Run(() => this.ExecuteAsync(driverCommandToExecute, parameters)).GetAwaiter().GetResult();
560579
}
561580

562581
/// <summary>
@@ -565,15 +584,15 @@ internal Response InternalExecute(string driverCommandToExecute, Dictionary<stri
565584
/// <param name="driverCommandToExecute">A <see cref="DriverCommand"/> value representing the command to execute.</param>
566585
/// <param name="parameters">A <see cref="Dictionary{K, V}"/> containing the names and values of the parameters of the command.</param>
567586
/// <returns>A <see cref="Response"/> containing information about the success or failure of the command and any data returned by the command.</returns>
568-
protected virtual Response Execute(string driverCommandToExecute, Dictionary<string, object> parameters)
587+
protected virtual async Task<Response> ExecuteAsync(string driverCommandToExecute, Dictionary<string, object> parameters)
569588
{
570589
Command commandToExecute = new Command(this.sessionId, driverCommandToExecute, parameters);
571590

572591
Response commandResponse;
573592

574593
try
575594
{
576-
commandResponse = this.executor.Execute(commandToExecute);
595+
commandResponse = await this.executor.ExecuteAsync(commandToExecute).ConfigureAwait(false);
577596
}
578597
catch (System.Net.Http.HttpRequestException e)
579598
{

0 commit comments

Comments
 (0)