Skip to content

Commit 6dfc940

Browse files
authored
Cleanups (#2136)
* Remove unused variable Unused since 13c04da * Remove redundant type cast * Remove null check If `res` was empty calling `.BackendDOMNodeId` would throw a NullReferenceException * Use coalesce expression * Use explicit cast In case of failure we get an InvalidCastException instead of a NullReferenceException * Remove unnecessary cast * Remove unused capturing group * Handle empty sequence from QueryAXTreeAsync
1 parent d4b53b7 commit 6dfc940

File tree

8 files changed

+15
-33
lines changed

8 files changed

+15
-33
lines changed

lib/PuppeteerSharp.Tests/PageTests/AddScriptTagTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task ShouldWorkWithAUrl()
3030
{
3131
await Page.GoToAsync(TestConstants.EmptyPage);
3232
var scriptHandle = await Page.AddScriptTagAsync(new AddTagOptions { Url = "/injectedfile.js" });
33-
Assert.NotNull(scriptHandle as IElementHandle);
33+
Assert.NotNull(scriptHandle);
3434
Assert.Equal(42, await Page.EvaluateExpressionAsync<int>("__injected"));
3535
}
3636

@@ -90,7 +90,7 @@ public async Task ShouldWorkWithAPath()
9090
{
9191
Path = Path.Combine(Directory.GetCurrentDirectory(), Path.Combine("Assets", "injectedfile.js"))
9292
});
93-
Assert.NotNull(scriptHandle as IElementHandle);
93+
Assert.NotNull(scriptHandle);
9494
Assert.Equal(42, await Page.EvaluateExpressionAsync<int>("__injected"));
9595
}
9696

@@ -113,7 +113,7 @@ public async Task ShouldWorkWithContent()
113113
{
114114
await Page.GoToAsync(TestConstants.EmptyPage);
115115
var scriptHandle = await Page.AddScriptTagAsync(new AddTagOptions { Content = "window.__injected = 35;" });
116-
Assert.NotNull(scriptHandle as IElementHandle);
116+
Assert.NotNull(scriptHandle);
117117
Assert.Equal(35, await Page.EvaluateExpressionAsync<int>("__injected"));
118118
}
119119

lib/PuppeteerSharp.Tests/PageTests/AddStyleTagTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task ShouldWorkWithAUrl()
3030
{
3131
await Page.GoToAsync(TestConstants.EmptyPage);
3232
var styleHandle = await Page.AddStyleTagAsync(new AddTagOptions { Url = "/injectedstyle.css" });
33-
Assert.NotNull(styleHandle as IElementHandle);
33+
Assert.NotNull(styleHandle);
3434
Assert.Equal("rgb(255, 0, 0)", await Page.EvaluateExpressionAsync<string>(
3535
"window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')"));
3636
}
@@ -51,7 +51,7 @@ public async Task ShouldWorkWithAPath()
5151
{
5252
await Page.GoToAsync(TestConstants.EmptyPage);
5353
var styleHandle = await Page.AddStyleTagAsync(new AddTagOptions { Path = "Assets/injectedstyle.css" });
54-
Assert.NotNull(styleHandle as IElementHandle);
54+
Assert.NotNull(styleHandle);
5555
Assert.Equal("rgb(255, 0, 0)", await Page.EvaluateExpressionAsync<string>(
5656
"window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')"));
5757
}
@@ -76,7 +76,7 @@ public async Task ShouldWorkWithContent()
7676
{
7777
await Page.GoToAsync(TestConstants.EmptyPage);
7878
var styleHandle = await Page.AddStyleTagAsync(new AddTagOptions { Content = "body { background-color: green; }" });
79-
Assert.NotNull(styleHandle as IElementHandle);
79+
Assert.NotNull(styleHandle);
8080
Assert.Equal("rgb(0, 128, 0)", await Page.EvaluateExpressionAsync<string>(
8181
"window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')"));
8282
}

lib/PuppeteerSharp/AriaQueryHandlerFactory.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ internal static InternalQueryHandler Create()
2626
{
2727
var queryOptions = ParseAriaSelector(selector);
2828
var res = await QueryAXTreeAsync(((ElementHandle)element).Client, element, queryOptions.Name, queryOptions.Role).ConfigureAwait(false);
29-
if (res.FirstOrDefault().BackendDOMNodeId == null)
30-
{
31-
return null;
32-
}
3329

34-
return res.First().BackendDOMNodeId;
30+
return res.FirstOrDefault()?.BackendDOMNodeId;
3531
};
3632

3733
Func<IElementHandle, string, Task<IElementHandle>> queryOne = async (IElementHandle element, string selector) =>
@@ -47,7 +43,7 @@ internal static InternalQueryHandler Create()
4743

4844
Func<IElementHandle, string, WaitForSelectorOptions, Task<IElementHandle>> waitFor = async (IElementHandle root, string selector, WaitForSelectorOptions options) =>
4945
{
50-
var frame = (root as ElementHandle).Frame;
46+
var frame = ((ElementHandle)root).Frame;
5147
var element = await frame.SecondaryWorld.AdoptHandleAsync(root).ConfigureAwait(false);
5248

5349
Func<string, Task<IElementHandle>> func = (string selector) => queryOne(element, selector);
@@ -67,7 +63,7 @@ internal static InternalQueryHandler Create()
6763

6864
Func<IElementHandle, string, Task<IElementHandle[]>> queryAll = async (IElementHandle element, string selector) =>
6965
{
70-
var exeCtx = element.ExecutionContext as ExecutionContext;
66+
var exeCtx = (ExecutionContext)element.ExecutionContext;
7167
var world = exeCtx.World;
7268
var ariaSelector = ParseAriaSelector(selector);
7369
var res = await QueryAXTreeAsync(exeCtx.Client, element, ariaSelector.Name, ariaSelector.Role).ConfigureAwait(false);

lib/PuppeteerSharp/Browser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ private void TargetManager_TargetChanged(object sender, TargetChangedArgs e)
362362
{
363363
var args = new TargetChangedArgs { Target = e.Target };
364364
TargetChanged?.Invoke(this, args);
365-
((BrowserContext)e.Target.BrowserContext).OnTargetChanged(this, args);
365+
e.Target.BrowserContext.OnTargetChanged(this, args);
366366
}
367367
}
368368

@@ -377,7 +377,7 @@ private async void TargetManager_TargetGone(object sender, TargetChangedArgs e)
377377
{
378378
var args = new TargetChangedArgs { Target = e.Target };
379379
TargetDestroyed?.Invoke(this, args);
380-
((BrowserContext)e.Target.BrowserContext).OnTargetDestroyed(this, args);
380+
e.Target.BrowserContext.OnTargetDestroyed(this, args);
381381
}
382382
}
383383
catch (Exception ex)

lib/PuppeteerSharp/ExecutionContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ExecutionContext : IExecutionContext
1515
{
1616
internal const string EvaluationScriptUrl = "__puppeteer_evaluation_script__";
1717

18-
private static readonly Regex _sourceUrlRegex = new(@"^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$", RegexOptions.Multiline);
18+
private static readonly Regex _sourceUrlRegex = new(@"^[\040\t]*\/\/[@#] sourceURL=\s*\S*?\s*$", RegexOptions.Multiline);
1919

2020
private readonly string _evaluationScriptSuffix = $"//# sourceURL={EvaluationScriptUrl}";
2121

lib/PuppeteerSharp/Page.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -628,12 +628,7 @@ public Task<string> ScreenshotBase64Async(ScreenshotOptions options)
628628
throw new ArgumentNullException(nameof(options));
629629
}
630630

631-
var screenshotType = options.Type;
632-
633-
if (!screenshotType.HasValue)
634-
{
635-
screenshotType = ScreenshotType.Png;
636-
}
631+
var screenshotType = options.Type ?? ScreenshotType.Png;
637632

638633
if (options.Quality.HasValue)
639634
{
@@ -663,7 +658,7 @@ public Task<string> ScreenshotBase64Async(ScreenshotOptions options)
663658
throw new ArgumentException("options.clip and options.fullPage are exclusive");
664659
}
665660

666-
return _screenshotTaskQueue.Enqueue(() => PerformScreenshot(screenshotType.Value, options));
661+
return _screenshotTaskQueue.Enqueue(() => PerformScreenshot(screenshotType, options));
667662
}
668663

669664
/// <inheritdoc/>

lib/PuppeteerSharp/PageAccessibility/AXNode.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,7 @@ private bool IsTextOnlyObject()
256256

257257
private bool HasFocusableChild()
258258
{
259-
if (!_cachedHasFocusableChild.HasValue)
260-
{
261-
_cachedHasFocusableChild = Children.Any(c => c.Focusable || c.HasFocusableChild());
262-
}
263-
264-
return _cachedHasFocusableChild.Value;
259+
return _cachedHasFocusableChild ??= Children.Any(c => c.Focusable || c.HasFocusableChild());
265260
}
266261

267262
private string GetIfNotFalse(string value) => value != null && value != "false" ? value : null;

lib/PuppeteerSharp/Target.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,12 @@ public Task<Worker> WorkerAsync()
158158

159159
internal void TargetInfoChanged(TargetInfo targetInfo)
160160
{
161-
var previousUrl = TargetInfo.Url;
162161
TargetInfo = targetInfo;
163162

164163
if (!IsInitialized && (TargetInfo.Type != TargetType.Page || !string.IsNullOrEmpty(TargetInfo.Url)))
165164
{
166165
IsInitialized = true;
167166
InitializedTaskWrapper.TrySetResult(true);
168-
return;
169167
}
170168
}
171169

@@ -183,8 +181,6 @@ private async Task<IPage> CreatePageAsync()
183181
{
184182
var session = Session ?? await _sessionFactory(true).ConfigureAwait(false);
185183

186-
var browser = (Browser)Browser;
187-
188184
return await Page.CreateAsync(
189185
session,
190186
this,

0 commit comments

Comments
 (0)