diff --git a/lib/PuppeteerSharp.Tests/JSHandleTests/ClickTests.cs b/lib/PuppeteerSharp.Tests/JSHandleTests/ClickTests.cs new file mode 100644 index 000000000..f31175772 --- /dev/null +++ b/lib/PuppeteerSharp.Tests/JSHandleTests/ClickTests.cs @@ -0,0 +1,53 @@ +using System.Collections.Generic; +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 ClickTests : PuppeteerPageBaseTest + { + public ClickTests(ITestOutputHelper output) : base(output) + { + } + + [PuppeteerTest("jshandle.spec.ts", "JSHandle.click", "should work")] + [SkipBrowserFact(skipFirefox: true)] + public async Task ShouldWork() + { + var clicks = new List(); + + await Page.ExposeFunctionAsync("reportClick", (int x, int y) => + { + clicks.Add(new BoxModelPoint { X = x, Y = y }); + + return true; + }); + + await Page.EvaluateExpressionAsync(@"document.body.style.padding = '0'; + document.body.style.margin = '0'; + document.body.innerHTML = '
'; + document.body.addEventListener('click', e => { + window.reportClick(e.clientX, e.clientY); + });"); + + var divHandle = await Page.QuerySelectorAsync("div"); + + await divHandle.ClickAsync(); + await divHandle.ClickAsync(new Input.ClickOptions { OffSet = new Offset(10, 15) }); + + await TestUtils.ShortWaitForCollectionToHaveAtLeastNElementsAsync(clicks, 2); + + // margin + middle point offset + Assert.Equal(clicks[0].X, 45 + 60); + Assert.Equal(clicks[0].Y, 45 + 30); + + // margin + offset + Assert.Equal(clicks[1].X, 30 + 10); + Assert.Equal(clicks[1].Y, 30 + 15); + } + } +} diff --git a/lib/PuppeteerSharp.Tests/JSHandleTests/ClickablePointTests.cs b/lib/PuppeteerSharp.Tests/JSHandleTests/ClickablePointTests.cs new file mode 100644 index 000000000..e41cf13bd --- /dev/null +++ b/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 = '
'; + "); + + 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 = `` + "); + + 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); + } + } +} diff --git a/lib/PuppeteerSharp.Tests/TestUtils.cs b/lib/PuppeteerSharp.Tests/TestUtils.cs index 6f8e7ce63..e2be25108 100644 --- a/lib/PuppeteerSharp.Tests/TestUtils.cs +++ b/lib/PuppeteerSharp.Tests/TestUtils.cs @@ -1,3 +1,4 @@ +using System.Collections; using System.IO; using System.Text; using System.Threading.Tasks; @@ -6,6 +7,18 @@ namespace PuppeteerSharp.Tests { public static class TestUtils { + public static async Task ShortWaitForCollectionToHaveAtLeastNElementsAsync(ICollection collection, int minLength, int attempts = 3, int timeout = 50) + { + for (var i = 0; i < attempts; i++) + { + if (collection.Count >= minLength) + { + break; + } + await Task.Delay(timeout); + } + } + public static string FindParentDirectory(string directory) { var current = Directory.GetCurrentDirectory(); diff --git a/lib/PuppeteerSharp/ElementHandle.cs b/lib/PuppeteerSharp/ElementHandle.cs index 544ce5e81..a44978a79 100644 --- a/lib/PuppeteerSharp/ElementHandle.cs +++ b/lib/PuppeteerSharp/ElementHandle.cs @@ -179,7 +179,7 @@ public async Task HoverAsync() public async Task ClickAsync(ClickOptions options = null) { await ScrollIntoViewIfNeededAsync().ConfigureAwait(false); - var clickablePoint = await ClickablePointAsync().ConfigureAwait(false); + var clickablePoint = await ClickablePointAsync(options?.OffSet).ConfigureAwait(false); await Page.Mouse.ClickAsync(clickablePoint.X, clickablePoint.Y, options).ConfigureAwait(false); } @@ -469,7 +469,7 @@ public async Task DragAndDropAsync(IElementHandle target, int delay = 0) } /// - public async Task ClickablePointAsync(BoxModelPoint? offset = null) + public async Task ClickablePointAsync(Offset? offset = null) { GetContentQuadsResponse result = null; diff --git a/lib/PuppeteerSharp/IElementHandle.cs b/lib/PuppeteerSharp/IElementHandle.cs index 581cccdcc..e25cc5e68 100644 --- a/lib/PuppeteerSharp/IElementHandle.cs +++ b/lib/PuppeteerSharp/IElementHandle.cs @@ -30,7 +30,7 @@ public interface IElementHandle : IJSHandle /// Optional offset /// When the node is not visible or not an HTMLElement /// A that resolves to the clickable point - public Task ClickablePointAsync(BoxModelPoint? offset = null); + public Task ClickablePointAsync(Offset? offset = null); /// /// Scrolls element into view if needed, and then uses to click in the center of the element. diff --git a/lib/PuppeteerSharp/Input/ClickOptions.cs b/lib/PuppeteerSharp/Input/ClickOptions.cs index 7401ff9c4..c30e58c01 100644 --- a/lib/PuppeteerSharp/Input/ClickOptions.cs +++ b/lib/PuppeteerSharp/Input/ClickOptions.cs @@ -19,5 +19,10 @@ public class ClickOptions /// The button to use for the click. Defaults to /// public MouseButton Button { get; set; } = MouseButton.Left; + + /// + /// Offset for the clickable point relative to the top-left corner of the border-box. + /// + public Offset? OffSet { get; set; } } } diff --git a/lib/PuppeteerSharp/Offset.cs b/lib/PuppeteerSharp/Offset.cs new file mode 100644 index 000000000..abd675b66 --- /dev/null +++ b/lib/PuppeteerSharp/Offset.cs @@ -0,0 +1,29 @@ +namespace PuppeteerSharp +{ + /// + /// Offset used in conjunction with + /// + public struct Offset + { + /// + /// Initializes a new instance of the struct. + /// + /// x-offset for the clickable point relative to the top-left corner of the border box. + /// y-offset for the clickable point relative to the top-left corner of the border box. + public Offset(decimal x, decimal y) + { + X = x; + Y = y; + } + + /// + /// x-offset for the clickable point relative to the top-left corner of the border box. + /// + public decimal X { get; set; } + + /// + /// y-offset for the clickable point relative to the top-left corner of the border box. + /// + public decimal Y { get; set; } + } +}