Skip to content

Commit 5a04a82

Browse files
chrisblockjimevans
authored andcommitted
Enhance check for value types to exclude Nullable
The System.Nullable type is a value type that can be assigned the null value. This change checks for types wrapped with Nullable and does not throw when they are encountered. Fixes #1316 Signed-off-by: Jim Evans <james.h.evans.jr@gmail.com>
1 parent 076e763 commit 5a04a82

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

dotnet/src/support/Extensions/WebDriverExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static T ExecuteJavaScript<T>(this IWebDriver driver, string script, para
9191
object value = executor.ExecuteScript(script, args);
9292
if (value == null)
9393
{
94-
if (type.IsValueType)
94+
if (type.IsValueType && (Nullable.GetUnderlyingType(type) == null))
9595
{
9696
throw new WebDriverException("Script returned null, but desired type is a value type");
9797
}

dotnet/test/support/Extensions/ExecuteJavaScriptTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ public void ShouldNotThrowWhenNullIsReturned()
112112
Assert.That(() => driver.ExecuteJavaScript<string>(JavaScript, JavaScriptParameters), Throws.Nothing);
113113
}
114114

115+
[Test]
116+
public void ShouldNotThrowWhenNullIsReturnedForNullableValueType()
117+
{
118+
Expect.Once.On(driver)
119+
.Method("ExecuteScript")
120+
.With(JavaScript, JavaScriptParameters)
121+
.Will(Return.Value(null));
122+
123+
Assert.That(() => driver.ExecuteJavaScript<int?>(JavaScript, JavaScriptParameters), Throws.Nothing);
124+
}
125+
115126
[Test]
116127
public void ShouldThrowWhenNullIsReturnedForValueType()
117128
{

0 commit comments

Comments
 (0)