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 interactions #15152

Open
wants to merge 1 commit 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
31 changes: 9 additions & 22 deletions dotnet/src/webdriver/ElementCoordinates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,40 @@
using OpenQA.Selenium.Internal;
using System;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
/// Defines the interface through which the user can discover where an element is on the screen.
/// </summary>
internal class ElementCoordinates : ICoordinates
internal sealed class ElementCoordinates : ICoordinates
{
private WebElement element;
private readonly WebElement element;

/// <summary>
/// Initializes a new instance of the <see cref="ElementCoordinates"/> class.
/// </summary>
/// <param name="element">The <see cref="WebElement"/> to be located.</param>
public ElementCoordinates(WebElement element)
{
this.element = element;
this.element = element ?? throw new ArgumentNullException(nameof(element));
}

/// <summary>
/// Gets the location of an element in absolute screen coordinates.
/// </summary>
public System.Drawing.Point LocationOnScreen
{
get { throw new NotImplementedException(); }
}
public System.Drawing.Point LocationOnScreen => throw new NotImplementedException();

/// <summary>
/// Gets the location of an element relative to the origin of the view port.
/// </summary>
public System.Drawing.Point LocationInViewport
{
get { return this.element.LocationOnScreenOnceScrolledIntoView; }
}
public System.Drawing.Point LocationInViewport => this.element.LocationOnScreenOnceScrolledIntoView;

/// <summary>
/// Gets the location of an element's position within the HTML DOM.
/// </summary>
public System.Drawing.Point LocationInDom
{
get { return this.element.Location; }
}
public System.Drawing.Point LocationInDom => this.element.Location;

/// <summary>
/// Gets a locator providing a user-defined location for this element.
Expand All @@ -70,17 +63,11 @@ public object AuxiliaryLocator
{
get
{
IWebDriverObjectReference elementReference = this.element as IWebDriverObjectReference;
if (elementReference == null)
{
return null;
}

// Note that the OSS dialect of the wire protocol for the Actions API
// uses the raw ID of the element, not an element reference. To use this,
// extract the ID using the well-known key to the dictionary for element
// references.
return elementReference.ObjectReferenceId;
return ((IWebDriverObjectReference)this.element).ObjectReferenceId;
Copy link
Contributor Author

@RenderMichael RenderMichael Jan 24, 2025

Choose a reason for hiding this comment

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

element is WebElement, which implements IWebDriverObjectReference directly.

None of the implementations of ObjectReferenceId can return null (WebElement and ShadowRoot).

}
}
}
Expand Down
18 changes: 6 additions & 12 deletions dotnet/src/webdriver/Interactions/ActionSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
using System.Globalization;
using System.Text;

#nullable enable

namespace OpenQA.Selenium.Interactions
{
/// <summary>
/// Represents a sequence of actions to be performed in the target browser.
/// </summary>
public class ActionSequence
{
private List<Interaction> interactions = new List<Interaction>();
private readonly List<Interaction> interactions = new List<Interaction>();

/// <summary>
/// Initializes a new instance of the <see cref="ActionSequence"/> class.
Expand All @@ -47,26 +49,18 @@ public ActionSequence(InputDevice device)
/// <param name="initialSize">the initial size of the sequence.</param>
public ActionSequence(InputDevice device, int initialSize)
{
if (device == null)
{
throw new ArgumentNullException(nameof(device), "Input device cannot be null.");
}

this.InputDevice = device;
this.InputDevice = device ?? throw new ArgumentNullException(nameof(device), "Input device cannot be null.");

for (int i = 0; i < initialSize; i++)
{
this.AddAction(new PauseInteraction(device, TimeSpan.Zero));
this.AddAction(new PauseInteraction(this.InputDevice, TimeSpan.Zero));
}
}

/// <summary>
/// Gets the count of actions in the sequence.
/// </summary>
public int Count
{
get { return this.interactions.Count; }
}
public int Count => this.interactions.Count;

/// <summary>
/// Gets the input device for this Action sequence.
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/Interactions/IAction.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.Interactions
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/Interactions/ICoordinates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

using System.Drawing;

#nullable enable

namespace OpenQA.Selenium.Interactions.Internal
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/Interactions/InputDeviceKind.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.Interactions
{
/// <summary>
Expand Down
18 changes: 5 additions & 13 deletions dotnet/src/webdriver/Interactions/Interaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,28 @@
using System;
using System.Collections.Generic;

#nullable enable

namespace OpenQA.Selenium.Interactions
{
/// <summary>
/// Represents a single interaction for a given input device.
/// </summary>
public abstract class Interaction
{
private InputDevice sourceDevice;

/// <summary>
/// Initializes a new instance of the <see cref="Interaction"/> class.
/// </summary>
/// <param name="sourceDevice">The input device which performs this action.</param>
protected Interaction(InputDevice sourceDevice)
{
if (sourceDevice == null)
{
throw new ArgumentNullException(nameof(sourceDevice), "Source device cannot be null");
}

this.sourceDevice = sourceDevice;
this.SourceDevice = sourceDevice ?? throw new ArgumentNullException(nameof(sourceDevice), "Source device cannot be null");
}

/// <summary>
/// Gets the device for which this action is intended.
/// </summary>
public InputDevice SourceDevice
{
get { return this.sourceDevice; }
}
public InputDevice SourceDevice { get; }

/// <summary>
/// Returns a value for this action that can be transmitted across the wire to a remote end.
Expand All @@ -65,7 +57,7 @@ public InputDevice SourceDevice
/// otherwise, <see langword="false"/>.</returns>
public virtual bool IsValidFor(InputDeviceKind sourceDeviceKind)
{
return this.sourceDevice.DeviceKind == sourceDeviceKind;
return this.SourceDevice.DeviceKind == sourceDeviceKind;
}
}
}
4 changes: 3 additions & 1 deletion dotnet/src/webdriver/Interactions/PauseInteraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
using System.Collections.Generic;
using System.Globalization;

#nullable enable

namespace OpenQA.Selenium.Interactions
{
/// <summary>
/// Represents a pause action.
/// </summary>
internal class PauseInteraction : Interaction
{
private TimeSpan duration = TimeSpan.Zero;
private readonly TimeSpan duration = TimeSpan.Zero;

/// <summary>
/// Initializes a new instance of the <see cref="PauseInteraction"/> class.
Expand Down
3 changes: 2 additions & 1 deletion dotnet/src/webdriver/WebElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ public class WebElement : IWebElement, IFindsElement, IWrapsDriver, ILocatable,
/// </summary>
/// <param name="parentDriver">The <see cref="WebDriver"/> instance that is driving this element.</param>
/// <param name="id">The ID value provided to identify the element.</param>
/// <exception cref="ArgumentNullException">If <paramref name="id"/> is <see langword="null"/>.</exception>
public WebElement(WebDriver parentDriver, string id)
{
this.driver = parentDriver;
this.elementId = id;
this.elementId = id ?? throw new ArgumentNullException(nameof(id));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

id == null is unreachable here outside of users directly creating one. Adding this check here to enforce IWebDriverObjectReference.ObjectReferenceId never returning null.

}

/// <summary>
Expand Down
Loading