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] Add NRT to Manage() and friends #14669

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions dotnet/src/webdriver/Alert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
using System;
using System.Collections.Generic;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
/// Defines the interface through which the user can manipulate JavaScript alerts.
/// </summary>
internal class Alert : IAlert
{
private WebDriver driver;
private readonly WebDriver driver;

/// <summary>
/// Initializes a new instance of the <see cref="Alert"/> class.
Expand All @@ -45,7 +47,7 @@ public string Text
get
{
Response commandResponse = this.driver.InternalExecute(DriverCommand.GetAlertText, null);
return commandResponse.Value.ToString();
return commandResponse.Value.ToString()!;
}
}

Expand All @@ -69,6 +71,7 @@ public void Accept()
/// Sends keys to the alert.
/// </summary>
/// <param name="keysToSend">The keystrokes to send.</param>
/// <exception cref="ArgumentNullException">If <paramref name="keysToSend" /> is <see langword="null"/>.</exception>
public void SendKeys(string keysToSend)
{
if (keysToSend == null)
Expand Down
49 changes: 26 additions & 23 deletions dotnet/src/webdriver/Cookie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
using System.Linq;
using System.Text.Json.Serialization;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand All @@ -33,9 +35,9 @@ public class Cookie
{
private string cookieName;
private string cookieValue;
private string cookiePath;
private string cookieDomain;
private string sameSite;
private string? cookiePath;
private string? cookieDomain;
private string? sameSite;
private bool isHttpOnly;
private bool secure;
private DateTime? cookieExpiry;
Expand Down Expand Up @@ -64,7 +66,7 @@ public Cookie(string name, string value)
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
/// or if it contains a semi-colon.</exception>
/// <exception cref="ArgumentNullException">If the value is <see langword="null"/>.</exception>
public Cookie(string name, string value, string path)
public Cookie(string name, string value, string? path)
: this(name, value, path, null)
{
}
Expand All @@ -80,7 +82,7 @@ public Cookie(string name, string value, string path)
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
/// or if it contains a semi-colon.</exception>
/// <exception cref="ArgumentNullException">If the value is <see langword="null"/>.</exception>
public Cookie(string name, string value, string path, DateTime? expiry)
public Cookie(string name, string value, string? path, DateTime? expiry)
: this(name, value, null, path, expiry)
{
}
Expand All @@ -97,7 +99,7 @@ public Cookie(string name, string value, string path, DateTime? expiry)
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
/// or if it contains a semi-colon.</exception>
/// <exception cref="ArgumentNullException">If the value is <see langword="null"/>.</exception>
public Cookie(string name, string value, string domain, string path, DateTime? expiry)
public Cookie(string name, string value, string? domain, string? path, DateTime? expiry)
: this(name, value, domain, path, expiry, false, false, null)
{
}
Expand All @@ -117,7 +119,7 @@ public Cookie(string name, string value, string domain, string path, DateTime? e
/// <exception cref="ArgumentException">If the name and value are both an empty string,
/// if the name contains a semi-colon, or if same site value is not valid.</exception>
/// <exception cref="ArgumentNullException">If the name, value or currentUrl is <see langword="null"/>.</exception>
public Cookie(string name, string value, string domain, string path, DateTime? expiry, bool secure, bool isHttpOnly, string sameSite)
public Cookie(string name, string value, string? domain, string? path, DateTime? expiry, bool secure, bool isHttpOnly, string? sameSite)
{
if (name == null)
{
Expand Down Expand Up @@ -190,7 +192,7 @@ public string Value
/// </summary>
[JsonPropertyName("domain")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Domain
public string? Domain
{
get { return this.cookieDomain; }
}
Expand All @@ -200,7 +202,7 @@ public string Domain
/// </summary>
[JsonPropertyName("path")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public virtual string Path
public virtual string? Path
{
get { return this.cookiePath; }
}
Expand Down Expand Up @@ -229,7 +231,7 @@ public virtual bool IsHttpOnly
/// </summary>
[JsonPropertyName("sameSite")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public virtual string SameSite
public virtual string? SameSite
{
get { return this.sameSite; }
}
Expand Down Expand Up @@ -272,27 +274,28 @@ internal long? ExpirySeconds
/// </summary>
/// <param name="rawCookie">The Dictionary object containing the cookie parameters.</param>
/// <returns>A <see cref="Cookie"/> object with the proper parameters set.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rawCookie"/> is null.</exception>
public static Cookie FromDictionary(Dictionary<string, object> rawCookie)
{
if (rawCookie == null)
{
throw new ArgumentNullException(nameof(rawCookie), "Dictionary cannot be null");
}

string name = rawCookie["name"].ToString();
string name = rawCookie["name"].ToString()!;
Copy link
Contributor Author

@RenderMichael RenderMichael Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If name and value are null, that'll surface in the constructor for Cookie. We can defer the null check here.

string value = string.Empty;
if (rawCookie["value"] != null)
{
value = rawCookie["value"].ToString();
value = rawCookie["value"].ToString()!;
}

string path = "/";
string? path = "/";
if (rawCookie.ContainsKey("path") && rawCookie["path"] != null)
{
path = rawCookie["path"].ToString();
}

string domain = string.Empty;
string? domain = string.Empty;
if (rawCookie.ContainsKey("domain") && rawCookie["domain"] != null)
{
domain = rawCookie["domain"].ToString();
Expand All @@ -307,16 +310,16 @@ public static Cookie FromDictionary(Dictionary<string, object> rawCookie)
bool secure = false;
if (rawCookie.ContainsKey("secure") && rawCookie["secure"] != null)
{
secure = bool.Parse(rawCookie["secure"].ToString());
secure = bool.Parse(rawCookie["secure"].ToString()!);
}

bool isHttpOnly = false;
if (rawCookie.ContainsKey("httpOnly") && rawCookie["httpOnly"] != null)
{
isHttpOnly = bool.Parse(rawCookie["httpOnly"].ToString());
isHttpOnly = bool.Parse(rawCookie["httpOnly"].ToString()!);
}

string sameSite = null;
string? sameSite = null;
if (rawCookie.ContainsKey("sameSite") && rawCookie["sameSite"] != null)
{
sameSite = rawCookie["sameSite"].ToString();
Expand Down Expand Up @@ -347,10 +350,10 @@ public override string ToString()
/// <returns><see langword="true"/> if the specified <see cref="object">Object</see>
/// is equal to the current <see cref="object">Object</see>; otherwise,
/// <see langword="false"/>.</returns>
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
// Two cookies are equal if the name and value match
Cookie cookie = obj as Cookie;
Cookie? cookie = obj as Cookie;

if (this == obj)
{
Expand All @@ -367,7 +370,7 @@ public override bool Equals(object obj)
return false;
}

return !(this.cookieValue != null ? !this.cookieValue.Equals(cookie.cookieValue) : cookie.Value != null);
return string.Equals(this.cookieValue, cookie.cookieValue);
}

/// <summary>
Expand All @@ -379,12 +382,12 @@ public override int GetHashCode()
return this.cookieName.GetHashCode();
}

private static string StripPort(string domain)
private static string? StripPort(string? domain)
{
return string.IsNullOrEmpty(domain) ? null : domain.Split(':')[0];
return string.IsNullOrEmpty(domain) ? null : domain!.Split(':')[0];
}

private static DateTime? ConvertExpirationTime(string expirationTime)
private static DateTime? ConvertExpirationTime(string? expirationTime)
{
DateTime? expires = null;
double seconds = 0;
Expand Down
26 changes: 17 additions & 9 deletions dotnet/src/webdriver/CookieJar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
/// Defines an interface allowing the user to manipulate cookies on the current page.
/// </summary>
internal class CookieJar : ICookieJar
{
private WebDriver driver;
private readonly WebDriver driver;

/// <summary>
/// Initializes a new instance of the <see cref="CookieJar"/> class.
Expand All @@ -50,8 +52,14 @@ public ReadOnlyCollection<Cookie> AllCookies
/// Method for creating a cookie in the browser
/// </summary>
/// <param name="cookie"><see cref="Cookie"/> that represents a cookie in the browser</param>
/// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
public void AddCookie(Cookie cookie)
{
if (cookie == null)
{
throw new ArgumentNullException(nameof(cookie));
}

Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("cookie", cookie);
this.driver.InternalExecute(DriverCommand.AddCookie, parameters);
Expand All @@ -61,9 +69,9 @@ public void AddCookie(Cookie cookie)
/// Delete the cookie by passing in the name of the cookie
/// </summary>
/// <param name="name">The name of the cookie that is in the browser</param>
public void DeleteCookieNamed(string name)
public void DeleteCookieNamed(string? name)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
Dictionary<string, object?> parameters = new Dictionary<string, object?>();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure how to handle this. When name is null, nothing seems to happen. So maybe we can do the same as DeleteCookie(Cookie) below?

parameters.Add("name", name);
this.driver.InternalExecute(DriverCommand.DeleteCookie, parameters);
}
Expand All @@ -72,7 +80,7 @@ public void DeleteCookieNamed(string name)
/// Delete a cookie in the browser by passing in a copy of a cookie
/// </summary>
/// <param name="cookie">An object that represents a copy of the cookie that needs to be deleted</param>
public void DeleteCookie(Cookie cookie)
public void DeleteCookie(Cookie? cookie)
{
if (cookie != null)
{
Expand All @@ -92,10 +100,10 @@ public void DeleteAllCookies()
/// Method for returning a getting a cookie by name
/// </summary>
/// <param name="name">name of the cookie that needs to be returned</param>
/// <returns>A Cookie from the name</returns>
public Cookie GetCookieNamed(string name)
/// <returns>A Cookie from the name, or <see langword="null"/> if not found.</returns>
public Cookie? GetCookieNamed(string? name)
{
Cookie cookieToReturn = null;
Cookie? cookieToReturn = null;
if (name != null)
{
ReadOnlyCollection<Cookie> allCookies = this.AllCookies;
Expand Down Expand Up @@ -123,14 +131,14 @@ private ReadOnlyCollection<Cookie> GetAllCookies()

try
{
object[] cookies = returned as object[];
object[]? cookies = returned as object[];
if (cookies != null)
{
foreach (object rawCookie in cookies)
{
Dictionary<string, object> cookieDictionary = rawCookie as Dictionary<string, object>;
if (rawCookie != null)
{
Dictionary<string, object> cookieDictionary = (Dictionary<string, object>)rawCookie;
toReturn.Add(Cookie.FromDictionary(cookieDictionary));
}
}
Expand Down
10 changes: 7 additions & 3 deletions dotnet/src/webdriver/ICookieJar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.ObjectModel;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand All @@ -34,6 +37,7 @@ public interface ICookieJar
/// Adds a cookie to the current page.
/// </summary>
/// <param name="cookie">The <see cref="Cookie"/> object to be added.</param>
/// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
void AddCookie(Cookie cookie);

/// <summary>
Expand All @@ -42,19 +46,19 @@ public interface ICookieJar
/// <param name="name">The name of the cookie to retrieve.</param>
/// <returns>The <see cref="Cookie"/> containing the name. Returns <see langword="null"/>
/// if no cookie with the specified name is found.</returns>
Cookie GetCookieNamed(string name);
Cookie? GetCookieNamed(string? name);

/// <summary>
/// Deletes the specified cookie from the page.
/// </summary>
/// <param name="cookie">The <see cref="Cookie"/> to be deleted.</param>
void DeleteCookie(Cookie cookie);
void DeleteCookie(Cookie? cookie);

/// <summary>
/// Deletes the cookie with the specified name from the page.
/// </summary>
/// <param name="name">The name of the cookie to be deleted.</param>
void DeleteCookieNamed(string name);
void DeleteCookieNamed(string? name);

/// <summary>
/// Deletes all cookies from the page.
Expand Down
4 changes: 4 additions & 0 deletions dotnet/src/webdriver/ILogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.ObjectModel;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand All @@ -36,6 +39,7 @@ public interface ILogs
/// <param name="logKind">The log for which to retrieve the log entries.
/// Log types can be found in the <see cref="LogType"/> class.</param>
/// <returns>The list of <see cref="LogEntry"/> objects for the specified log.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="logKind"/> is <see langword="null"/>.</exception>
ReadOnlyCollection<LogEntry> GetLog(string logKind);
}
}
7 changes: 5 additions & 2 deletions dotnet/src/webdriver/INetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using System;
using System.Threading.Tasks;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand All @@ -29,18 +31,19 @@ public interface INetwork
/// <summary>
/// Occurs when a browser sends a network request.
/// </summary>
event EventHandler<NetworkRequestSentEventArgs> NetworkRequestSent;
event EventHandler<NetworkRequestSentEventArgs>? NetworkRequestSent;

/// <summary>
/// Occurs when a browser receives a network response.
/// </summary>
event EventHandler<NetworkResponseReceivedEventArgs> NetworkResponseReceived;
event EventHandler<NetworkResponseReceivedEventArgs>? NetworkResponseReceived;

/// <summary>
/// Adds a <see cref="NetworkRequestHandler"/> to examine incoming network requests,
/// and optionally modify the request or provide a response.
/// </summary>
/// <param name="handler">The <see cref="NetworkRequestHandler"/> to add.</param>
/// <exception cref="ArgumentNullException">If <paramref name="handler"/> is <see langword="null"/>.</exception>
void AddRequestHandler(NetworkRequestHandler handler);

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/IOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// limitations under the License.
// </copyright>

#nullable enable

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