Skip to content

Commit 2ea99a3

Browse files
committed
[dotnet] Annotate nullability on more of WebElement
1 parent 787cf6f commit 2ea99a3

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

dotnet/src/webdriver/WebElement.cs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class WebElement : IWebElement, IFindsElement, IWrapsDriver, ILocatable,
4040
/// </summary>
4141
public const string ElementReferencePropertyName = "element-6066-11e4-a52e-4f735466cecf";
4242

43+
#nullable enable
44+
4345
private readonly WebDriver driver;
4446

4547
/// <summary>
@@ -59,6 +61,8 @@ public WebElement(WebDriver parentDriver, string id)
5961
/// </summary>
6062
public IWebDriver WrappedDriver => this.driver;
6163

64+
#nullable restore
65+
6266
/// <summary>
6367
/// Gets the tag name of this element.
6468
/// </summary>
@@ -99,6 +103,8 @@ public virtual string Text
99103
}
100104
}
101105

106+
#nullable enable
107+
102108
/// <summary>
103109
/// Gets a value indicating whether or not this element is enabled.
104110
/// </summary>
@@ -151,7 +157,11 @@ public virtual Point Location
151157

152158
Response commandResponse = this.Execute(DriverCommand.GetElementRect, parameters);
153159

154-
Dictionary<string, object> rawPoint = (Dictionary<string, object>)commandResponse.Value;
160+
if (commandResponse.Value is not Dictionary<string, object?> rawPoint)
161+
{
162+
throw new WebDriverException($"GetElementRect command was successful, but response was not an object: {commandResponse.Value}");
163+
}
164+
155165
int x = Convert.ToInt32(rawPoint["x"], CultureInfo.InvariantCulture);
156166
int y = Convert.ToInt32(rawPoint["y"], CultureInfo.InvariantCulture);
157167
return new Point(x, y);
@@ -171,7 +181,11 @@ public virtual Size Size
171181

172182
Response commandResponse = this.Execute(DriverCommand.GetElementRect, parameters);
173183

174-
Dictionary<string, object> rawSize = (Dictionary<string, object>)commandResponse.Value;
184+
if (commandResponse.Value is not Dictionary<string, object?> rawSize)
185+
{
186+
throw new WebDriverException($"GetElementRect command was successful, but response was not an object: {commandResponse.Value}");
187+
}
188+
175189
int width = Convert.ToInt32(rawSize["width"], CultureInfo.InvariantCulture);
176190
int height = Convert.ToInt32(rawSize["height"], CultureInfo.InvariantCulture);
177191
return new Size(width, height);
@@ -207,7 +221,7 @@ public virtual Point LocationOnScreenOnceScrolledIntoView
207221
{
208222
get
209223
{
210-
object scriptResponse = this.driver.ExecuteScript("var rect = arguments[0].getBoundingClientRect(); return {'x': rect.left, 'y': rect.top};", this);
224+
object scriptResponse = this.driver.ExecuteScript("var rect = arguments[0].getBoundingClientRect(); return {'x': rect.left, 'y': rect.top};", this)!;
211225

212226
Dictionary<string, object> rawLocation = (Dictionary<string, object>)scriptResponse;
213227

@@ -217,6 +231,8 @@ public virtual Point LocationOnScreenOnceScrolledIntoView
217231
}
218232
}
219233

234+
#nullable restore
235+
220236
/// <summary>
221237
/// Gets the computed accessible label of this element.
222238
/// </summary>
@@ -253,6 +269,8 @@ public virtual string ComputedAccessibleRole
253269
}
254270
}
255271

272+
#nullable enable
273+
256274
/// <summary>
257275
/// Gets the coordinates identifying the location of this element using
258276
/// various frames of reference.
@@ -318,6 +336,7 @@ public virtual void Click()
318336
/// </summary>
319337
/// <param name="by">The locating mechanism to use.</param>
320338
/// <returns>The first matching <see cref="IWebElement"/> on the current context.</returns>
339+
/// <exception cref="ArgumentNullException">If <paramref name="by"/> is <see langword="null"/>.</exception>
321340
/// <exception cref="NoSuchElementException">If no element matches the criteria.</exception>
322341
public virtual IWebElement FindElement(By by)
323342
{
@@ -329,6 +348,8 @@ public virtual IWebElement FindElement(By by)
329348
return by.FindElement(this);
330349
}
331350

351+
#nullable restore
352+
332353
/// <summary>
333354
/// Finds a child element matching the given mechanism and value.
334355
/// </summary>
@@ -382,6 +403,8 @@ public virtual ReadOnlyCollection<IWebElement> FindElements(string mechanism, st
382403
return this.driver.GetElementsFromResponse(commandResponse);
383404
}
384405

406+
#nullable enable
407+
385408
/// <summary>
386409
/// Gets the value of the specified attribute or property for this element.
387410
/// </summary>
@@ -419,15 +442,14 @@ public virtual ReadOnlyCollection<IWebElement> FindElements(string mechanism, st
419442
/// via JavaScript.
420443
/// </remarks>
421444
/// <exception cref="StaleElementReferenceException">Thrown when the target element is no longer valid in the document DOM.</exception>
422-
public virtual string GetAttribute(string attributeName)
445+
public virtual string? GetAttribute(string attributeName)
423446
{
424-
Response commandResponse = null;
425-
string attributeValue = string.Empty;
426447
Dictionary<string, object> parameters = new Dictionary<string, object>();
427448
string atom = GetAtom("get-attribute.js");
428449
parameters.Add("script", atom);
429450
parameters.Add("args", new object[] { ((IWebDriverObjectReference)this).ToDictionary(), attributeName });
430-
commandResponse = this.Execute(DriverCommand.ExecuteScript, parameters);
451+
452+
Response commandResponse = Execute(DriverCommand.ExecuteScript, parameters);
431453

432454

433455
// Normalize string values of boolean results as lowercase.
@@ -452,7 +474,7 @@ public virtual string GetAttribute(string attributeName)
452474
/// of an IDL property of the element, either use the <see cref="GetAttribute(string)"/>
453475
/// method or the <see cref="GetDomProperty(string)"/> method.
454476
/// </remarks>
455-
public virtual string GetDomAttribute(string attributeName)
477+
public virtual string? GetDomAttribute(string attributeName)
456478
{
457479
Dictionary<string, object> parameters = new Dictionary<string, object>();
458480
parameters.Add("id", this.Id);
@@ -470,7 +492,7 @@ public virtual string GetDomAttribute(string attributeName)
470492
/// <returns>The JavaScript property's current value. Returns a <see langword="null"/> if the
471493
/// value is not set or the property does not exist.</returns>
472494
/// <exception cref="StaleElementReferenceException">Thrown when the target element is no longer valid in the document DOM.</exception>
473-
public virtual string GetDomProperty(string propertyName)
495+
public virtual string? GetDomProperty(string propertyName)
474496
{
475497
Dictionary<string, object> parameters = new Dictionary<string, object>();
476498
parameters.Add("id", this.Id);
@@ -493,19 +515,21 @@ public virtual ISearchContext GetShadowRoot()
493515
parameters.Add("id", this.Id);
494516

495517
Response commandResponse = this.Execute(DriverCommand.GetElementShadowRoot, parameters);
496-
if (commandResponse.Value is not Dictionary<string, object> shadowRootDictionary)
518+
if (commandResponse.Value is not Dictionary<string, object?> shadowRootDictionary)
497519
{
498520
throw new WebDriverException("Get shadow root command succeeded, but response value does not represent a shadow root.");
499521
}
500522

501-
if (!ShadowRoot.TryCreate(this.driver, shadowRootDictionary, out ShadowRoot shadowRoot))
523+
if (!ShadowRoot.TryCreate(this.driver, shadowRootDictionary, out ShadowRoot? shadowRoot))
502524
{
503525
throw new WebDriverException("Get shadow root command succeeded, but response value does not have a shadow root key value.");
504526
}
505527

506528
return shadowRoot;
507529
}
508530

531+
#nullable restore
532+
509533
/// <summary>
510534
/// Gets the value of a CSS property of this element.
511535
/// </summary>
@@ -524,9 +548,12 @@ public virtual string GetCssValue(string propertyName)
524548
parameters.Add("name", propertyName);
525549

526550
Response commandResponse = this.Execute(DriverCommand.GetElementValueOfCssProperty, parameters);
551+
527552
return commandResponse.Value.ToString();
528553
}
529554

555+
#nullable enable
556+
530557
/// <summary>
531558
/// Gets a <see cref="Screenshot"/> object representing the image of this element on the screen.
532559
/// </summary>
@@ -538,7 +565,7 @@ public virtual Screenshot GetScreenshot()
538565

539566
// Get the screenshot as base64.
540567
Response screenshotResponse = this.Execute(DriverCommand.ElementScreenshot, parameters);
541-
string base64 = screenshotResponse.Value.ToString();
568+
string base64 = screenshotResponse.Value?.ToString() ?? throw new WebDriverException("ElementScreenshot command returned successfully, but with no response");
542569

543570
// ... and convert it.
544571
return new Screenshot(base64);
@@ -597,7 +624,7 @@ public virtual void SendKeys(string text)
597624
/// <exception cref="StaleElementReferenceException">Thrown when the target element is no longer valid in the document DOM.</exception>
598625
public virtual void Submit()
599626
{
600-
string elementType = this.GetAttribute("type");
627+
string? elementType = this.GetAttribute("type");
601628
if (elementType != null && elementType == "submit")
602629
{
603630
this.Click();
@@ -641,7 +668,7 @@ public override int GetHashCode()
641668
/// </summary>
642669
/// <param name="obj">Object to compare against</param>
643670
/// <returns>A boolean if it is equal or not</returns>
644-
public override bool Equals(object obj)
671+
public override bool Equals(object? obj)
645672
{
646673
if (obj is not IWebElement other)
647674
{
@@ -676,6 +703,8 @@ Dictionary<string, object> IWebDriverObjectReference.ToDictionary()
676703
return elementDictionary;
677704
}
678705

706+
#nullable restore
707+
679708
/// <summary>
680709
/// Executes a command on this element using the specified parameters.
681710
/// </summary>
@@ -687,6 +716,8 @@ protected virtual Response Execute(string commandToExecute, Dictionary<string, o
687716
return this.driver.InternalExecute(commandToExecute, parameters);
688717
}
689718

719+
#nullable enable
720+
690721
private static string GetAtom(string atomResourceName)
691722
{
692723
string atom = string.Empty;
@@ -703,6 +734,8 @@ private static string GetAtom(string atomResourceName)
703734
return wrappedAtom;
704735
}
705736

737+
#nullable restore
738+
706739
private string UploadFile(string localFile)
707740
{
708741
string base64zip;

0 commit comments

Comments
 (0)