From d1e7536872ccd27bf4d1b88fbf01c0d76ba184f7 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 25 Nov 2024 17:12:44 -0500 Subject: [PATCH 1/8] [dotnet] Annotate nullable reference types on input devices --- .../src/webdriver/Interactions/InputDevice.cs | 24 +-- .../webdriver/Interactions/KeyInputDevice.cs | 24 +-- .../Interactions/PointerInputDevice.cs | 174 ++++++------------ .../Interactions/WheelInputDevice.cs | 73 +++----- 4 files changed, 101 insertions(+), 194 deletions(-) diff --git a/dotnet/src/webdriver/Interactions/InputDevice.cs b/dotnet/src/webdriver/Interactions/InputDevice.cs index 6db6e30a7d8b7..5ff305d773638 100644 --- a/dotnet/src/webdriver/Interactions/InputDevice.cs +++ b/dotnet/src/webdriver/Interactions/InputDevice.cs @@ -21,6 +21,8 @@ using System.Collections.Generic; using System.Globalization; +#nullable enable + namespace OpenQA.Selenium.Interactions { /// @@ -28,12 +30,11 @@ namespace OpenQA.Selenium.Interactions /// public abstract class InputDevice { - private string deviceName; - /// /// Initializes a new instance of the class. /// /// The unique name of the input device represented by this class. + /// If is or . protected InputDevice(string deviceName) { if (string.IsNullOrEmpty(deviceName)) @@ -41,16 +42,13 @@ protected InputDevice(string deviceName) throw new ArgumentException("Device name must not be null or empty", nameof(deviceName)); } - this.deviceName = deviceName; + this.DeviceName = deviceName; } /// /// Gets the unique name of this input device. /// - public string DeviceName - { - get { return this.deviceName; } - } + public string DeviceName { get; } /// /// Gets the kind of device for this input device. @@ -67,10 +65,7 @@ public string DeviceName /// Creates a pause action for synchronization with other action sequences. /// /// The representing the action. - public Interaction CreatePause() - { - return this.CreatePause(TimeSpan.Zero); - } + public Interaction CreatePause() => this.CreatePause(TimeSpan.Zero); /// /// Creates a pause action for synchronization with other action sequences. @@ -88,10 +83,7 @@ public Interaction CreatePause(TimeSpan duration) /// Returns a hash code for the current . /// /// A hash code for the current . - public override int GetHashCode() - { - return this.deviceName.GetHashCode(); - } + public override int GetHashCode() => this.DeviceName.GetHashCode(); /// /// Returns a string that represents the current . @@ -99,7 +91,7 @@ public override int GetHashCode() /// A string that represents the current . public override string ToString() { - return string.Format(CultureInfo.InvariantCulture, "{0} input device [name: {1}]", this.DeviceKind, this.deviceName); + return string.Format(CultureInfo.InvariantCulture, "{0} input device [name: {1}]", this.DeviceKind, this.DeviceName); } } } diff --git a/dotnet/src/webdriver/Interactions/KeyInputDevice.cs b/dotnet/src/webdriver/Interactions/KeyInputDevice.cs index 4273a59b6c0c0..d3e9be3962df0 100644 --- a/dotnet/src/webdriver/Interactions/KeyInputDevice.cs +++ b/dotnet/src/webdriver/Interactions/KeyInputDevice.cs @@ -21,6 +21,8 @@ using System.Collections.Generic; using System.Globalization; +#nullable enable + namespace OpenQA.Selenium.Interactions { /// @@ -40,6 +42,7 @@ public KeyInputDevice() /// Initializes a new instance of the class, given the device's name. /// /// The unique name of this input device. + /// If is or . public KeyInputDevice(string deviceName) : base(deviceName) { @@ -48,10 +51,7 @@ public KeyInputDevice(string deviceName) /// /// Gets the type of device for this input device. /// - public override InputDeviceKind DeviceKind - { - get { return InputDeviceKind.Key; } - } + public override InputDeviceKind DeviceKind => InputDeviceKind.Key; /// /// Converts this input device into an object suitable for serializing across the wire. @@ -115,27 +115,23 @@ public override string ToString() private class TypingInteraction : Interaction { - private string type; - private string value; + private readonly string _type; public TypingInteraction(InputDevice sourceDevice, string type, char codePoint) : base(sourceDevice) { - this.type = type; - this.value = codePoint.ToString(); + this._type = type; + this.Value = codePoint.ToString(); } - protected string Value - { - get { return this.value; } - } + protected string Value { get; } public override Dictionary ToDictionary() { Dictionary toReturn = new Dictionary(); - toReturn["type"] = this.type; - toReturn["value"] = this.value; + toReturn["type"] = this._type; + toReturn["value"] = this.Value; return toReturn; } diff --git a/dotnet/src/webdriver/Interactions/PointerInputDevice.cs b/dotnet/src/webdriver/Interactions/PointerInputDevice.cs index ef57abe4e49cb..5979dbaa00f99 100644 --- a/dotnet/src/webdriver/Interactions/PointerInputDevice.cs +++ b/dotnet/src/webdriver/Interactions/PointerInputDevice.cs @@ -17,10 +17,12 @@ // under the License. // -using OpenQA.Selenium.Internal; using System; using System.Collections.Generic; using System.Globalization; +using OpenQA.Selenium.Internal; + +#nullable enable namespace OpenQA.Selenium.Interactions { @@ -107,7 +109,7 @@ public enum MouseButton /// public class PointerInputDevice : InputDevice { - private PointerKind pointerKind; + private readonly PointerKind pointerKind; /// /// Initializes a new instance of the class. @@ -131,6 +133,7 @@ public PointerInputDevice(PointerKind pointerKind) /// /// The kind of pointer represented by this input device. /// The unique name for this input device. + /// If is or . public PointerInputDevice(PointerKind pointerKind, string deviceName) : base(deviceName) { @@ -140,10 +143,7 @@ public PointerInputDevice(PointerKind pointerKind, string deviceName) /// /// Gets the type of device for this input device. /// - public override InputDeviceKind DeviceKind - { - get { return InputDeviceKind.Pointer; } - } + public override InputDeviceKind DeviceKind => InputDeviceKind.Pointer; /// /// Returns a value for this input device that can be transmitted across the wire to a remote end. @@ -265,7 +265,7 @@ public Interaction CreatePointerMove(CoordinateOrigin origin, int xOffset, int y /// The object containing additional proprties for this pointer move operation. /// The action representing the pointer move gesture. /// Thrown when passing CoordinateOrigin.Element into origin. - /// Users should us the other CreatePointerMove overload to move to a specific element. + /// Users should use the other CreatePointerMove overload to move to a specific element. public Interaction CreatePointerMove(CoordinateOrigin origin, int xOffset, int yOffset, TimeSpan duration, PointerEventProperties properties) { if (origin == CoordinateOrigin.Element) @@ -290,44 +290,24 @@ public Interaction CreatePointerCancel() /// public class PointerEventProperties { - private double? width; - private double? height; - private double? pressure; - private double? tangentialPressure; - private int? tiltX; - private int? tiltY; - private int? twist; - private double? altitudeAngle; - private double? azimuthAngle; - /// /// Gets or sets the width (magnitude on x-axis) in pixels of the contact geometry of the pointer. /// - public double? Width - { - get { return this.width; } - set { this.width = value; } - } + public double? Width { get; set; } /// /// Gets or sets the height (magnitude on y-axis) in pixels of the contact geometry of the pointer. /// - public double? Height - { - get { return this.height; } - set { this.height = value; } - } + public double? Height { get; set; } + /// /// Gets or sets the normalized pressure of the pointer input. /// /// /// 0 and 1 represent the minimum and maximum pressure the hardware is capable of detecting, respectively. /// - public double? Pressure - { - get { return this.pressure; } - set { this.pressure = value; } - } + public double? Pressure { get; set; } + /// /// Gets or sets the normalized tangential pressure (also known as barrel pressure) of the pointer input. /// @@ -335,11 +315,8 @@ public double? Pressure /// Valid values are between -1 and 1 with 0 being the neutral position of the control. /// Some hardware may only support positive values between 0 and 1. /// - public double? TangentialPressure - { - get { return this.tangentialPressure; } - set { this.tangentialPressure = value; } - } + public double? TangentialPressure { get; set; } + /// /// Gets or sets the plane angle in degrees between the Y-Z plane and the plane containing /// both the transducer (e.g. pen stylus) axis and the Y axis.. @@ -347,11 +324,8 @@ public double? TangentialPressure /// /// Valid values are between -90 and 90. A positive value is to the right. /// - public int? TiltX - { - get { return this.tiltX; } - set { this.tiltX = value; } - } + public int? TiltX { get; set; } + /// /// Gets or sets the plane angle in degrees between the X-Z plane and the plane containing /// both the transducer (e.g. pen stylus) axis and the X axis.. @@ -359,22 +333,16 @@ public int? TiltX /// /// Valid values are between -90 and 90. A positive value is toward the user. /// - public int? TiltY - { - get { return this.tiltY; } - set { this.tiltY = value; } - } + public int? TiltY { get; set; } + /// /// Gets or sets the clockwise rotation in degrees of a transducer (e.g. stylus) around its own major axis /// /// /// Valid values are between 0 and 359. /// - public int? Twist - { - get { return this.twist; } - set { this.twist = value; } - } + public int? Twist { get; set; } + /// /// Gets or sets the altitude in radians of the transducer (e.g. pen/stylus) /// @@ -382,11 +350,8 @@ public int? Twist /// Valid values are between 0 and π/2, where 0 is parallel to the surface (X-Y plane), /// and π/2 is perpendicular to the surface. /// - public double? AltitudeAngle - { - get { return this.altitudeAngle; } - set { this.altitudeAngle = value; } - } + public double? AltitudeAngle { get; set; } + /// /// Gets or sets the azimuth angle (in radians) of the transducer (e.g. pen/stylus) /// @@ -395,11 +360,7 @@ public double? AltitudeAngle /// where 0 represents a transducer whose cap is pointing in the direction of increasing X values, /// and the values progressively increase when going clockwise. /// - public double? AzimuthAngle - { - get { return this.azimuthAngle; } - set { this.azimuthAngle = value; } - } + public double? AzimuthAngle { get; set; } /// /// Serializes the properties of this input device as a dictionary. @@ -414,44 +375,44 @@ public Dictionary ToDictionary() toReturn["width"] = this.Width.Value; } - if (this.height.HasValue) + if (this.Height.HasValue) { - toReturn["height"] = this.height.Value; + toReturn["height"] = this.Height.Value; } - if (this.pressure.HasValue) + if (this.Pressure.HasValue) { - toReturn["pressure"] = this.pressure.Value; + toReturn["pressure"] = this.Pressure.Value; } - if (this.tangentialPressure.HasValue) + if (this.TangentialPressure.HasValue) { - toReturn["tangentialPressure"] = this.tangentialPressure.Value; + toReturn["tangentialPressure"] = this.TangentialPressure.Value; } - if (this.tiltX.HasValue) + if (this.TiltX.HasValue) { - toReturn["tiltX"] = this.tiltX.Value; + toReturn["tiltX"] = this.TiltX.Value; } - if (this.tiltY.HasValue) + if (this.TiltY.HasValue) { - toReturn["tiltY"] = this.tiltY.Value; + toReturn["tiltY"] = this.TiltY.Value; } - if (this.twist.HasValue) + if (this.Twist.HasValue) { - toReturn["twist"] = this.twist.Value; + toReturn["twist"] = this.Twist.Value; } - if (this.altitudeAngle.HasValue) + if (this.AltitudeAngle.HasValue) { - toReturn["altitudeAngle"] = this.altitudeAngle.Value; + toReturn["altitudeAngle"] = this.AltitudeAngle.Value; } - if (this.azimuthAngle.HasValue) + if (this.AzimuthAngle.HasValue) { - toReturn["azimuthAngle"] = this.azimuthAngle.Value; + toReturn["azimuthAngle"] = this.AzimuthAngle.Value; } return toReturn; @@ -460,8 +421,8 @@ public Dictionary ToDictionary() private class PointerDownInteraction : Interaction { - private MouseButton button; - private PointerEventProperties eventProperties; + private readonly MouseButton button; + private readonly PointerEventProperties eventProperties; public PointerDownInteraction(InputDevice sourceDevice, MouseButton button, PointerEventProperties properties) : base(sourceDevice) @@ -473,7 +434,7 @@ public PointerDownInteraction(InputDevice sourceDevice, MouseButton button, Poin public override Dictionary ToDictionary() { Dictionary toReturn; - if (eventProperties == null) + if (eventProperties is null) { toReturn = new Dictionary(); } @@ -487,16 +448,13 @@ public override Dictionary ToDictionary() return toReturn; } - public override string ToString() - { - return "Pointer down"; - } + public override string ToString() => "Pointer down"; } private class PointerUpInteraction : Interaction { - private MouseButton button; - private PointerEventProperties eventProperties; + private readonly MouseButton button; + private readonly PointerEventProperties eventProperties; public PointerUpInteraction(InputDevice sourceDevice, MouseButton button, PointerEventProperties properties) : base(sourceDevice) @@ -508,7 +466,7 @@ public PointerUpInteraction(InputDevice sourceDevice, MouseButton button, Pointe public override Dictionary ToDictionary() { Dictionary toReturn; - if (eventProperties == null) + if (eventProperties is null) { toReturn = new Dictionary(); } @@ -523,10 +481,7 @@ public override Dictionary ToDictionary() return toReturn; } - public override string ToString() - { - return "Pointer up"; - } + public override string ToString() => "Pointer up"; } private class PointerCancelInteraction : Interaction @@ -543,22 +498,19 @@ public override Dictionary ToDictionary() return toReturn; } - public override string ToString() - { - return "Pointer cancel"; - } + public override string ToString() => "Pointer cancel"; } private class PointerMoveInteraction : Interaction { - private IWebElement target; - private int x = 0; - private int y = 0; + private readonly IWebElement? target; + private readonly int x = 0; + private readonly int y = 0; private TimeSpan duration = TimeSpan.MinValue; - private CoordinateOrigin origin = CoordinateOrigin.Pointer; - private PointerEventProperties eventProperties; + private readonly CoordinateOrigin origin = CoordinateOrigin.Pointer; + private readonly PointerEventProperties eventProperties; - public PointerMoveInteraction(InputDevice sourceDevice, IWebElement target, CoordinateOrigin origin, int x, int y, TimeSpan duration, PointerEventProperties properties) + public PointerMoveInteraction(InputDevice sourceDevice, IWebElement? target, CoordinateOrigin origin, int x, int y, TimeSpan duration, PointerEventProperties properties) : base(sourceDevice) { if (target != null) @@ -622,7 +574,7 @@ public override string ToString() string originDescription = this.origin.ToString(); if (this.origin == CoordinateOrigin.Element) { - originDescription = this.target.ToString(); + originDescription = this.target!.ToString()!; } return string.Format(CultureInfo.InvariantCulture, "Pointer move [origin: {0}, x offset: {1}, y offset: {2}, duration: {3}ms]", originDescription, this.x, this.y, this.duration.TotalMilliseconds); @@ -630,23 +582,17 @@ public override string ToString() private Dictionary ConvertElement() { - IWebDriverObjectReference elementReference = this.target as IWebDriverObjectReference; - if (elementReference == null) + if (this.target is IWebDriverObjectReference element) { - IWrapsElement elementWrapper = this.target as IWrapsElement; - if (elementWrapper != null) - { - elementReference = elementWrapper.WrappedElement as IWebDriverObjectReference; - } + return element.ToDictionary(); } - if (elementReference == null) + if (this.target is IWrapsElement { WrappedElement: IWebDriverObjectReference wrappedElement }) { - throw new ArgumentException("Target element cannot be converted to IWebElementReference"); + return wrappedElement.ToDictionary(); } - Dictionary elementDictionary = elementReference.ToDictionary(); - return elementDictionary; + throw new ArgumentException("Target element cannot be converted to IWebElementReference"); } } } diff --git a/dotnet/src/webdriver/Interactions/WheelInputDevice.cs b/dotnet/src/webdriver/Interactions/WheelInputDevice.cs index 2016d229a08c2..39ac416121b78 100644 --- a/dotnet/src/webdriver/Interactions/WheelInputDevice.cs +++ b/dotnet/src/webdriver/Interactions/WheelInputDevice.cs @@ -17,9 +17,11 @@ // under the License. // -using OpenQA.Selenium.Internal; using System; using System.Collections.Generic; +using OpenQA.Selenium.Internal; + +#nullable enable namespace OpenQA.Selenium.Interactions { @@ -40,6 +42,7 @@ public WheelInputDevice() /// Initializes a new instance of the class, given the device's name. /// /// The unique name of this input device. + /// If is or . public WheelInputDevice(string deviceName) : base(deviceName) { @@ -48,10 +51,7 @@ public WheelInputDevice(string deviceName) /// /// Gets the type of device for this input device. /// - public override InputDeviceKind DeviceKind - { - get { return InputDeviceKind.Wheel; } - } + public override InputDeviceKind DeviceKind => InputDeviceKind.Wheel; /// /// Returns a value for this input device that can be transmitted across the wire to a remote end. @@ -114,60 +114,39 @@ public Interaction CreateWheelScroll(CoordinateOrigin origin, int xOffset, int y /// public class ScrollOrigin { - private IWebElement element; - private bool viewport; - private int xOffset = 0; - private int yOffset = 0; - /// /// Gets or sets the element for the scroll origin. /// - public IWebElement Element - { - get { return this.element; } - set { this.element = value; } - } + public IWebElement? Element { get; set; } /// /// Gets or sets a value indicating whether the viewport should be used as the origin. /// - public bool Viewport - { - get { return this.viewport; } - set { this.viewport = value; } - } + public bool Viewport { get; set; } /// /// Gets or sets the horizontal offset of the scroll origin. /// - public int XOffset - { - get { return this.xOffset; } - set { this.xOffset = value; } - } + public int XOffset { get; set; } = 0; /// /// Gets or sets the vertical offset of the scroll origin. /// - public int YOffset - { - get { return this.yOffset; } - set { this.yOffset = value; } - } + public int YOffset { get; set; } = 0; } private class WheelScrollInteraction : Interaction { - private IWebElement target; - private int x = 0; - private int y = 0; - private int deltaX = 0; - private int deltaY = 0; - private TimeSpan duration = TimeSpan.MinValue; - private CoordinateOrigin origin = CoordinateOrigin.Viewport; - - public WheelScrollInteraction(InputDevice sourceDevice, IWebElement target, CoordinateOrigin origin, int x, int y, int deltaX, int deltaY, TimeSpan duration) + private readonly IWebElement? target; + private readonly int x = 0; + private readonly int y = 0; + private readonly int deltaX = 0; + private readonly int deltaY = 0; + private readonly TimeSpan duration = TimeSpan.MinValue; + private readonly CoordinateOrigin origin = CoordinateOrigin.Viewport; + + public WheelScrollInteraction(InputDevice sourceDevice, IWebElement? target, CoordinateOrigin origin, int x, int y, int deltaX, int deltaY, TimeSpan duration) : base(sourceDevice) { if (target != null) @@ -224,23 +203,17 @@ public override Dictionary ToDictionary() private Dictionary ConvertElement() { - IWebDriverObjectReference elementReference = this.target as IWebDriverObjectReference; - if (elementReference == null) + if (this.target is IWebDriverObjectReference element) { - IWrapsElement elementWrapper = this.target as IWrapsElement; - if (elementWrapper != null) - { - elementReference = elementWrapper.WrappedElement as IWebDriverObjectReference; - } + return element.ToDictionary(); } - if (elementReference == null) + if (this.target is IWrapsElement { WrappedElement: IWebDriverObjectReference wrappedElement }) { - throw new ArgumentException("Target element cannot be converted to IWebElementReference"); + return wrappedElement.ToDictionary(); } - Dictionary elementDictionary = elementReference.ToDictionary(); - return elementDictionary; + throw new ArgumentException("Target element cannot be converted to IWebElementReference"); } } } From 28724604e1abd62aeb24fce8e424433948cf9a81 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 25 Nov 2024 17:14:42 -0500 Subject: [PATCH 2/8] remove unnecessary `_` --- dotnet/src/webdriver/Interactions/KeyInputDevice.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dotnet/src/webdriver/Interactions/KeyInputDevice.cs b/dotnet/src/webdriver/Interactions/KeyInputDevice.cs index d3e9be3962df0..d04a05e3866b4 100644 --- a/dotnet/src/webdriver/Interactions/KeyInputDevice.cs +++ b/dotnet/src/webdriver/Interactions/KeyInputDevice.cs @@ -115,12 +115,12 @@ public override string ToString() private class TypingInteraction : Interaction { - private readonly string _type; + private readonly string type; public TypingInteraction(InputDevice sourceDevice, string type, char codePoint) : base(sourceDevice) { - this._type = type; + this.type = type; this.Value = codePoint.ToString(); } @@ -130,7 +130,7 @@ public override Dictionary ToDictionary() { Dictionary toReturn = new Dictionary(); - toReturn["type"] = this._type; + toReturn["type"] = this.type; toReturn["value"] = this.Value; return toReturn; From 8f7180db8feba3493718a44508ede5e332d7401f Mon Sep 17 00:00:00 2001 From: Michael Render Date: Sun, 1 Dec 2024 17:06:28 -0500 Subject: [PATCH 3/8] Use block-body methods in `InputDevice` --- dotnet/src/webdriver/Interactions/InputDevice.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/Interactions/InputDevice.cs b/dotnet/src/webdriver/Interactions/InputDevice.cs index 5ff305d773638..f73c0578e2586 100644 --- a/dotnet/src/webdriver/Interactions/InputDevice.cs +++ b/dotnet/src/webdriver/Interactions/InputDevice.cs @@ -65,7 +65,10 @@ protected InputDevice(string deviceName) /// Creates a pause action for synchronization with other action sequences. /// /// The representing the action. - public Interaction CreatePause() => this.CreatePause(TimeSpan.Zero); + public Interaction CreatePause() + { + return this.CreatePause(TimeSpan.Zero); + } /// /// Creates a pause action for synchronization with other action sequences. @@ -83,7 +86,10 @@ public Interaction CreatePause(TimeSpan duration) /// Returns a hash code for the current . /// /// A hash code for the current . - public override int GetHashCode() => this.DeviceName.GetHashCode(); + public override int GetHashCode() + { + return this.DeviceName.GetHashCode(); + } /// /// Returns a string that represents the current . From 2432a515970ef8f913943f01765afa131b4ae4f8 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 2 Dec 2024 13:11:29 -0500 Subject: [PATCH 4/8] Revert `WheelInputDevice.ConvertElement` and `PointerInputDevice.ConvertElement` --- .../Interactions/PointerInputDevice.cs | 18 ++++++++++++------ .../webdriver/Interactions/WheelInputDevice.cs | 18 ++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/dotnet/src/webdriver/Interactions/PointerInputDevice.cs b/dotnet/src/webdriver/Interactions/PointerInputDevice.cs index 5979dbaa00f99..b8d48b25f3251 100644 --- a/dotnet/src/webdriver/Interactions/PointerInputDevice.cs +++ b/dotnet/src/webdriver/Interactions/PointerInputDevice.cs @@ -17,10 +17,10 @@ // under the License. // +using OpenQA.Selenium.Internal; using System; using System.Collections.Generic; using System.Globalization; -using OpenQA.Selenium.Internal; #nullable enable @@ -582,17 +582,23 @@ public override string ToString() private Dictionary ConvertElement() { - if (this.target is IWebDriverObjectReference element) + IWebDriverObjectReference? elementReference = this.target as IWebDriverObjectReference; + if (elementReference == null) { - return element.ToDictionary(); + IWrapsElement? elementWrapper = this.target as IWrapsElement; + if (elementWrapper != null) + { + elementReference = elementWrapper.WrappedElement as IWebDriverObjectReference; + } } - if (this.target is IWrapsElement { WrappedElement: IWebDriverObjectReference wrappedElement }) + if (elementReference == null) { - return wrappedElement.ToDictionary(); + throw new ArgumentException("Target element cannot be converted to IWebElementReference"); } - throw new ArgumentException("Target element cannot be converted to IWebElementReference"); + Dictionary elementDictionary = elementReference.ToDictionary(); + return elementDictionary; } } } diff --git a/dotnet/src/webdriver/Interactions/WheelInputDevice.cs b/dotnet/src/webdriver/Interactions/WheelInputDevice.cs index 39ac416121b78..c3e049a290ee0 100644 --- a/dotnet/src/webdriver/Interactions/WheelInputDevice.cs +++ b/dotnet/src/webdriver/Interactions/WheelInputDevice.cs @@ -17,9 +17,9 @@ // under the License. // +using OpenQA.Selenium.Internal; using System; using System.Collections.Generic; -using OpenQA.Selenium.Internal; #nullable enable @@ -203,17 +203,23 @@ public override Dictionary ToDictionary() private Dictionary ConvertElement() { - if (this.target is IWebDriverObjectReference element) + IWebDriverObjectReference? elementReference = this.target as IWebDriverObjectReference; + if (elementReference == null) { - return element.ToDictionary(); + IWrapsElement? elementWrapper = this.target as IWrapsElement; + if (elementWrapper != null) + { + elementReference = elementWrapper.WrappedElement as IWebDriverObjectReference; + } } - if (this.target is IWrapsElement { WrappedElement: IWebDriverObjectReference wrappedElement }) + if (elementReference == null) { - return wrappedElement.ToDictionary(); + throw new ArgumentException("Target element cannot be converted to IWebElementReference"); } - throw new ArgumentException("Target element cannot be converted to IWebElementReference"); + Dictionary elementDictionary = elementReference.ToDictionary(); + return elementDictionary; } } } From fef6ec6875dd38b2dbbd96b7ed27780e00abcd78 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 2 Dec 2024 13:33:43 -0500 Subject: [PATCH 5/8] Make methods always body-blocked --- .../webdriver/Interactions/PointerInputDevice.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/dotnet/src/webdriver/Interactions/PointerInputDevice.cs b/dotnet/src/webdriver/Interactions/PointerInputDevice.cs index b8d48b25f3251..fc66550d24df3 100644 --- a/dotnet/src/webdriver/Interactions/PointerInputDevice.cs +++ b/dotnet/src/webdriver/Interactions/PointerInputDevice.cs @@ -448,7 +448,10 @@ public override Dictionary ToDictionary() return toReturn; } - public override string ToString() => "Pointer down"; + public override string ToString() + { + return "Pointer down"; + } } private class PointerUpInteraction : Interaction @@ -481,7 +484,10 @@ public override Dictionary ToDictionary() return toReturn; } - public override string ToString() => "Pointer up"; + public override string ToString() + { + return "Pointer up"; + } } private class PointerCancelInteraction : Interaction @@ -498,7 +504,10 @@ public override Dictionary ToDictionary() return toReturn; } - public override string ToString() => "Pointer cancel"; + public override string ToString() + { + return "Pointer cancel"; + } } private class PointerMoveInteraction : Interaction From ad4966713f87f82f9ddcd2aa2d254957c0fc72f4 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 2 Dec 2024 15:29:20 -0500 Subject: [PATCH 6/8] Add null check to `CreatePointerMove` --- dotnet/src/webdriver/Interactions/PointerInputDevice.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dotnet/src/webdriver/Interactions/PointerInputDevice.cs b/dotnet/src/webdriver/Interactions/PointerInputDevice.cs index fc66550d24df3..4b697b6ecc09b 100644 --- a/dotnet/src/webdriver/Interactions/PointerInputDevice.cs +++ b/dotnet/src/webdriver/Interactions/PointerInputDevice.cs @@ -219,6 +219,7 @@ public Interaction CreatePointerUp(MouseButton button, PointerEventProperties pr /// The vertical offset from the origin of the move. /// The length of time the move gesture takes to complete. /// The action representing the pointer move gesture. + /// If is . public Interaction CreatePointerMove(IWebElement target, int xOffset, int yOffset, TimeSpan duration) { return CreatePointerMove(target, xOffset, yOffset, duration, new PointerEventProperties()); @@ -233,8 +234,14 @@ public Interaction CreatePointerMove(IWebElement target, int xOffset, int yOffse /// The length of time the move gesture takes to complete. /// The specifications for the pointer event interaction /// The action representing the pointer move gesture. + /// If is . public Interaction CreatePointerMove(IWebElement target, int xOffset, int yOffset, TimeSpan duration, PointerEventProperties properties) { + if (target is null) + { + throw new ArgumentNullException(nameof(target)); + } + return new PointerMoveInteraction(this, target, CoordinateOrigin.Element, xOffset, yOffset, duration, properties); } From 23cf0af4c50eb65cda05976bdbb0dd393c56d2b3 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 2 Dec 2024 15:38:05 -0500 Subject: [PATCH 7/8] Add null check to `CreateWheelScroll` --- dotnet/src/webdriver/Interactions/WheelInputDevice.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dotnet/src/webdriver/Interactions/WheelInputDevice.cs b/dotnet/src/webdriver/Interactions/WheelInputDevice.cs index c3e049a290ee0..8d690ba4c7db6 100644 --- a/dotnet/src/webdriver/Interactions/WheelInputDevice.cs +++ b/dotnet/src/webdriver/Interactions/WheelInputDevice.cs @@ -89,8 +89,14 @@ public Interaction CreateWheelScroll(int deltaX, int deltaY, TimeSpan duration) /// The distance along the Y axis to scroll using the wheel. /// The duration of the scroll action. /// The representing the wheel scroll. + /// If is . public Interaction CreateWheelScroll(IWebElement target, int xOffset, int yOffset, int deltaX, int deltaY, TimeSpan duration) { + if (target is null) + { + throw new ArgumentNullException(nameof(target)); + } + return new WheelScrollInteraction(this, target, CoordinateOrigin.Element, xOffset, yOffset, deltaX, deltaY, duration); } From e244523ec15e862417e6cfc8f520bc9794e0daee Mon Sep 17 00:00:00 2001 From: Michael Render Date: Sat, 7 Dec 2024 22:41:03 -0500 Subject: [PATCH 8/8] Do not demand for `PointerMoveInteraction.target` to be not-null --- dotnet/src/webdriver/Interactions/PointerInputDevice.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/dotnet/src/webdriver/Interactions/PointerInputDevice.cs b/dotnet/src/webdriver/Interactions/PointerInputDevice.cs index 4b697b6ecc09b..ca92a7136b378 100644 --- a/dotnet/src/webdriver/Interactions/PointerInputDevice.cs +++ b/dotnet/src/webdriver/Interactions/PointerInputDevice.cs @@ -237,11 +237,6 @@ public Interaction CreatePointerMove(IWebElement target, int xOffset, int yOffse /// If is . public Interaction CreatePointerMove(IWebElement target, int xOffset, int yOffset, TimeSpan duration, PointerEventProperties properties) { - if (target is null) - { - throw new ArgumentNullException(nameof(target)); - } - return new PointerMoveInteraction(this, target, CoordinateOrigin.Element, xOffset, yOffset, duration, properties); } @@ -590,7 +585,7 @@ public override string ToString() string originDescription = this.origin.ToString(); if (this.origin == CoordinateOrigin.Element) { - originDescription = this.target!.ToString()!; + originDescription = this.target?.ToString() ?? ""; } return string.Format(CultureInfo.InvariantCulture, "Pointer move [origin: {0}, x offset: {1}, y offset: {2}, duration: {3}ms]", originDescription, this.x, this.y, this.duration.TotalMilliseconds);