Skip to content

Commit 43012fd

Browse files
committed
Annotate more of WebDriver, leaving only the commands where we need to read the spec for
1 parent a7ace95 commit 43012fd

File tree

1 file changed

+51
-55
lines changed

1 file changed

+51
-55
lines changed

dotnet/src/webdriver/WebDriver.cs

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ public ReadOnlyCollection<string> WindowHandles
183183
/// </summary>
184184
public SessionId SessionId { get; private set; }
185185

186+
#nullable enable
187+
186188
/// <summary>
187189
/// Gets or sets the <see cref="IFileDetector"/> responsible for detecting
188190
/// sequences of keystrokes representing file paths and names.
@@ -194,10 +196,7 @@ public virtual IFileDetector FileDetector
194196
set => this.fileDetector = value ?? throw new ArgumentNullException(nameof(value), "FileDetector cannot be null");
195197
}
196198

197-
internal INetwork Network
198-
{
199-
get { return this.network; }
200-
}
199+
internal INetwork Network => this.network;
201200

202201
/// <summary>
203202
/// Gets or sets the factory object used to create instances of <see cref="WebElement"/>
@@ -234,7 +233,7 @@ public void Dispose()
234233
/// <param name="script">The JavaScript code to execute.</param>
235234
/// <param name="args">The arguments to the script.</param>
236235
/// <returns>The value returned by the script.</returns>
237-
public object ExecuteAsyncScript(string script, params object[] args)
236+
public object? ExecuteAsyncScript(string script, params object?[]? args)
238237
{
239238
return this.ExecuteScriptCommand(script, DriverCommand.ExecuteAsyncScript, args);
240239
}
@@ -245,7 +244,7 @@ public object ExecuteAsyncScript(string script, params object[] args)
245244
/// <param name="script">The JavaScript code to execute.</param>
246245
/// <param name="args">The arguments to the script.</param>
247246
/// <returns>The value returned by the script.</returns>
248-
public object ExecuteScript(string script, params object[] args)
247+
public object? ExecuteScript(string script, params object?[]? args)
249248
{
250249
return this.ExecuteScriptCommand(script, DriverCommand.ExecuteScript, args);
251250
}
@@ -257,7 +256,7 @@ public object ExecuteScript(string script, params object[] args)
257256
/// <param name="args">The arguments to the script.</param>
258257
/// <returns>The value returned by the script.</returns>
259258
/// <exception cref="ArgumentNullException">If <paramref name="script" /> is <see langword="null"/>.</exception>
260-
public object ExecuteScript(PinnedScript script, params object[] args)
259+
public object? ExecuteScript(PinnedScript script, params object?[]? args)
261260
{
262261
if (script == null)
263262
{
@@ -267,6 +266,8 @@ public object ExecuteScript(PinnedScript script, params object[] args)
267266
return this.ExecuteScript(script.MakeExecutionScript(), args);
268267
}
269268

269+
#nullable restore
270+
270271
/// <summary>
271272
/// Finds the first element in the page that matches the <see cref="By"/> object
272273
/// </summary>
@@ -344,6 +345,8 @@ public virtual ReadOnlyCollection<IWebElement> FindElements(string mechanism, st
344345
return this.GetElementsFromResponse(commandResponse);
345346
}
346347

348+
#nullable enable
349+
347350
/// <summary>
348351
/// Gets a <see cref="Screenshot"/> object representing the image of the page on the screen.
349352
/// </summary>
@@ -352,7 +355,7 @@ public Screenshot GetScreenshot()
352355
{
353356
Response screenshotResponse = this.Execute(DriverCommand.Screenshot, null);
354357

355-
string base64 = screenshotResponse.Value.ToString();
358+
string base64 = screenshotResponse.Value?.ToString() ?? throw new WebDriverException("Screenshot command returned successfully, but response was empty");
356359
return new Screenshot(base64);
357360
}
358361

@@ -371,7 +374,7 @@ public PrintDocument Print(PrintOptions printOptions)
371374

372375
Response commandResponse = this.Execute(DriverCommand.Print, printOptions.ToDictionary());
373376

374-
string base64 = commandResponse.Value.ToString();
377+
string base64 = commandResponse.Value?.ToString() ?? throw new WebDriverException("Print command returned successfully, but response was empty");
375378
return new PrintDocument(base64);
376379
}
377380

@@ -392,14 +395,12 @@ public void PerformActions(IList<ActionSequence> actionSequenceList)
392395
objectList.Add(sequence.ToDictionary());
393396
}
394397

395-
Dictionary<string, object> parameters = new Dictionary<string, object>();
398+
Dictionary<string, object?> parameters = new Dictionary<string, object?>();
396399
parameters["actions"] = objectList;
397400

398401
this.Execute(DriverCommand.Actions, parameters);
399402
}
400403

401-
#nullable enable
402-
403404
/// <summary>
404405
/// Resets the input state of the action executor.
405406
/// </summary>
@@ -458,7 +459,7 @@ public INavigation Navigate()
458459
/// <param name="parameters">A <see cref="Dictionary{K, V}"/> containing the names and values of the parameters of the command.</param>
459460
/// <returns>A <see cref="Response"/> containing information about the success or failure of the command and any data returned by the command.</returns>
460461
/// <exception cref="WebDriverException">The command returned an exceptional value.</exception>
461-
public object? ExecuteCustomDriverCommand(string driverCommandToExecute, Dictionary<string, object> parameters)
462+
public object? ExecuteCustomDriverCommand(string driverCommandToExecute, Dictionary<string, object?>? parameters)
462463
{
463464
if (this.registeredCommands.Contains(driverCommandToExecute))
464465
{
@@ -526,7 +527,7 @@ internal bool RegisterDriverCommand(string commandName, [NotNullWhen(true)] Comm
526527
throw new NoSuchElementException();
527528
}
528529

529-
if (response.Value is Dictionary<string, object> elementDictionary)
530+
if (response.Value is Dictionary<string, object?> elementDictionary)
530531
{
531532
return this.elementFactory.CreateElement(elementDictionary);
532533
}
@@ -546,7 +547,7 @@ internal ReadOnlyCollection<IWebElement> GetElementsFromResponse(Response respon
546547
{
547548
foreach (object? elementObject in elements)
548549
{
549-
if (elementObject is Dictionary<string, object> elementDictionary)
550+
if (elementObject is Dictionary<string, object?> elementDictionary)
550551
{
551552
WebElement element = this.elementFactory.CreateElement(elementDictionary);
552553
toReturn.Add(element);
@@ -557,15 +558,13 @@ internal ReadOnlyCollection<IWebElement> GetElementsFromResponse(Response respon
557558
return toReturn.AsReadOnly();
558559
}
559560

560-
#nullable restore
561-
562561
/// <summary>
563562
/// Executes commands with the driver
564563
/// </summary>
565564
/// <param name="driverCommandToExecute">Command that needs executing</param>
566565
/// <param name="parameters">Parameters needed for the command</param>
567566
/// <returns>WebDriver Response</returns>
568-
internal Response InternalExecute(string driverCommandToExecute, Dictionary<string, object> parameters)
567+
internal Response InternalExecute(string driverCommandToExecute, Dictionary<string, object?>? parameters)
569568
{
570569
return Task.Run(() => this.InternalExecuteAsync(driverCommandToExecute, parameters)).GetAwaiter().GetResult();
571570
}
@@ -577,7 +576,7 @@ internal Response InternalExecute(string driverCommandToExecute, Dictionary<stri
577576
/// <param name="parameters">Parameters needed for the command</param>
578577
/// <returns>A task object representing the asynchronous operation</returns>
579578
internal Task<Response> InternalExecuteAsync(string driverCommandToExecute,
580-
Dictionary<string, object> parameters)
579+
Dictionary<string, object?>? parameters)
581580
{
582581
return this.ExecuteAsync(driverCommandToExecute, parameters);
583582
}
@@ -589,7 +588,7 @@ internal Task<Response> InternalExecuteAsync(string driverCommandToExecute,
589588
/// <param name="parameters">A <see cref="Dictionary{K, V}"/> containing the names and values of the parameters of the command.</param>
590589
/// <returns>A <see cref="Response"/> containing information about the success or failure of the command and any data returned by the command.</returns>
591590
protected virtual Response Execute(string driverCommandToExecute,
592-
Dictionary<string, object> parameters)
591+
Dictionary<string, object?>? parameters)
593592
{
594593
return Task.Run(() => this.ExecuteAsync(driverCommandToExecute, parameters)).GetAwaiter().GetResult();
595594
}
@@ -600,7 +599,7 @@ protected virtual Response Execute(string driverCommandToExecute,
600599
/// <param name="driverCommandToExecute">A <see cref="DriverCommand"/> value representing the command to execute.</param>
601600
/// <param name="parameters">A <see cref="Dictionary{K, V}"/> containing the names and values of the parameters of the command.</param>
602601
/// <returns>A <see cref="Response"/> containing information about the success or failure of the command and any data returned by the command.</returns>
603-
protected virtual async Task<Response> ExecuteAsync(string driverCommandToExecute, Dictionary<string, object> parameters)
602+
protected virtual async Task<Response> ExecuteAsync(string driverCommandToExecute, Dictionary<string, object?>? parameters)
604603
{
605604
Command commandToExecute = new Command(SessionId, driverCommandToExecute, parameters);
606605

@@ -614,6 +613,8 @@ protected virtual async Task<Response> ExecuteAsync(string driverCommandToExecut
614613
return commandResponse;
615614
}
616615

616+
#nullable restore
617+
617618
/// <summary>
618619
/// Starts a session with the driver
619620
/// </summary>
@@ -861,20 +862,18 @@ private static void UnpackAndThrowOnError(Response errorResponse, string command
861862
}
862863
}
863864

864-
#nullable restore
865-
866865
/// <summary>
867866
/// Executes JavaScript in the context of the currently selected frame or window using a specific command.
868867
/// </summary>
869868
/// <param name="script">The JavaScript code to execute.</param>
870869
/// <param name="commandName">The name of the command to execute.</param>
871870
/// <param name="args">The arguments to the script.</param>
872871
/// <returns>The value returned by the script.</returns>
873-
protected object ExecuteScriptCommand(string script, string commandName, params object[] args)
872+
protected object? ExecuteScriptCommand(string script, string commandName, params object?[]? args)
874873
{
875-
object[] convertedArgs = ConvertArgumentsToJavaScriptObjects(args);
874+
object?[] convertedArgs = ConvertArgumentsToJavaScriptObjects(args);
876875

877-
Dictionary<string, object> parameters = new Dictionary<string, object>();
876+
Dictionary<string, object?> parameters = new Dictionary<string, object?>();
878877
parameters.Add("script", script);
879878

880879
if (convertedArgs != null && convertedArgs.Length > 0)
@@ -890,16 +889,16 @@ protected object ExecuteScriptCommand(string script, string commandName, params
890889
return this.ParseJavaScriptReturnValue(commandResponse.Value);
891890
}
892891

893-
private static object ConvertObjectToJavaScriptObject(object arg)
892+
private static object? ConvertObjectToJavaScriptObject(object? arg)
894893
{
895-
IWebDriverObjectReference argAsObjectReference = arg as IWebDriverObjectReference;
894+
IWebDriverObjectReference? argAsObjectReference = arg as IWebDriverObjectReference;
896895

897896
if (argAsObjectReference == null && arg is IWrapsElement argAsWrapsElement)
898897
{
899898
argAsObjectReference = argAsWrapsElement.WrappedElement as IWebDriverObjectReference;
900899
}
901900

902-
object converted;
901+
object? converted;
903902

904903
if (arg is string || arg is float || arg is double || arg is int || arg is long || arg is bool || arg == null)
905904
{
@@ -916,18 +915,18 @@ private static object ConvertObjectToJavaScriptObject(object arg)
916915
// checking for IEnumerable, since dictionaries also implement IEnumerable.
917916
// Additionally, JavaScript objects have property names as strings, so all
918917
// keys will be converted to strings.
919-
Dictionary<string, object> dictionary = new Dictionary<string, object>();
920-
foreach (var key in argAsDictionary.Keys)
918+
Dictionary<string, object?> dictionary = new Dictionary<string, object?>();
919+
foreach (DictionaryEntry argEntry in argAsDictionary)
921920
{
922-
dictionary.Add(key.ToString(), ConvertObjectToJavaScriptObject(argAsDictionary[key]));
921+
dictionary.Add(argEntry.Key.ToString()!, ConvertObjectToJavaScriptObject(argEntry.Value));
923922
}
924923

925924
converted = dictionary;
926925
}
927926
else if (arg is IEnumerable argAsEnumerable)
928927
{
929-
List<object> objectList = new List<object>();
930-
foreach (object item in argAsEnumerable)
928+
List<object?> objectList = new List<object?>();
929+
foreach (object? item in argAsEnumerable)
931930
{
932931
objectList.Add(ConvertObjectToJavaScriptObject(item));
933932
}
@@ -947,11 +946,11 @@ private static object ConvertObjectToJavaScriptObject(object arg)
947946
/// </summary>
948947
/// <param name="args">The arguments.</param>
949948
/// <returns>The list of the arguments converted to JavaScript objects.</returns>
950-
private static object[] ConvertArgumentsToJavaScriptObjects(object[] args)
949+
private static object?[] ConvertArgumentsToJavaScriptObjects(object?[]? args)
951950
{
952951
if (args == null)
953952
{
954-
return new object[] { null };
953+
return new object?[] { null };
955954
}
956955

957956
for (int i = 0; i < args.Length; i++)
@@ -962,17 +961,17 @@ private static object[] ConvertArgumentsToJavaScriptObjects(object[] args)
962961
return args;
963962
}
964963

965-
private object ParseJavaScriptReturnValue(object responseValue)
964+
private object? ParseJavaScriptReturnValue(object? responseValue)
966965
{
967-
object returnValue;
966+
object? returnValue;
968967

969-
if (responseValue is Dictionary<string, object> resultAsDictionary)
968+
if (responseValue is Dictionary<string, object?> resultAsDictionary)
970969
{
971970
if (this.elementFactory.ContainsElementReference(resultAsDictionary))
972971
{
973972
returnValue = this.elementFactory.CreateElement(resultAsDictionary);
974973
}
975-
else if (ShadowRoot.TryCreate(this, resultAsDictionary, out ShadowRoot shadowRoot))
974+
else if (ShadowRoot.TryCreate(this, resultAsDictionary, out ShadowRoot? shadowRoot))
976975
{
977976
returnValue = shadowRoot;
978977
}
@@ -989,13 +988,13 @@ private object ParseJavaScriptReturnValue(object responseValue)
989988
returnValue = resultAsDictionary;
990989
}
991990
}
992-
else if (responseValue is object[] resultAsArray)
991+
else if (responseValue is object?[] resultAsArray)
993992
{
994993
bool allElementsAreWebElements = true;
995-
List<object> toReturn = new List<object>();
996-
foreach (object item in resultAsArray)
994+
List<object?> toReturn = new List<object?>();
995+
foreach (object? item in resultAsArray)
997996
{
998-
object parsedItem = this.ParseJavaScriptReturnValue(item);
997+
object? parsedItem = this.ParseJavaScriptReturnValue(item);
999998
if (parsedItem is not IWebElement parsedItemAsElement)
1000999
{
10011000
allElementsAreWebElements = false;
@@ -1007,10 +1006,9 @@ private object ParseJavaScriptReturnValue(object responseValue)
10071006
if (toReturn.Count > 0 && allElementsAreWebElements)
10081007
{
10091008
List<IWebElement> elementList = new List<IWebElement>();
1010-
foreach (object listItem in toReturn)
1009+
foreach (object? listItem in toReturn)
10111010
{
1012-
IWebElement itemAsElement = listItem as IWebElement;
1013-
elementList.Add(itemAsElement);
1011+
elementList.Add((IWebElement)listItem!);
10141012
}
10151013

10161014
returnValue = elementList.AsReadOnly();
@@ -1028,8 +1026,6 @@ private object ParseJavaScriptReturnValue(object responseValue)
10281026
return returnValue;
10291027
}
10301028

1031-
#nullable enable
1032-
10331029
/// <summary>
10341030
/// Creates a Virtual Authenticator.
10351031
/// </summary>
@@ -1061,7 +1057,7 @@ public void RemoveVirtualAuthenticator(string authenticatorId)
10611057
throw new ArgumentNullException(nameof(authenticatorId));
10621058
}
10631059

1064-
Dictionary<string, object> parameters = new Dictionary<string, object>();
1060+
Dictionary<string, object?> parameters = new Dictionary<string, object?>();
10651061
parameters.Add("authenticatorId", authenticatorId);
10661062

10671063
this.Execute(DriverCommand.RemoveVirtualAuthenticator, parameters);
@@ -1088,7 +1084,7 @@ public void AddCredential(Credential credential)
10881084

10891085
string authenticatorId = this.AuthenticatorId ?? throw new InvalidOperationException("Virtual Authenticator needs to be added before it can perform operations");
10901086

1091-
Dictionary<string, object> parameters = new Dictionary<string, object>(credential.ToDictionary());
1087+
Dictionary<string, object?> parameters = new Dictionary<string, object?>(credential.ToDictionary());
10921088
parameters.Add("authenticatorId", authenticatorId);
10931089

10941090
this.Execute(driverCommandToExecute: DriverCommand.AddCredential, parameters);
@@ -1103,7 +1099,7 @@ public List<Credential> GetCredentials()
11031099
{
11041100
string authenticatorId = this.AuthenticatorId ?? throw new InvalidOperationException("Virtual Authenticator needs to be added before it can perform operations");
11051101

1106-
Dictionary<string, object> parameters = new Dictionary<string, object>();
1102+
Dictionary<string, object?> parameters = new Dictionary<string, object?>();
11071103
parameters.Add("authenticatorId", authenticatorId);
11081104

11091105
Response getCredentialsResponse = this.Execute(driverCommandToExecute: DriverCommand.GetCredentials, parameters);
@@ -1149,7 +1145,7 @@ public void RemoveCredential(string credentialId)
11491145

11501146
string authenticatorId = this.AuthenticatorId ?? throw new InvalidOperationException("Virtual Authenticator needs to be added before it can perform operations");
11511147

1152-
Dictionary<string, object> parameters = new Dictionary<string, object>();
1148+
Dictionary<string, object?> parameters = new Dictionary<string, object?>();
11531149
parameters.Add("authenticatorId", authenticatorId);
11541150
parameters.Add("credentialId", credentialId);
11551151

@@ -1164,7 +1160,7 @@ public void RemoveAllCredentials()
11641160
{
11651161
string authenticatorId = this.AuthenticatorId ?? throw new InvalidOperationException("Virtual Authenticator needs to be added before it can perform operations");
11661162

1167-
Dictionary<string, object> parameters = new Dictionary<string, object>();
1163+
Dictionary<string, object?> parameters = new Dictionary<string, object?>();
11681164
parameters.Add("authenticatorId", authenticatorId);
11691165

11701166
this.Execute(driverCommandToExecute: DriverCommand.RemoveAllCredentials, parameters);
@@ -1178,7 +1174,7 @@ public void SetUserVerified(bool verified)
11781174
{
11791175
string authenticatorId = this.AuthenticatorId ?? throw new InvalidOperationException("Virtual Authenticator needs to be added before it can perform operations");
11801176

1181-
Dictionary<string, object> parameters = new Dictionary<string, object>();
1177+
Dictionary<string, object?> parameters = new Dictionary<string, object?>();
11821178
parameters.Add("authenticatorId", authenticatorId);
11831179
parameters.Add("isUserVerified", verified);
11841180

0 commit comments

Comments
 (0)