Skip to content

Commit e8c53af

Browse files
committed
Implement nullability on more of WebElement
1 parent 696832f commit e8c53af

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

dotnet/src/webdriver/WebElement.cs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ public WebElement(WebDriver parentDriver, string id)
6161
/// </summary>
6262
public IWebDriver WrappedDriver => this.driver;
6363

64-
#nullable restore
65-
6664
/// <summary>
6765
/// Gets the tag name of this element.
6866
/// </summary>
@@ -81,7 +79,11 @@ public virtual string TagName
8179

8280
Response commandResponse = this.Execute(DriverCommand.GetElementTagName, parameters);
8381

84-
return commandResponse.Value.ToString();
82+
if (commandResponse.Value is not Dictionary<string, object?> rawSize)
83+
{
84+
throw new WebDriverException($"GetElementTagName command was successful, but response was not an object: {commandResponse.Value}");
85+
}
86+
return commandResponse.Value.ToString()!;
8587
}
8688
}
8789

@@ -99,12 +101,15 @@ public virtual string Text
99101

100102
Response commandResponse = this.Execute(DriverCommand.GetElementText, parameters);
101103

102-
return commandResponse.Value.ToString();
104+
if (commandResponse.Value is not Dictionary<string, object?> rawSize)
105+
{
106+
throw new WebDriverException($"GetElementText command was successful, but response was not an object: {commandResponse.Value}");
107+
}
108+
109+
return commandResponse.Value.ToString()!;
103110
}
104111
}
105112

106-
#nullable enable
107-
108113
/// <summary>
109114
/// Gets a value indicating whether or not this element is enabled.
110115
/// </summary>
@@ -231,8 +236,6 @@ public virtual Point LocationOnScreenOnceScrolledIntoView
231236
}
232237
}
233238

234-
#nullable restore
235-
236239
/// <summary>
237240
/// Gets the computed accessible label of this element.
238241
/// </summary>
@@ -245,7 +248,12 @@ public virtual string ComputedAccessibleLabel
245248

246249
Response commandResponse = this.Execute(DriverCommand.GetComputedAccessibleLabel, parameters);
247250

248-
return commandResponse.Value.ToString();
251+
if (commandResponse.Value is null)
252+
{
253+
throw new WebDriverException("GetComputedAccessibleLabel command returned a successful result, but contained no data");
254+
}
255+
256+
return commandResponse.Value.ToString()!;
249257
}
250258
}
251259

@@ -256,21 +264,21 @@ public virtual string ComputedAccessibleRole
256264
{
257265
get
258266
{
259-
// TODO: Returning this as a string is incorrect. The W3C WebDriver Specification
260-
// needs to be updated to more thoroughly document the structure of what is returned
261-
// by this command. Once that is done, a type-safe class will be created, and will
262-
// be returned by this property.
263267
Dictionary<string, object> parameters = new Dictionary<string, object>();
264268
parameters.Add("id", this.Id);
265269

266270
Response commandResponse = this.Execute(DriverCommand.GetComputedAccessibleRole, parameters);
267271

272+
#nullable disable
273+
// TODO: Returning this as a string is incorrect. The W3C WebDriver Specification
274+
// needs to be updated to more thoroughly document the structure of what is returned
275+
// by this command. Once that is done, a type-safe class will be created, and will
276+
// be returned by this property.
268277
return commandResponse.Value.ToString();
278+
#nullable enable
269279
}
270280
}
271281

272-
#nullable enable
273-
274282
/// <summary>
275283
/// Gets the coordinates identifying the location of this element using
276284
/// various frames of reference.
@@ -331,6 +339,8 @@ public virtual void Click()
331339
this.Execute(DriverCommand.ClickElement, parameters);
332340
}
333341

342+
#nullable restore
343+
334344
/// <summary>
335345
/// Finds the first <see cref="IWebElement"/> using the given method.
336346
/// </summary>
@@ -348,7 +358,7 @@ public virtual IWebElement FindElement(By by)
348358
return by.FindElement(this);
349359
}
350360

351-
#nullable restore
361+
#nullable enable
352362

353363
/// <summary>
354364
/// Finds a child element matching the given mechanism and value.
@@ -368,6 +378,8 @@ public virtual IWebElement FindElement(string mechanism, string value)
368378
return this.driver.GetElementFromResponse(commandResponse);
369379
}
370380

381+
#nullable restore
382+
371383
/// <summary>
372384
/// Finds all <see cref="IWebElement">IWebElements</see> within the current context
373385
/// using the given mechanism.
@@ -385,6 +397,8 @@ public virtual ReadOnlyCollection<IWebElement> FindElements(By by)
385397
return by.FindElements(this);
386398
}
387399

400+
#nullable enable
401+
388402
/// <summary>
389403
/// Finds all child elements matching the given mechanism and value.
390404
/// </summary>
@@ -403,8 +417,6 @@ public virtual ReadOnlyCollection<IWebElement> FindElements(string mechanism, st
403417
return this.driver.GetElementsFromResponse(commandResponse);
404418
}
405419

406-
#nullable enable
407-
408420
/// <summary>
409421
/// Gets the value of the specified attribute or property for this element.
410422
/// </summary>

0 commit comments

Comments
 (0)