Skip to content

Commit 56ff6af

Browse files
RenderMichaelgryznar
authored andcommitted
[dotnet] Annotate nullability on JavaScriptEngine and related types (SeleniumHQ#15218)
* [dotnet] Fix `JavaScriptEngine.ScriptCallbackBindings` not containing new bindings * Annotate nullability on `JavaScriptEngine` * remove unnecessary changes to `InitializationScript` * fix build
1 parent ff2a0c8 commit 56ff6af

8 files changed

+146
-116
lines changed

dotnet/src/webdriver/DomMutatedEventArgs.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ namespace OpenQA.Selenium
2626
/// </summary>
2727
public class DomMutatedEventArgs : EventArgs
2828
{
29-
private DomMutationData attributeData;
29+
internal DomMutatedEventArgs(DomMutationData attributeData)
30+
{
31+
AttributeData = attributeData;
32+
}
3033

3134
/// <summary>
3235
/// Gets the data about the attribute being changed.
3336
/// </summary>
34-
public DomMutationData AttributeData
35-
{
36-
get { return this.attributeData; }
37-
internal set { this.attributeData = value; }
38-
}
37+
public DomMutationData AttributeData { get; }
3938
}
4039
}

dotnet/src/webdriver/DomMutationData.cs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,58 +26,38 @@ namespace OpenQA.Selenium
2626
/// </summary>
2727
public class DomMutationData
2828
{
29-
private string targetId;
30-
private string attributeName;
31-
private string attributeValue;
32-
private string attributeOriginalValue;
33-
3429
/// <summary>
3530
/// Gets the ID of the element whose value is changing.
3631
/// </summary>
3732
[JsonPropertyName("target")]
3833
[JsonInclude]
39-
public string TargetId
40-
{
41-
get { return this.targetId; }
42-
internal set { this.targetId = value; }
43-
}
34+
public string TargetId { get; internal set; }
4435

4536
/// <summary>
4637
/// Gets the name of the attribute that is changing.
4738
/// </summary>
4839
[JsonPropertyName("name")]
4940
[JsonInclude]
50-
public string AttributeName
51-
{
52-
get { return this.attributeName; }
53-
internal set { this.attributeName = value; }
54-
}
41+
public string AttributeName { get; internal set; }
5542

5643
/// <summary>
5744
/// Gets the value to which the attribute is being changed.
5845
/// </summary>
5946
[JsonPropertyName("value")]
6047
[JsonInclude]
61-
public string AttributeValue
62-
{
63-
get { return this.attributeValue; }
64-
internal set { this.attributeValue = value; }
65-
}
48+
public string AttributeValue { get; internal set; }
6649

6750
/// <summary>
6851
/// Gets the value from which the attribute has been changed.
6952
/// </summary>
7053
[JsonPropertyName("oldValue")]
7154
[JsonInclude]
72-
public string AttributeOriginalValue
73-
{
74-
get { return this.attributeOriginalValue; }
75-
internal set { this.attributeOriginalValue = value; }
76-
}
55+
public string AttributeOriginalValue { get; internal set; }
7756

7857
/// <summary>
7958
/// Stores the element associated with the target ID
8059
/// </summary>
60+
[JsonIgnore]
8161
public IWebElement Element { get; internal set; }
8262

8363
/// <summary>
@@ -86,7 +66,7 @@ public string AttributeOriginalValue
8666
/// <returns>A string that represents the current object.</returns>
8767
public override string ToString()
8868
{
89-
return string.Format("target: {0}, name: {1}, value: {2}, originalValue: {3}", this.targetId, this.attributeName, this.attributeValue, this.attributeOriginalValue);
69+
return string.Format("target: {0}, name: {1}, value: {2}, originalValue: {3}", this.TargetId, this.AttributeName, this.AttributeValue, this.AttributeOriginalValue);
9070
}
9171
}
9272
}

dotnet/src/webdriver/IJavaScriptEngine.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using System.Collections.Generic;
2222
using System.Threading.Tasks;
2323

24+
#nullable enable
25+
2426
namespace OpenQA.Selenium
2527
{
2628
/// <summary>
@@ -31,22 +33,22 @@ public interface IJavaScriptEngine : IDisposable
3133
/// <summary>
3234
/// Occurs when a JavaScript callback with a named binding is executed.
3335
/// </summary>
34-
event EventHandler<JavaScriptCallbackExecutedEventArgs> JavaScriptCallbackExecuted;
36+
event EventHandler<JavaScriptCallbackExecutedEventArgs>? JavaScriptCallbackExecuted;
3537

3638
/// <summary>
37-
/// Occurs when an exeception is thrown by JavaScript being executed in the browser.
39+
/// Occurs when an exception is thrown by JavaScript being executed in the browser.
3840
/// </summary>
39-
event EventHandler<JavaScriptExceptionThrownEventArgs> JavaScriptExceptionThrown;
41+
event EventHandler<JavaScriptExceptionThrownEventArgs>? JavaScriptExceptionThrown;
4042

4143
/// <summary>
4244
/// Occurs when methods on the JavaScript console are called.
4345
/// </summary>
44-
event EventHandler<JavaScriptConsoleApiCalledEventArgs> JavaScriptConsoleApiCalled;
46+
event EventHandler<JavaScriptConsoleApiCalledEventArgs>? JavaScriptConsoleApiCalled;
4547

4648
/// <summary>
4749
/// Occurs when a value of an attribute in an element is being changed.
4850
/// </summary>
49-
event EventHandler<DomMutatedEventArgs> DomMutated;
51+
event EventHandler<DomMutatedEventArgs>? DomMutated;
5052

5153
/// <summary>
5254
/// Gets the read-only list of initialization scripts added for this JavaScript engine.
@@ -87,7 +89,7 @@ public interface IJavaScriptEngine : IDisposable
8789
/// <param name="scriptName">The friendly name by which to refer to this initialization script.</param>
8890
/// <param name="script">The JavaScript to be loaded on every page.</param>
8991
/// <returns>A task containing an <see cref="InitializationScript"/> object representing the script to be loaded on each page.</returns>
90-
/// <exception cref="ArgumentNullException">If <paramref name="scriptName"/> is <see langword="null"/>.</exception>
92+
/// <exception cref="ArgumentNullException">If <paramref name="scriptName"/> or <paramref name="script"/> are <see langword="null"/>.</exception>
9193
Task<InitializationScript> AddInitializationScript(string scriptName, string script);
9294

9395
/// <summary>
@@ -99,7 +101,7 @@ public interface IJavaScriptEngine : IDisposable
99101
Task RemoveInitializationScript(string scriptName);
100102

101103
/// <summary>
102-
/// Asynchronously removes all intialization scripts from being
104+
/// Asynchronously removes all initialization scripts from being
103105
/// loaded on every document load.
104106
/// </summary>
105107
/// <returns>A task that represents the asynchronous operation.</returns>
@@ -129,13 +131,16 @@ public interface IJavaScriptEngine : IDisposable
129131
/// </summary>
130132
/// <param name="bindingName">The name of the callback that will trigger events when called by JavaScript executing in the browser.</param>
131133
/// <returns>A task that represents the asynchronous operation.</returns>
134+
/// <exception cref="ArgumentNullException">If <paramref name="bindingName"/> is <see langword="null"/>.</exception>
135+
/// <exception cref="ArgumentException">If A binding with the specified name already exists.</exception>
132136
Task AddScriptCallbackBinding(string bindingName);
133137

134138
/// <summary>
135139
/// Asynchronously removes a binding to a JavaScript callback.
136140
/// </summary>
137141
/// <param name="bindingName">The name of the callback to be removed.</param>
138142
/// <returns>A task that represents the asynchronous operation.</returns>
143+
/// <exception cref="ArgumentNullException">If <paramref name="bindingName"/> is <see langword="null"/>.</exception>
139144
Task RemoveScriptCallbackBinding(string bindingName);
140145

141146
/// <summary>

dotnet/src/webdriver/InitializationScript.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,46 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using System;
2021
using System.Globalization;
2122

23+
#nullable enable
24+
2225
namespace OpenQA.Selenium
2326
{
2427
/// <summary>
2528
/// Represents a JavaScript script that is loaded and run on every document load.
2629
/// </summary>
2730
public class InitializationScript
2831
{
32+
internal InitializationScript(string scriptId, string scriptName, string scriptSource)
33+
{
34+
this.ScriptId = scriptId ?? throw new ArgumentNullException(nameof(scriptId));
35+
this.ScriptName = scriptName ?? throw new ArgumentNullException(nameof(scriptName));
36+
this.ScriptSource = scriptSource ?? throw new ArgumentNullException(nameof(scriptSource));
37+
}
38+
2939
/// <summary>
3040
/// Gets the internal ID of the initialization script.
3141
/// </summary>
32-
public string ScriptId { get; internal set; }
42+
public string ScriptId { get; }
3343

3444
/// <summary>
3545
/// Gets the friendly name of the initialization script.
3646
/// </summary>
37-
public string ScriptName { get; internal set; }
47+
public string ScriptName { get; }
3848

3949
/// <summary>
4050
/// Gets the JavaScript source of the initialization script.
4151
/// </summary>
42-
public string ScriptSource { get; internal set; }
52+
public string ScriptSource { get; }
4353

4454
/// <summary>
4555
/// Indicates whether the current <see cref="InitializationScript"/> is equal to another <see cref="InitializationScript"/> of the same type.
4656
/// </summary>
47-
/// <param name="other">An <see cref="InitializationScript"/> to compare with this <see cref="InitializationScript"/>.</param>
57+
/// <param name="obj">An <see cref="InitializationScript"/> to compare with this <see cref="InitializationScript"/>.</param>
4858
/// <returns><see langword="true"/> if the current <see cref="InitializationScript"/> is equal to the other parameter; otherwise, <see langword="false"/>.</returns>
49-
public override bool Equals(object obj)
59+
public override bool Equals(object? obj)
5060
{
5161
return obj is InitializationScript other && this.ScriptId == other.ScriptId && this.ScriptName == other.ScriptName && this.ScriptSource == other.ScriptSource;
5262
}

dotnet/src/webdriver/JavaScriptCallbackExecutedEventArgs.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,25 @@ namespace OpenQA.Selenium
2626
/// </summary>
2727
public class JavaScriptCallbackExecutedEventArgs : EventArgs
2828
{
29-
private string bindingName;
30-
private string payload;
31-
3229
/// <summary>
33-
/// Gets or sets the binding name of the JavaScript callback that was execute.
30+
/// Initializes a new instance of the <see cref="JavaScriptCallbackExecutedEventArgs"/> type.
3431
/// </summary>
35-
public string BindingName { get => bindingName; set => bindingName = value; }
32+
/// <param name="scriptPayload">The payload sent from the JavaScript callback.</param>
33+
/// <param name="bindingName">The binding name of the JavaScript callback that was execute.</param>
34+
public JavaScriptCallbackExecutedEventArgs(string scriptPayload, string bindingName)
35+
{
36+
this.ScriptPayload = scriptPayload;
37+
this.BindingName = bindingName;
38+
}
3639

3740
/// <summary>
3841
/// Gets or sets the payload sent from the JavaScript callback.
3942
/// </summary>
40-
public string ScriptPayload { get => payload; set => payload = value; }
43+
public string ScriptPayload { get; set; }
44+
45+
/// <summary>
46+
/// Gets or sets the binding name of the JavaScript callback that was execute.
47+
/// </summary>
48+
public string BindingName { get; set; }
4149
}
4250
}

dotnet/src/webdriver/JavaScriptConsoleApiCalledEventArgs.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,32 @@ namespace OpenQA.Selenium
2626
/// </summary>
2727
public class JavaScriptConsoleApiCalledEventArgs : EventArgs
2828
{
29-
private string messageContent;
30-
private DateTime messageTimeStamp;
31-
private string messageType;
29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="JavaScriptConsoleApiCalledEventArgs"/> type.
31+
/// </summary>
32+
/// <param name="messageContent">The content of the message written to the JavaScript console.</param>
33+
/// <param name="messageTimeStamp">The time stamp of the message written to the JavaScript console.</param>
34+
/// <param name="messageType">The type of message written to the JavaScript console.</param>
35+
public JavaScriptConsoleApiCalledEventArgs(string messageContent, DateTime messageTimeStamp, string messageType)
36+
{
37+
this.MessageContent = messageContent;
38+
this.MessageTimeStamp = messageTimeStamp;
39+
this.MessageType = messageType;
40+
}
3241

3342
/// <summary>
34-
/// Gets or sets the content of the message written to the JavaScript console
43+
/// Gets or sets the content of the message written to the JavaScript console.
3544
/// </summary>
36-
public string MessageContent { get => messageContent; set => messageContent = value; }
45+
public string MessageContent { get; set; }
3746

3847
/// <summary>
3948
/// Gets or sets the time stamp of the message written to the JavaScript console.
4049
/// </summary>
41-
public DateTime MessageTimeStamp { get => messageTimeStamp; set => messageTimeStamp = value; }
50+
public DateTime MessageTimeStamp { get; set; }
4251

4352
/// <summary>
4453
/// Gets or sets the type of message written to the JavaScript console.
4554
/// </summary>
46-
public string MessageType { get => messageType; set => messageType = value; }
55+
public string MessageType { get; set; }
4756
}
4857
}

0 commit comments

Comments
 (0)