Skip to content

Commit 833112c

Browse files
[dotnet] Add C# 14 extension to polyfill ArgumentNullException.ThrowIfNull (#16697)
Co-authored-by: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com>
1 parent 83b8e82 commit 833112c

File tree

86 files changed

+353
-397
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+353
-397
lines changed

dotnet/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ pkg_zip(
3232
filegroup(
3333
name = "source_files_support_needs_from_core",
3434
srcs = [
35+
"//dotnet/src/webdriver:Properties/ArgumentNullExceptionExtensions.cs",
36+
"//dotnet/src/webdriver:Properties/CallerArgumentExpressionAttribute.cs",
37+
"//dotnet/src/webdriver:Properties/NullableAttributes.cs",
3538
"//dotnet/src/webdriver:Properties/StringSyntaxAttribute.cs",
3639
"//dotnet/src/webdriver:Properties/StringSyntaxConstants.cs",
3740
],

dotnet/src/support/Events/EventFiringWebDriver.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public class EventFiringWebDriver : IWebDriver, IJavaScriptExecutor, ITakesScree
3636
/// <exception cref="ArgumentNullException">If <paramref name="parentDriver"/> is <see langword="null"/>.</exception>
3737
public EventFiringWebDriver(IWebDriver parentDriver)
3838
{
39-
this.WrappedDriver = parentDriver ?? throw new ArgumentNullException(nameof(parentDriver));
39+
ArgumentNullException.ThrowIfNull(parentDriver);
40+
this.WrappedDriver = parentDriver;
4041
}
4142

4243
/// <summary>
@@ -537,10 +538,7 @@ protected virtual async ValueTask DisposeAsyncCore()
537538
/// </remarks>
538539
public object? ExecuteScript(PinnedScript script, params object?[] args)
539540
{
540-
if (script == null)
541-
{
542-
throw new ArgumentNullException(nameof(script));
543-
}
541+
ArgumentNullException.ThrowIfNull(script);
544542

545543
if (this.WrappedDriver is not IJavaScriptExecutor javascriptDriver)
546544
{
@@ -808,7 +806,8 @@ private class EventFiringNavigation : INavigation
808806
/// <param name="driver">Driver in use</param>
809807
public EventFiringNavigation(EventFiringWebDriver driver)
810808
{
811-
this.parentDriver = driver ?? throw new ArgumentNullException(nameof(driver));
809+
ArgumentNullException.ThrowIfNull(driver);
810+
this.parentDriver = driver;
812811
this.wrappedNavigation = this.parentDriver.WrappedDriver.Navigate();
813812
}
814813

@@ -1025,7 +1024,8 @@ private class EventFiringTargetLocator : ITargetLocator
10251024
/// <param name="driver">The driver that is currently in use</param>
10261025
public EventFiringTargetLocator(EventFiringWebDriver driver)
10271026
{
1028-
this.parentDriver = driver ?? throw new ArgumentNullException(nameof(driver));
1027+
ArgumentNullException.ThrowIfNull(driver);
1028+
this.parentDriver = driver;
10291029
this.wrappedLocator = this.parentDriver.WrappedDriver.SwitchTo();
10301030
}
10311031

@@ -1297,8 +1297,10 @@ private class EventFiringWebElement : ITakesScreenshot, IWebElement, IWrapsEleme
12971297
/// <param name="element">The <see cref="IWebElement"/> to wrap for event firing.</param>
12981298
public EventFiringWebElement(EventFiringWebDriver driver, IWebElement element)
12991299
{
1300-
this.WrappedElement = element ?? throw new ArgumentNullException(nameof(element));
1301-
this.parentDriver = driver ?? throw new ArgumentNullException(nameof(driver));
1300+
ArgumentNullException.ThrowIfNull(element);
1301+
ArgumentNullException.ThrowIfNull(driver);
1302+
this.WrappedElement = element;
1303+
this.parentDriver = driver;
13021304
}
13031305

13041306
/// <summary>
@@ -1773,7 +1775,8 @@ private class EventFiringShadowRoot : ISearchContext, IWrapsDriver, IEquatable<I
17731775
/// <param name="searchContext">The <see cref="ISearchContext"/> to wrap for event firing.</param>
17741776
public EventFiringShadowRoot(EventFiringWebDriver driver, ISearchContext searchContext)
17751777
{
1776-
this.WrappedSearchContext = searchContext ?? throw new ArgumentNullException(nameof(searchContext));
1778+
ArgumentNullException.ThrowIfNull(searchContext);
1779+
this.WrappedSearchContext = searchContext;
17771780
this.parentDriver = driver;
17781781
}
17791782

dotnet/src/support/Events/FindElementEventArgs.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ public FindElementEventArgs(IWebDriver driver, By method)
4444
/// <exception cref="ArgumentNullException">If <paramref name="driver"/> or <paramref name="method"/> are <see langword="null"/>.</exception>
4545
public FindElementEventArgs(IWebDriver driver, IWebElement? element, By method)
4646
{
47-
this.Driver = driver ?? throw new ArgumentNullException(nameof(driver));
47+
ArgumentNullException.ThrowIfNull(driver);
48+
ArgumentNullException.ThrowIfNull(method);
49+
this.Driver = driver;
4850
this.Element = element;
49-
this.FindMethod = method ?? throw new ArgumentNullException(nameof(method));
51+
this.FindMethod = method;
5052
}
5153

5254
/// <summary>

dotnet/src/support/Events/GetShadowRootEventArgs.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ public class GetShadowRootEventArgs : EventArgs
3232
/// <exception cref="ArgumentNullException">If <paramref name="driver"/> or <paramref name="searchContext"/> are <see langword="null"/>.</exception>
3333
public GetShadowRootEventArgs(IWebDriver driver, ISearchContext searchContext)
3434
{
35-
this.Driver = driver ?? throw new ArgumentNullException(nameof(driver));
36-
this.SearchContext = searchContext ?? throw new ArgumentNullException(nameof(searchContext));
35+
ArgumentNullException.ThrowIfNull(driver);
36+
ArgumentNullException.ThrowIfNull(searchContext);
37+
this.Driver = driver;
38+
this.SearchContext = searchContext;
3739
}
3840

3941
/// <summary>

dotnet/src/support/Events/WebDriverExceptionEventArgs.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ public class WebDriverExceptionEventArgs : EventArgs
3232
/// <exception cref="ArgumentNullException">If <paramref name="driver"/> or <paramref name="thrownException"/> are <see langword="null"/>.</exception>
3333
public WebDriverExceptionEventArgs(IWebDriver driver, Exception thrownException)
3434
{
35-
this.Driver = driver ?? throw new ArgumentNullException(nameof(driver));
36-
this.ThrownException = thrownException ?? throw new ArgumentNullException(nameof(thrownException));
35+
ArgumentNullException.ThrowIfNull(driver);
36+
ArgumentNullException.ThrowIfNull(thrownException);
37+
this.Driver = driver;
38+
this.ThrownException = thrownException;
3739
}
3840

3941
/// <summary>

dotnet/src/support/Events/WebDriverNavigationEventArgs.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ public WebDriverNavigationEventArgs(IWebDriver driver)
4242
/// <exception cref="ArgumentNullException">If <paramref name="driver"/> is <see langword="null"/>.</exception>
4343
public WebDriverNavigationEventArgs(IWebDriver driver, string? url)
4444
{
45+
ArgumentNullException.ThrowIfNull(driver);
4546
this.Url = url;
46-
this.Driver = driver ?? throw new ArgumentNullException(nameof(driver));
47+
this.Driver = driver;
4748
}
4849

4950
/// <summary>

dotnet/src/support/Events/WebDriverScriptEventArgs.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ public class WebDriverScriptEventArgs : EventArgs
3434
/// <exception cref="ArgumentNullException">If <paramref name="driver"/> or <paramref name="script"/> are <see langword="null"/>.</exception>
3535
public WebDriverScriptEventArgs(IWebDriver driver, [StringSyntax(StringSyntaxConstants.JavaScript)] string script)
3636
{
37-
this.Driver = driver ?? throw new ArgumentNullException(nameof(driver));
38-
this.Script = script ?? throw new ArgumentNullException(nameof(script));
37+
ArgumentNullException.ThrowIfNull(driver);
38+
ArgumentNullException.ThrowIfNull(script);
39+
this.Driver = driver;
40+
this.Script = script;
3941
}
4042

4143
/// <summary>

dotnet/src/support/Events/WebElementEventArgs.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ public class WebElementEventArgs : EventArgs
3232
/// <exception cref="ArgumentNullException">If <paramref name="driver"/> or <paramref name="element"/> are <see langword="null"/>.</exception>
3333
public WebElementEventArgs(IWebDriver driver, IWebElement element)
3434
{
35-
this.Driver = driver ?? throw new ArgumentNullException(nameof(driver));
36-
this.Element = element ?? throw new ArgumentNullException(nameof(element));
35+
ArgumentNullException.ThrowIfNull(driver);
36+
ArgumentNullException.ThrowIfNull(element);
37+
this.Driver = driver;
38+
this.Element = element;
3739
}
3840

3941
/// <summary>

dotnet/src/support/Selenium.WebDriver.Support.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
<!--Workaround for being unable to have two different types of the same name in WebDriver and in Support, despite both being internal-->
4545
<Compile Include="$(CsprojIncludePath)\Properties\StringSyntaxAttribute.cs" Link="Properties\StringSyntaxAttribute.cs" />
4646
<Compile Include="$(CsprojIncludePath)\Properties\StringSyntaxConstants.cs" Link="Properties\StringSyntaxConstants.cs" />
47+
<Compile Include="$(CsprojIncludePath)\Properties\NullableAttributes.cs" Link="Properties\NullableAttributes.cs" />
48+
<Compile Include="$(CsprojIncludePath)\Properties\CallerArgumentExpressionAttribute.cs" Link="Properties\CallerArgumentExpressionAttribute.cs" />
49+
<Compile Include="$(CsprojIncludePath)\Properties\ArgumentNullExceptionExtensions.cs" Link="Properties\ArgumentNullExceptionExtensions.cs" />
4750
</ItemGroup>
4851

4952
</Project>

dotnet/src/support/UI/PopupWindowFinder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public PopupWindowFinder(IWebDriver driver, TimeSpan timeout)
8484
/// <exception cref="ArgumentNullException">If <paramref name="driver"/> is <see langword="null"/>.</exception>
8585
public PopupWindowFinder(IWebDriver driver, TimeSpan timeout, TimeSpan sleepInterval)
8686
{
87-
this.driver = driver ?? throw new ArgumentNullException(nameof(driver));
87+
ArgumentNullException.ThrowIfNull(driver);
88+
this.driver = driver;
8889
this.timeout = timeout;
8990
this.sleepInterval = sleepInterval;
9091
}

0 commit comments

Comments
 (0)