|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Reflection; |
| 4 | +using System.Security.Policy; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using PuppeteerSharp.Tests.Attributes; |
| 7 | +using PuppeteerSharp.Xunit; |
| 8 | +using Xunit; |
| 9 | +using Xunit.Abstractions; |
| 10 | + |
| 11 | +namespace PuppeteerSharp.Tests.JSHandleTests |
| 12 | +{ |
| 13 | + [Collection(TestConstants.TestFixtureCollectionName)] |
| 14 | + public class ClickablePointTests : PuppeteerPageBaseTest |
| 15 | + { |
| 16 | + public ClickablePointTests(ITestOutputHelper output) : base(output) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + [PuppeteerTest("jshandle.spec.ts", "JSHandle.clickablePoint", "should work")] |
| 21 | + [PuppeteerFact] |
| 22 | + public async Task ShouldWork() |
| 23 | + { |
| 24 | + await Page.EvaluateExpressionAsync(@"document.body.style.padding = '0'; |
| 25 | + document.body.style.margin = '0'; |
| 26 | + document.body.innerHTML = '<div style=""cursor: pointer; width: 120px; height: 60px; margin: 30px; padding: 15px;""></div>'; |
| 27 | + "); |
| 28 | + |
| 29 | + await Page.EvaluateExpressionAsync("new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));"); |
| 30 | + |
| 31 | + var divHandle = await Page.QuerySelectorAsync("div"); |
| 32 | + |
| 33 | + var clickablePoint = await divHandle.ClickablePointAsync(); |
| 34 | + |
| 35 | + // margin + middle point offset |
| 36 | + Assert.Equal(45 + 60, clickablePoint.X); |
| 37 | + Assert.Equal(45 + 30, clickablePoint.Y); |
| 38 | + |
| 39 | + clickablePoint = await divHandle.ClickablePointAsync(new Offset { X = 10, Y = 15 }); |
| 40 | + |
| 41 | + // margin + offset |
| 42 | + Assert.Equal(30 + 10, clickablePoint.X); |
| 43 | + Assert.Equal(30 + 15, clickablePoint.Y); |
| 44 | + } |
| 45 | + |
| 46 | + [PuppeteerTest("jshandle.spec.ts", "JSHandle.clickablePoint", "should work for iframes")] |
| 47 | + [PuppeteerFact] |
| 48 | + public async Task ShouldWorkForIFrames() |
| 49 | + { |
| 50 | + await Page.EvaluateExpressionAsync(@"document.body.style.padding = '10px'; |
| 51 | + document.body.style.margin = '10px'; |
| 52 | + 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>` |
| 53 | + "); |
| 54 | + |
| 55 | + await Page.EvaluateExpressionAsync("new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));"); |
| 56 | + |
| 57 | + var frame = Page.FirstChildFrame(); |
| 58 | + |
| 59 | + var divHandle = await frame.QuerySelectorAsync("div"); |
| 60 | + |
| 61 | + var clickablePoint = await divHandle.ClickablePointAsync(); |
| 62 | + |
| 63 | + // iframe pos + margin + middle point offset |
| 64 | + Assert.Equal(20 + 45 + 60, clickablePoint.X); |
| 65 | + Assert.Equal(20 + 45 + 30, clickablePoint.Y); |
| 66 | + |
| 67 | + clickablePoint = await divHandle.ClickablePointAsync(new Offset { X = 10, Y = 15 }); |
| 68 | + |
| 69 | + // iframe pos + margin + offset |
| 70 | + Assert.Equal(20 + 30 + 10, clickablePoint.X); |
| 71 | + Assert.Equal(20 + 30 + 15, clickablePoint.Y); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments