Skip to content

Commit

Permalink
Test - Add JSHandle.clickablePoint tests
Browse files Browse the repository at this point in the history
- Add test cases
- Fix offset typo
  • Loading branch information
amaitland committed Aug 30, 2022
1 parent e07ace2 commit 13fbdde
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
74 changes: 74 additions & 0 deletions lib/PuppeteerSharp.Tests/JSHandleTests/ClickablePointTests.cs
@@ -0,0 +1,74 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Policy;
using System.Threading.Tasks;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;

namespace PuppeteerSharp.Tests.JSHandleTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class ClickablePointTests : PuppeteerPageBaseTest
{
public ClickablePointTests(ITestOutputHelper output) : base(output)
{
}

[PuppeteerTest("jshandle.spec.ts", "JSHandle.clickablePoint", "should work")]
[PuppeteerFact]
public async Task ShouldWork()
{
await Page.EvaluateExpressionAsync(@"document.body.style.padding = '0';
document.body.style.margin = '0';
document.body.innerHTML = '<div style=""cursor: pointer; width: 120px; height: 60px; margin: 30px; padding: 15px;""></div>';
");

await Page.EvaluateExpressionAsync("new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));");

var divHandle = await Page.QuerySelectorAsync("div");

var clickablePoint = await divHandle.ClickablePointAsync();

// margin + middle point offset
Assert.Equal(45 + 60, clickablePoint.X);
Assert.Equal(45 + 30, clickablePoint.Y);

clickablePoint = await divHandle.ClickablePointAsync(new Offset { X = 10, Y = 15 });

// margin + offset
Assert.Equal(30 + 10, clickablePoint.X);
Assert.Equal(30 + 15, clickablePoint.Y);
}

[PuppeteerTest("jshandle.spec.ts", "JSHandle.clickablePoint", "should work for iframes")]
[PuppeteerFact]
public async Task ShouldWorkForIFrames()
{
await Page.EvaluateExpressionAsync(@"document.body.style.padding = '10px';
document.body.style.margin = '10px';
document.body.innerHTML = `<iframe style=""border: none; margin: 0; padding: 0;"" seamless sandbox srcdoc=""<style>* { margin: 0; padding: 0;}</style><div style='cursor: pointer; width: 120px; height: 60px; margin: 30px; padding: 15px;' />""></iframe>`
");

await Page.EvaluateExpressionAsync("new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));");

var frame = Page.FirstChildFrame();

var divHandle = await frame.QuerySelectorAsync("div");

var clickablePoint = await divHandle.ClickablePointAsync();

// iframe pos + margin + middle point offset
Assert.Equal(20 + 45 + 60, clickablePoint.X);
Assert.Equal(20 + 45 + 30, clickablePoint.Y);

clickablePoint = await divHandle.ClickablePointAsync(new Offset { X = 10, Y = 15 });

// iframe pos + margin + offset
Assert.Equal(20 + 30 + 10, clickablePoint.X);
Assert.Equal(20 + 30 + 15, clickablePoint.Y);
}
}
}
10 changes: 5 additions & 5 deletions lib/PuppeteerSharp/Offset.cs
Expand Up @@ -6,23 +6,23 @@ namespace PuppeteerSharp
public struct Offset
{
/// <summary>
/// Offset
/// Initializes a new instance of the <see cref="Offset"/> struct.
/// </summary>
/// <param name="x">x-offset for the clickable point relative to the top-left corder of the border box.</param>
/// <param name="y">y-offset for the clickable point relative to the top-left corder of the border box.</param>
/// <param name="x">x-offset for the clickable point relative to the top-left corner of the border box.</param>
/// <param name="y">y-offset for the clickable point relative to the top-left corner of the border box.</param>
public Offset(decimal x, decimal y)
{
X = x;
Y = y;
}

/// <summary>
/// x-offset for the clickable point relative to the top-left corder of the border box.
/// x-offset for the clickable point relative to the top-left corner of the border box.
/// </summary>
public decimal X { get; set; }

/// <summary>
/// y-offset for the clickable point relative to the top-left corder of the border box.
/// y-offset for the clickable point relative to the top-left corner of the border box.
/// </summary>
public decimal Y { get; set; }
}
Expand Down

0 comments on commit 13fbdde

Please sign in to comment.