Skip to content

Commit b2483a1

Browse files
liviocjimevans
authored andcommitted
.NET DriverService improvment to check response on startup
With this commit, a driver using a driver service will now throw a WebDriverException if it cannot start the driver service, or if the service executable returns an invalid response. Note that the checks for the validation of the repsonses is minimal, checking for the Content-Type header and that the repsonse code is a 200. Signed-off-by: Jim Evans <[email protected]>
1 parent b43d286 commit b2483a1

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

Diff for: dotnet/src/webdriver/DriverService.cs

+19-5
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@
1717
// </copyright>
1818

1919
using System;
20-
using System.Collections.Generic;
2120
using System.Diagnostics;
2221
using System.Globalization;
2322
using System.IO;
2423
using System.Net;
25-
using System.Net.Sockets;
2624
using System.Security.Permissions;
27-
using System.Text;
2825
using OpenQA.Selenium.Internal;
2926
using OpenQA.Selenium.Remote;
3027

@@ -181,23 +178,40 @@ public void Start()
181178
this.driverServiceProcess.StartInfo.UseShellExecute = false;
182179
this.driverServiceProcess.StartInfo.CreateNoWindow = this.hideCommandPromptWindow;
183180
this.driverServiceProcess.Start();
184-
Uri serviceHealthUri = new Uri(this.ServiceUrl, new Uri("status", UriKind.Relative));
181+
Uri serviceHealthUri = new Uri(this.ServiceUrl, new Uri(DriverCommand.Status, UriKind.Relative));
185182
bool processStarted = false;
186183
DateTime timeout = DateTime.Now.Add(TimeSpan.FromSeconds(20));
187184
while (!processStarted && DateTime.Now < timeout)
188185
{
189186
try
190187
{
188+
// If the driver service process has exited, we can exit early.
189+
if (this.driverServiceProcess.HasExited)
190+
{
191+
break;
192+
}
193+
191194
HttpWebRequest request = HttpWebRequest.Create(serviceHealthUri) as HttpWebRequest;
192195
request.KeepAlive = false;
193196
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
197+
198+
// Checking the response from the 'status' end point. Note that we are simply checking
199+
// that the HTTP status returned is a 200 status, and that the resposne has the correct
200+
// Content-Type header. A more sophisticated check would parse the JSON response and
201+
// validate its values. At the moment we do not do this more sophisticated check.
202+
processStarted = response.StatusCode == HttpStatusCode.OK && response.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase);
194203
response.Close();
195-
processStarted = true;
196204
}
197205
catch (WebException)
198206
{
199207
}
200208
}
209+
210+
if (!processStarted)
211+
{
212+
string msg = "Cannot start the driver service on " + this.ServiceUrl;
213+
throw new WebDriverException(msg);
214+
}
201215
}
202216

203217
/// <summary>

0 commit comments

Comments
 (0)