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 SafariOptions, error, and enums #15219

Merged
merged 2 commits into from
Feb 3, 2025
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
13 changes: 11 additions & 2 deletions dotnet/src/webdriver/CapabilityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
// </copyright>

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

#nullable enable

namespace OpenQA.Selenium
{
Expand Down Expand Up @@ -167,7 +170,8 @@ public static class CapabilityType
/// </summary>
public static readonly string EnableDownloads = "se:downloadsEnabled";

private static readonly List<string> KnownSpecCompliantCapabilityNames = new List<string>() {
private static readonly HashSet<string> KnownSpecCompliantCapabilityNames = new HashSet<string>()
{
BrowserName,
BrowserVersion,
PlatformName,
Expand All @@ -188,8 +192,13 @@ public static class CapabilityType
/// <param name="capabilityName">The name of the capability to check for compliance.</param>
/// <returns><see langword="true"/> if the capability name is valid according to the rules
/// of the specification; otherwise, <see langword="false"/>.</returns>
public static bool IsSpecCompliantCapabilityName(string capabilityName)
public static bool IsSpecCompliantCapabilityName([NotNullWhen(true)] string? capabilityName)
{
if (capabilityName is null)
{
return false;
}

if (KnownSpecCompliantCapabilityNames.Contains(capabilityName) || capabilityName.Contains(":"))
{
return true;
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/IRotatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// under the License.
// </copyright>

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down
31 changes: 9 additions & 22 deletions dotnet/src/webdriver/Safari/SafariOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// under the License.
// </copyright>

#nullable enable

namespace OpenQA.Selenium.Safari
{
/// <summary>
Expand Down Expand Up @@ -46,17 +48,13 @@ public class SafariOptions : DriverOptions
private const string EnableAutomaticInspectionSafariOption = "safari:automaticInspection";
private const string EnableAutomaticProfilingSafariOption = "safari:automaticProfiling";

private bool enableAutomaticInspection = false;
private bool enableAutomaticProfiling = false;
private bool technologyPreview = false;

/// <summary>
/// Initializes a new instance of the <see cref="SafariOptions"/> class.
/// </summary>
public SafariOptions() : base()
{
this.BrowserName = BrowserNameValue;
this.technologyPreview = false;
this.TechnologyPreview = false;
this.AddKnownCapabilityName(SafariOptions.EnableAutomaticInspectionSafariOption, "EnableAutomaticInspection property");
this.AddKnownCapabilityName(SafariOptions.EnableAutomaticProfilingSafariOption, "EnableAutomaticProfiling property");
}
Expand All @@ -66,38 +64,27 @@ public SafariOptions() : base()
/// </summary>
public void UseTechnologyPreview()
{
this.technologyPreview = true;
this.TechnologyPreview = true;
this.BrowserName = "Safari Technology Preview";
}

/// <summary>
/// Gets or sets a value indicating whether to have the driver preload the
/// Web Inspector and JavaScript debugger in the background.
/// </summary>
public bool TechnologyPreview
{
get { return this.technologyPreview; }
}
public bool TechnologyPreview { get; private set; } = false;

/// <summary>
/// Gets or sets a value indicating whether to have the driver preload the
/// Web Inspector and JavaScript debugger in the background.
/// </summary>
public bool EnableAutomaticInspection
{
get { return this.enableAutomaticInspection; }
set { this.enableAutomaticInspection = value; }
}
public bool EnableAutomaticInspection { get; set; } = false;

/// <summary>
/// Gets or sets a value indicating whether to have the driver preload the
/// Web Inspector and start a timeline recording in the background.
/// </summary>
public bool EnableAutomaticProfiling
{
get { return this.enableAutomaticProfiling; }
set { this.enableAutomaticProfiling = value; }
}
public bool EnableAutomaticProfiling { get; set; } = false;

/// <summary>
/// Returns ICapabilities for Safari with these options included as
Expand All @@ -108,12 +95,12 @@ public bool EnableAutomaticProfiling
public override ICapabilities ToCapabilities()
{
IWritableCapabilities capabilities = this.GenerateDesiredCapabilities(true);
if (this.enableAutomaticInspection)
if (this.EnableAutomaticInspection)
{
capabilities.SetCapability(EnableAutomaticInspectionSafariOption, true);
}

if (this.enableAutomaticProfiling)
if (this.EnableAutomaticProfiling)
{
capabilities.SetCapability(EnableAutomaticProfilingSafariOption, true);
}
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/ScreenOrientation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// under the License.
// </copyright>

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down
73 changes: 37 additions & 36 deletions dotnet/src/webdriver/WebDriverError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
// under the License.
// </copyright>

using System;
using System.Collections.Generic;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down Expand Up @@ -167,54 +170,52 @@ internal static class WebDriverError
/// </summary>
public const string UnsupportedOperation = "unsupported operation";

private static readonly Dictionary<string, WebDriverResult> resultMap;

static WebDriverError()
private static readonly Dictionary<string, WebDriverResult> resultMap = new Dictionary<string, WebDriverResult>
{
resultMap = new Dictionary<string, WebDriverResult>();
resultMap[ElementClickIntercepted] = WebDriverResult.ElementClickIntercepted;
resultMap[ElementNotInteractable] = WebDriverResult.ElementNotInteractable;
resultMap[InsecureCertificate] = WebDriverResult.InsecureCertificate;
resultMap[InvalidArgument] = WebDriverResult.InvalidArgument;
resultMap[InvalidCookieDomain] = WebDriverResult.InvalidCookieDomain;
resultMap[InvalidElementState] = WebDriverResult.InvalidElementState;
resultMap[InvalidSelector] = WebDriverResult.InvalidSelector;
resultMap[InvalidSessionId] = WebDriverResult.NoSuchDriver;
resultMap[JavaScriptError] = WebDriverResult.UnexpectedJavaScriptError;
resultMap[MoveTargetOutOfBounds] = WebDriverResult.MoveTargetOutOfBounds;
resultMap[NoSuchAlert] = WebDriverResult.NoAlertPresent;
resultMap[NoSuchCookie] = WebDriverResult.NoSuchCookie;
resultMap[NoSuchElement] = WebDriverResult.NoSuchElement;
resultMap[NoSuchFrame] = WebDriverResult.NoSuchFrame;
resultMap[NoSuchWindow] = WebDriverResult.NoSuchWindow;
resultMap[NoSuchShadowRoot] = WebDriverResult.NoSuchShadowRoot;
resultMap[ScriptTimeout] = WebDriverResult.AsyncScriptTimeout;
resultMap[SessionNotCreated] = WebDriverResult.SessionNotCreated;
resultMap[StaleElementReference] = WebDriverResult.ObsoleteElement;
resultMap[DetachedShadowRoot] = WebDriverResult.DetachedShadowRoot;
resultMap[Timeout] = WebDriverResult.Timeout;
resultMap[UnableToSetCookie] = WebDriverResult.UnableToSetCookie;
resultMap[UnableToCaptureScreen] = WebDriverResult.UnableToCaptureScreen;
resultMap[UnexpectedAlertOpen] = WebDriverResult.UnexpectedAlertOpen;
resultMap[UnknownCommand] = WebDriverResult.UnknownCommand;
resultMap[UnknownError] = WebDriverResult.UnknownError;
resultMap[UnknownMethod] = WebDriverResult.UnknownMethod;
resultMap[UnsupportedOperation] = WebDriverResult.UnsupportedOperation;
}
{ ElementClickIntercepted, WebDriverResult.ElementClickIntercepted },
{ ElementNotInteractable, WebDriverResult.ElementNotInteractable },
{ InsecureCertificate, WebDriverResult.InsecureCertificate },
{ InvalidArgument, WebDriverResult.InvalidArgument },
{ InvalidCookieDomain, WebDriverResult.InvalidCookieDomain },
{ InvalidElementState, WebDriverResult.InvalidElementState },
{ InvalidSelector, WebDriverResult.InvalidSelector },
{ InvalidSessionId, WebDriverResult.NoSuchDriver },
{ JavaScriptError, WebDriverResult.UnexpectedJavaScriptError },
{ MoveTargetOutOfBounds, WebDriverResult.MoveTargetOutOfBounds },
{ NoSuchAlert, WebDriverResult.NoAlertPresent },
{ NoSuchCookie, WebDriverResult.NoSuchCookie },
{ NoSuchElement, WebDriverResult.NoSuchElement },
{ NoSuchFrame, WebDriverResult.NoSuchFrame },
{ NoSuchWindow, WebDriverResult.NoSuchWindow },
{ NoSuchShadowRoot, WebDriverResult.NoSuchShadowRoot },
{ ScriptTimeout, WebDriverResult.AsyncScriptTimeout },
{ SessionNotCreated, WebDriverResult.SessionNotCreated },
{ StaleElementReference, WebDriverResult.ObsoleteElement },
{ DetachedShadowRoot, WebDriverResult.DetachedShadowRoot },
{ Timeout, WebDriverResult.Timeout },
{ UnableToSetCookie, WebDriverResult.UnableToSetCookie },
{ UnableToCaptureScreen, WebDriverResult.UnableToCaptureScreen },
{ UnexpectedAlertOpen, WebDriverResult.UnexpectedAlertOpen },
{ UnknownCommand, WebDriverResult.UnknownCommand },
{ UnknownError, WebDriverResult.UnknownError },
{ UnknownMethod, WebDriverResult.UnknownMethod },
{ UnsupportedOperation, WebDriverResult.UnsupportedOperation }
};

/// <summary>
/// Converts a string error to a <see cref="WebDriverResult"/> value.
/// </summary>
/// <param name="error">The error string to convert.</param>
/// <returns>The converted <see cref="WebDriverResult"/> value.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="error"/> is <see langword="null"/>.</exception>
public static WebDriverResult ResultFromError(string error)
{
if (!resultMap.ContainsKey(error))
if (!resultMap.TryGetValue(error, out WebDriverResult result))
{
error = UnsupportedOperation;
return WebDriverResult.UnsupportedOperation;
}

return resultMap[error];
return result;
}
}
}
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/WebDriverResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

using System;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions dotnet/src/webdriver/WindowType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
// under the License.
// </copyright>


#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down
Loading