Skip to content

Commit

Permalink
More validations
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed Nov 16, 2023
1 parent 71c6603 commit 1ee2d78
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp.Tests/ElementHandleTests/ClickTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task ShouldThrowForDetachedNodes()
var button = await Page.QuerySelectorAsync("button");
await Page.EvaluateFunctionAsync("button => button.remove()", button);
var exception = Assert.ThrowsAsync<PuppeteerException>(async () => await button.ClickAsync());
Assert.AreEqual("Node is detached from document", exception.Message);
Assert.AreEqual("Node is either not visible or not an HTMLElement", exception.Message);
}

[PuppeteerTest("elementhandle.spec.ts", "ElementHandle.click", "should throw for hidden nodes")]
Expand Down
3 changes: 1 addition & 2 deletions lib/PuppeteerSharp/ElementHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,7 @@ await handle.EvaluateFunctionAsync(
inline: 'center',
behavior: 'instant',
});
}",
null).ConfigureAwait(false);
}").ConfigureAwait(false);
}

return handle;
Expand Down
37 changes: 33 additions & 4 deletions lib/PuppeteerSharp/Frame.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using PuppeteerSharp.Helpers;
using PuppeteerSharp.Input;

namespace PuppeteerSharp
{
/// <inheritdoc/>
/// <inheritdoc cref="PuppeteerSharp.IFrame" />
public class Frame : IFrame, IEnvironment
{
private Task<ElementHandle> _documentTask;
Expand All @@ -33,7 +32,7 @@ internal Frame(FrameManager frameManager, string frameId, string parentFrameId,
public string Url { get; private set; } = string.Empty;

/// <inheritdoc/>
public bool Detached { get; set; }
public bool Detached { get; private set; }

/// <inheritdoc/>
public IPage Page => FrameManager.Page;
Expand All @@ -59,7 +58,7 @@ internal Frame(FrameManager frameManager, string frameId, string parentFrameId,

internal FrameManager FrameManager { get; }

internal string LoaderId { get; set; }
internal string LoaderId { get; private set; }

internal List<string> LifecycleEvents { get; } = new();

Expand Down Expand Up @@ -158,6 +157,12 @@ public Task<IJSHandle> WaitForExpressionAsync(string script, WaitForFunctionOpti
public async Task<string[]> SelectAsync(string selector, params string[] values)
{
var handle = await QuerySelectorAsync(selector).ConfigureAwait(false);

if (handle == null)
{
throw new SelectorException($"No node found for selector: {selector}", selector);
}

return await handle.SelectAsync(values).ConfigureAwait(false);
}

Expand Down Expand Up @@ -369,6 +374,12 @@ await IsolatedRealm.EvaluateFunctionAsync(
public async Task ClickAsync(string selector, ClickOptions options = null)
{
var handle = await QuerySelectorAsync(selector).ConfigureAwait(false);

if (handle == null)
{
throw new SelectorException($"No node found for selector: {selector}", selector);
}

await handle.ClickAsync(options).ConfigureAwait(false);
await handle.DisposeAsync().ConfigureAwait(false);
}
Expand All @@ -377,20 +388,38 @@ public async Task ClickAsync(string selector, ClickOptions options = null)
public async Task HoverAsync(string selector)
{
var handle = await QuerySelectorAsync(selector).ConfigureAwait(false);

if (handle == null)
{
throw new SelectorException($"No node found for selector: {selector}", selector);
}

await handle.HoverAsync().ConfigureAwait(false);
}

/// <inheritdoc/>
public async Task FocusAsync(string selector)
{
var handle = await QuerySelectorAsync(selector).ConfigureAwait(false);

if (handle == null)
{
throw new SelectorException($"No node found for selector: {selector}", selector);
}

await handle.FocusAsync().ConfigureAwait(false);
}

/// <inheritdoc/>
public async Task TypeAsync(string selector, string text, TypeOptions options = null)
{
var handle = await QuerySelectorAsync(selector).ConfigureAwait(false);

if (handle == null)
{
throw new SelectorException($"No node found for selector: {selector}", selector);
}

await handle.TypeAsync(text, options).ConfigureAwait(false);
}

Expand Down

0 comments on commit 1ee2d78

Please sign in to comment.