Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add new Offset struct and replace BoxModelPoint usage for ClickablePointAsync - Add new Test case based on JSHandle.click should work - Add JSHandle.clickablePoint tests
- Loading branch information
Showing
7 changed files
with
177 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<BoxModelPoint>(); | ||
|
||
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 = '<div style=""cursor: pointer; width: 120px; height: 60px; margin: 30px; padding: 15px;""></div>'; | ||
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); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
lib/PuppeteerSharp.Tests/JSHandleTests/ClickablePointTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace PuppeteerSharp | ||
{ | ||
/// <summary> | ||
/// Offset used in conjunction with <see cref="ElementHandle.ClickablePointAsync(Offset?)"/> | ||
/// </summary> | ||
public struct Offset | ||
{ | ||
/// <summary> | ||
/// 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 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 corner of the border box. | ||
/// </summary> | ||
public decimal X { get; set; } | ||
|
||
/// <summary> | ||
/// y-offset for the clickable point relative to the top-left corner of the border box. | ||
/// </summary> | ||
public decimal Y { get; set; } | ||
} | ||
} |