Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Annotate nullability on elements and WebDriver #15352

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions dotnet/src/support/Events/EventFiringWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,14 +442,14 @@ public void Dispose()
/// variable, as if the function were called via "Function.apply"
/// </para>
/// </remarks>
public object ExecuteScript(string script, params object?[] args)
public object? ExecuteScript(string script, params object?[] args)
{
if (this.WrappedDriver is not IJavaScriptExecutor javascriptDriver)
{
throw new NotSupportedException("Underlying driver instance does not support executing JavaScript");
}

object scriptResult;
object? scriptResult;
try
{
object?[] unwrappedArgs = UnwrapElementArguments(args);
Expand Down Expand Up @@ -505,7 +505,7 @@ public object ExecuteScript(string script, params object?[] args)
/// variable, as if the function were called via "Function.apply"
/// </para>
/// </remarks>
public object ExecuteScript(PinnedScript script, params object?[] args)
public object? ExecuteScript(PinnedScript script, params object?[] args)
{
if (script == null)
{
Expand All @@ -517,7 +517,7 @@ public object ExecuteScript(PinnedScript script, params object?[] args)
throw new NotSupportedException("Underlying driver instance does not support executing JavaScript");
}

object scriptResult;
object? scriptResult;
try
{
object?[] unwrappedArgs = UnwrapElementArguments(args);
Expand All @@ -542,14 +542,14 @@ public object ExecuteScript(PinnedScript script, params object?[] args)
/// <param name="script">The JavaScript code to execute.</param>
/// <param name="args">The arguments to the script.</param>
/// <returns>The value returned by the script.</returns>
public object ExecuteAsyncScript(string script, params object?[] args)
public object? ExecuteAsyncScript(string script, params object?[] args)
{
if (this.WrappedDriver is not IJavaScriptExecutor javascriptDriver)
{
throw new NotSupportedException("Underlying driver instance does not support executing JavaScript");
}

object scriptResult;
object? scriptResult;
try
{
object?[] unwrappedArgs = UnwrapElementArguments(args);
Expand Down Expand Up @@ -1589,20 +1589,17 @@ public void Click()
/// </summary>
/// <param name="attributeName">Attribute you wish to get details of</param>
/// <returns>The attribute's current value or null if the value is not set.</returns>
public string GetAttribute(string attributeName)
public string? GetAttribute(string attributeName)
{
string attribute;
try
{
attribute = this.WrappedElement.GetAttribute(attributeName);
return this.WrappedElement.GetAttribute(attributeName);
}
catch (Exception ex)
{
this.parentDriver.OnException(new WebDriverExceptionEventArgs(this.parentDriver, ex));
throw;
}

return attribute;
}

/// <summary>
Expand All @@ -1617,20 +1614,17 @@ public string GetAttribute(string attributeName)
/// of an IDL property of the element, either use the <see cref="GetAttribute(string)"/>
/// method or the <see cref="GetDomProperty(string)"/> method.
/// </remarks>
public string GetDomAttribute(string attributeName)
public string? GetDomAttribute(string attributeName)
{
string attribute;
try
{
attribute = this.WrappedElement.GetDomAttribute(attributeName);
return this.WrappedElement.GetDomAttribute(attributeName);
}
catch (Exception ex)
{
this.parentDriver.OnException(new WebDriverExceptionEventArgs(this.parentDriver, ex));
throw;
}

return attribute;
}

/// <summary>
Expand All @@ -1639,20 +1633,17 @@ public string GetDomAttribute(string attributeName)
/// <param name="propertyName">The name of the JavaScript property to get the value of.</param>
/// <returns>The JavaScript property's current value. Returns a <see langword="null"/> if the
/// value is not set or the property does not exist.</returns>
public string GetDomProperty(string propertyName)
public string? GetDomProperty(string propertyName)
{
string elementProperty;
try
{
elementProperty = this.WrappedElement.GetDomProperty(propertyName);
return this.WrappedElement.GetDomProperty(propertyName);
}
catch (Exception ex)
{
this.parentDriver.OnException(new WebDriverExceptionEventArgs(this.parentDriver, ex));
throw;
}

return elementProperty;
}

/// <summary>
Expand Down Expand Up @@ -1683,22 +1674,19 @@ public string GetCssValue(string propertyName)
/// <returns>A shadow root representation.</returns>
public ISearchContext GetShadowRoot()
{
ISearchContext shadowRoot;
try
{
GetShadowRootEventArgs e = new GetShadowRootEventArgs(this.parentDriver.WrappedDriver, this.WrappedElement);
this.parentDriver.OnGettingShadowRoot(e);
shadowRoot = this.WrappedElement.GetShadowRoot();
ISearchContext shadowRoot = this.WrappedElement.GetShadowRoot();
this.parentDriver.OnGetShadowRootCompleted(e);
shadowRoot = new EventFiringShadowRoot(this.parentDriver, shadowRoot);
return new EventFiringShadowRoot(this.parentDriver, shadowRoot);
}
catch (Exception ex)
{
this.parentDriver.OnException(new WebDriverExceptionEventArgs(this.parentDriver, ex));
throw;
}

return shadowRoot;
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion dotnet/src/webdriver/By.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected By(Func<ISearchContext, IWebElement> findElementMethod, Func<ISearchCo
protected string Description { get; set; } = "OpenQA.Selenium.By";

/// <summary>
/// Gets or sets the method used to find a single element matching specified criteria.
/// Gets or sets the method used to find a single element matching specified criteria, or throws <see cref="NoSuchElementException"/> if no element is found.
/// </summary>
protected Func<ISearchContext, IWebElement>? FindElementMethod { get; set; }

Expand Down Expand Up @@ -324,6 +324,7 @@ public static By CssSelector(string cssSelectorToFind)
/// </summary>
/// <param name="context">An <see cref="ISearchContext"/> object to use to search for the elements.</param>
/// <returns>The first matching <see cref="IWebElement"/> on the current context.</returns>
/// <exception cref="NoSuchElementException">If no element matches the criteria.</exception>
public virtual IWebElement FindElement(ISearchContext context)
{
if (this.FindElementMethod is not { } findElementMethod)
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Chromium/ChromiumDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverServi
string fullServicePath = finder.GetDriverPath();
service.DriverServicePath = Path.GetDirectoryName(fullServicePath);
service.DriverServiceExecutableName = Path.GetFileName(fullServicePath);
if (finder.TryGetBrowserPath(out string browserPath))
if (finder.TryGetBrowserPath(out string? browserPath))
{
options.BinaryLocation = browserPath;
options.BrowserVersion = null;
Expand Down
10 changes: 5 additions & 5 deletions dotnet/src/webdriver/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public Command(string name, string jsonParameters)
/// <param name="name">Name of the command</param>
/// <param name="parameters">Parameters for that command</param>
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/>.</exception>
public Command(SessionId? sessionId, string name, Dictionary<string, object>? parameters)
public Command(SessionId? sessionId, string name, Dictionary<string, object?>? parameters)
{
this.SessionId = sessionId;
this.Parameters = parameters ?? new Dictionary<string, object>();
this.Parameters = parameters ?? new Dictionary<string, object?>();
this.Name = name ?? throw new ArgumentNullException(nameof(name));
}

Expand All @@ -79,7 +79,7 @@ public Command(SessionId? sessionId, string name, Dictionary<string, object>? pa
/// Gets the parameters of the command
/// </summary>
[JsonPropertyName("parameters")]
public Dictionary<string, object> Parameters { get; }
public Dictionary<string, object?> Parameters { get; }

/// <summary>
/// Gets the parameters of the command as a JSON-encoded string.
Expand Down Expand Up @@ -118,9 +118,9 @@ public override string ToString()
/// <returns>A <see cref="Dictionary{K, V}"/> with a string keys, and an object value. </returns>
/// <exception cref="JsonException">If <paramref name="value"/> is not a JSON object.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="value"/> is <see langword="null"/>.</exception>
private static Dictionary<string, object>? ConvertParametersFromJson(string value)
private static Dictionary<string, object?>? ConvertParametersFromJson(string value)
{
Dictionary<string, object>? parameters = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions);
Dictionary<string, object?>? parameters = JsonSerializer.Deserialize<Dictionary<string, object?>>(value, s_jsonSerializerOptions);
return parameters;
}
}
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/CookieJar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void DeleteAllCookies()
{
var rawCookie = driver.InternalExecute(DriverCommand.GetCookie, new() { { "name", name } }).Value;

return Cookie.FromDictionary((Dictionary<string, object>)rawCookie!);
return Cookie.FromDictionary((Dictionary<string, object?>)rawCookie!);
}
catch (NoSuchCookieException)
{
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Firefox/FirefoxDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverServi
string fullServicePath = finder.GetDriverPath();
service.DriverServicePath = Path.GetDirectoryName(fullServicePath);
service.DriverServiceExecutableName = Path.GetFileName(fullServicePath);
if (finder.TryGetBrowserPath(out string browserPath))
if (finder.TryGetBrowserPath(out string? browserPath))
{
options.BinaryLocation = browserPath;
options.BrowserVersion = null;
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Firefox/FirefoxProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void WriteToDisk()
this.ProfileDirectory = GenerateProfileDirectoryName();
if (!string.IsNullOrEmpty(this.sourceProfileDir))
{
FileUtilities.CopyDirectory(this.sourceProfileDir, this.ProfileDirectory);
FileUtilities.CopyDirectory(this.sourceProfileDir!, this.ProfileDirectory);
}
else
{
Expand Down
8 changes: 5 additions & 3 deletions dotnet/src/webdriver/IJavascriptExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using System;
using System.Collections.Generic;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down Expand Up @@ -62,7 +64,7 @@ public interface IJavaScriptExecutor
/// variable, as if the function were called via "Function.apply"
/// </para>
/// </remarks>
object ExecuteScript(string script, params object[] args);
object? ExecuteScript(string script, params object?[] args);

/// <summary>
/// Executes JavaScript in the context of the currently selected frame or window.
Expand Down Expand Up @@ -100,14 +102,14 @@ public interface IJavaScriptExecutor
/// </para>
/// </remarks>
/// <exception cref="ArgumentNullException">If <paramref name="script" /> is <see langword="null"/>.</exception>
object ExecuteScript(PinnedScript script, params object[] args);
object? ExecuteScript(PinnedScript script, params object?[] args);

/// <summary>
/// Executes JavaScript asynchronously in the context of the currently selected frame or window.
/// </summary>
/// <param name="script">The JavaScript code to execute.</param>
/// <param name="args">The arguments to the script.</param>
/// <returns>The value returned by the script.</returns>
object ExecuteAsyncScript(string script, params object[] args);
object? ExecuteAsyncScript(string script, params object?[] args);
}
}
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/IWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using System;
using System.Collections.ObjectModel;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down
8 changes: 5 additions & 3 deletions dotnet/src/webdriver/IWebElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using System;
using System.Drawing;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down Expand Up @@ -170,7 +172,7 @@ public interface IWebElement : ISearchContext
/// </list>
/// </remarks>
/// <exception cref="StaleElementReferenceException">Thrown when the target element is no longer valid in the document DOM.</exception>
string GetAttribute(string attributeName);
string? GetAttribute(string attributeName);

/// <summary>
/// Gets the value of a declared HTML attribute of this element.
Expand All @@ -185,7 +187,7 @@ public interface IWebElement : ISearchContext
/// of an IDL property of the element, either use the <see cref="GetAttribute(string)"/>
/// method or the <see cref="GetDomProperty(string)"/> method.
/// </remarks>
string GetDomAttribute(string attributeName);
string? GetDomAttribute(string attributeName);

/// <summary>
/// Gets the value of a JavaScript property of this element.
Expand All @@ -194,7 +196,7 @@ public interface IWebElement : ISearchContext
/// <returns>The JavaScript property's current value. Returns a <see langword="null"/> if the
/// value is not set or the property does not exist.</returns>
/// <exception cref="StaleElementReferenceException">Thrown when the target element is no longer valid in the document DOM.</exception>
string GetDomProperty(string propertyName);
string? GetDomProperty(string propertyName);

/// <summary>
/// Gets the value of a CSS property of this element.
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/RelativeBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public override ReadOnlyCollection<IWebElement> FindElements(ISearchContext cont
filterParameters["root"] = GetSerializableObject(this.root);
filterParameters["filters"] = this.filters;
parameters["relative"] = filterParameters;
object rawElements = js.ExecuteScript(wrappedAtom, parameters);
object? rawElements = js.ExecuteScript(wrappedAtom, parameters);

if (rawElements is ReadOnlyCollection<IWebElement> elements)
{
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/ShadowRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public IWebElement FindElement(By by)
parameters.Add("value", by.Criteria);

Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElement, parameters);
return this.driver.GetElementFromResponse(commandResponse);
return this.driver.GetElementFromResponse(commandResponse)!;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/webdriver/TargetLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public IWebDriver Window(string windowHandleOrName)
foreach (string handle in this.driver.WindowHandles)
{
this.Window(handle);
if (windowHandleOrName == this.driver.ExecuteScript("return window.name").ToString())
if (windowHandleOrName == this.driver.ExecuteScript("return window.name")!.ToString())
{
return this.driver; // found by name
}
Expand Down Expand Up @@ -223,7 +223,7 @@ public IWebDriver DefaultContent()
public IWebElement ActiveElement()
{
Response response = this.driver.InternalExecute(DriverCommand.GetActiveElement, null);
return this.driver.GetElementFromResponse(response);
return this.driver.GetElementFromResponse(response)!;
}

/// <summary>
Expand Down
Loading