From e71b6e9db698653e11abb3a4f3d11c9b06522409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Tue, 19 Mar 2019 08:02:11 -0300 Subject: [PATCH] Test refactors (#1021) --- .../wwwroot/frames/nested-frames.html | 49 ++++++++------- .../wwwroot/frames/two-frames.html | 26 ++++---- .../wwwroot/title.html | 1 + .../wwwroot/wrappedlink.html | 60 ++++++++++--------- .../FrameTests/FrameManagementTests.cs | 4 +- lib/PuppeteerSharp.Tests/FrameUtils.cs | 14 +++-- .../InputTests/InputTests.cs | 1 - .../PageTests/EvaluateTests.cs | 16 +---- .../PageTests/TitleTests.cs | 4 +- .../PuppeteerTests/PuppeteerConnectTests.cs | 2 +- lib/PuppeteerSharp.Tests/TestConstants.cs | 14 +++-- lib/PuppeteerSharp/FrameTree.cs | 1 + 12 files changed, 97 insertions(+), 95 deletions(-) create mode 100644 lib/PuppeteerSharp.TestServer/wwwroot/title.html diff --git a/lib/PuppeteerSharp.TestServer/wwwroot/frames/nested-frames.html b/lib/PuppeteerSharp.TestServer/wwwroot/frames/nested-frames.html index cb14fe6be..a87bdfb0a 100644 --- a/lib/PuppeteerSharp.TestServer/wwwroot/frames/nested-frames.html +++ b/lib/PuppeteerSharp.TestServer/wwwroot/frames/nested-frames.html @@ -1,25 +1,24 @@ - - - - + + + + diff --git a/lib/PuppeteerSharp.TestServer/wwwroot/frames/two-frames.html b/lib/PuppeteerSharp.TestServer/wwwroot/frames/two-frames.html index 46a50f3f6..cf062cc54 100644 --- a/lib/PuppeteerSharp.TestServer/wwwroot/frames/two-frames.html +++ b/lib/PuppeteerSharp.TestServer/wwwroot/frames/two-frames.html @@ -1,13 +1,13 @@ - - - + + + diff --git a/lib/PuppeteerSharp.TestServer/wwwroot/title.html b/lib/PuppeteerSharp.TestServer/wwwroot/title.html new file mode 100644 index 000000000..88a86ce41 --- /dev/null +++ b/lib/PuppeteerSharp.TestServer/wwwroot/title.html @@ -0,0 +1 @@ +Woof-Woof diff --git a/lib/PuppeteerSharp.TestServer/wwwroot/wrappedlink.html b/lib/PuppeteerSharp.TestServer/wwwroot/wrappedlink.html index 55e9c3f9f..75461a69a 100644 --- a/lib/PuppeteerSharp.TestServer/wwwroot/wrappedlink.html +++ b/lib/PuppeteerSharp.TestServer/wwwroot/wrappedlink.html @@ -1,29 +1,31 @@ - -
- 123321 -
- + +
+ 123321 +
+ diff --git a/lib/PuppeteerSharp.Tests/FrameTests/FrameManagementTests.cs b/lib/PuppeteerSharp.Tests/FrameTests/FrameManagementTests.cs index e14c85c02..94bf55201 100644 --- a/lib/PuppeteerSharp.Tests/FrameTests/FrameManagementTests.cs +++ b/lib/PuppeteerSharp.Tests/FrameTests/FrameManagementTests.cs @@ -18,8 +18,8 @@ public async Task ShouldHandleNestedFrames() { await Page.GoToAsync(TestConstants.ServerUrl + "/frames/nested-frames.html"); Assert.Equal( - TestUtils.CompressText(TestConstants.NestedFramesDumpResult), - TestUtils.CompressText(FrameUtils.DumpFrames(Page.MainFrame))); + TestConstants.NestedFramesDumpResult, + FrameUtils.DumpFrames(Page.MainFrame)); } [Fact] diff --git a/lib/PuppeteerSharp.Tests/FrameUtils.cs b/lib/PuppeteerSharp.Tests/FrameUtils.cs index c034f072d..b9f36a62c 100644 --- a/lib/PuppeteerSharp.Tests/FrameUtils.cs +++ b/lib/PuppeteerSharp.Tests/FrameUtils.cs @@ -1,4 +1,5 @@ -using System.Text.RegularExpressions; +using System.Collections.Generic; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace PuppeteerSharp.Tests @@ -26,12 +27,17 @@ await page.EvaluateFunctionAsync(@"function detachFrame(frameId) { }", frameId); } - public static string DumpFrames(Frame frame, string indentation = "") + public static IEnumerable DumpFrames(Frame frame, string indentation = "") { - var result = indentation + Regex.Replace(frame.Url, @":\d{4}", ":"); + var description = indentation + Regex.Replace(frame.Url, @":\d{4}", ":"); + if (!string.IsNullOrEmpty(frame.Name)) + { + description += $" ({frame.Name})"; + } + var result = new List() { description }; foreach (var child in frame.ChildFrames) { - result += "\n" + DumpFrames(child, " " + indentation); + result.AddRange(DumpFrames(child, " " + indentation)); } return result; diff --git a/lib/PuppeteerSharp.Tests/InputTests/InputTests.cs b/lib/PuppeteerSharp.Tests/InputTests/InputTests.cs index 8852b5523..0c2438420 100644 --- a/lib/PuppeteerSharp.Tests/InputTests/InputTests.cs +++ b/lib/PuppeteerSharp.Tests/InputTests/InputTests.cs @@ -10,7 +10,6 @@ namespace PuppeteerSharp.Tests.InputTests [Collection("PuppeteerLoaderFixture collection")] public class InputTests : PuppeteerPageBaseTest { - private Task dummy; private const string Dimensions = @"function dimensions() { const rect = document.querySelector('textarea').getBoundingClientRect(); return { diff --git a/lib/PuppeteerSharp.Tests/PageTests/EvaluateTests.cs b/lib/PuppeteerSharp.Tests/PageTests/EvaluateTests.cs index d4306d230..5ed6642ad 100644 --- a/lib/PuppeteerSharp.Tests/PageTests/EvaluateTests.cs +++ b/lib/PuppeteerSharp.Tests/PageTests/EvaluateTests.cs @@ -125,10 +125,7 @@ public async Task ShouldProperlySerializeNullFields() [Fact] public async Task ShouldReturnNullForNonSerializableObjects() - { - Assert.Null(await Page.EvaluateFunctionAsync("() => window")); - Assert.Null(await Page.EvaluateFunctionAsync("() => [Symbol('foo4')]")); - } + => Assert.Null(await Page.EvaluateFunctionAsync("() => window")); [Fact] public async Task ShouldAcceptElementHandleAsAnArgument() @@ -258,15 +255,8 @@ public async Task ShouldFailForCircularObject() } [Fact] - public Task ShouldSimulateAUserGesture() - => Page.EvaluateExpressionAsync(@"( - function playAudio() - { - const audio = document.createElement('audio'); - audio.src = 'data:audio/wav;base64,UklGRiQAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQAAAAA='; - // This returns a promise which throws if it was not triggered by a user gesture. - return audio.play(); - })()"); + public async Task ShouldSimulateAUserGesture() + => Assert.True(await Page.EvaluateFunctionAsync("() => document.execCommand('copy')")); [Fact] public async Task ShouldThrowANiceErrorAfterANavigation() diff --git a/lib/PuppeteerSharp.Tests/PageTests/TitleTests.cs b/lib/PuppeteerSharp.Tests/PageTests/TitleTests.cs index ee65952d7..04cad04a4 100644 --- a/lib/PuppeteerSharp.Tests/PageTests/TitleTests.cs +++ b/lib/PuppeteerSharp.Tests/PageTests/TitleTests.cs @@ -14,8 +14,8 @@ public TitleTests(ITestOutputHelper output) : base(output) [Fact] public async Task ShouldReturnThePageTitle() { - await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html"); - Assert.Equal("Button test", await Page.GetTitleAsync()); + await Page.GoToAsync(TestConstants.ServerUrl + "/title.html"); + Assert.Equal("Woof-Woof", await Page.GetTitleAsync()); } } } diff --git a/lib/PuppeteerSharp.Tests/PuppeteerTests/PuppeteerConnectTests.cs b/lib/PuppeteerSharp.Tests/PuppeteerTests/PuppeteerConnectTests.cs index bfc573f9b..3ad237540 100644 --- a/lib/PuppeteerSharp.Tests/PuppeteerTests/PuppeteerConnectTests.cs +++ b/lib/PuppeteerSharp.Tests/PuppeteerTests/PuppeteerConnectTests.cs @@ -89,7 +89,7 @@ public async Task ShouldBeAbleToReconnectToADisconnectedBrowser() var restoredPage = pages.FirstOrDefault(x => x.Url == url); Assert.NotNull(restoredPage); var frameDump = FrameUtils.DumpFrames(restoredPage.MainFrame); - Assert.Equal(TestUtils.CompressText(TestConstants.NestedFramesDumpResult), TestUtils.CompressText(frameDump)); + Assert.Equal(TestConstants.NestedFramesDumpResult, frameDump); var response = await restoredPage.EvaluateExpressionAsync("7 * 8"); Assert.Equal(56, response); } diff --git a/lib/PuppeteerSharp.Tests/TestConstants.cs b/lib/PuppeteerSharp.Tests/TestConstants.cs index bbc7629f9..4e570706b 100644 --- a/lib/PuppeteerSharp.Tests/TestConstants.cs +++ b/lib/PuppeteerSharp.Tests/TestConstants.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using Microsoft.Extensions.Logging; @@ -26,11 +27,14 @@ public static class TestConstants public static ILoggerFactory LoggerFactory { get; private set; } - public static readonly string NestedFramesDumpResult = @"http://localhost:/frames/nested-frames.html - http://localhost:/frames/two-frames.html - http://localhost:/frames/frame.html - http://localhost:/frames/frame.html - http://localhost:/frames/frame.html"; + public static readonly IEnumerable NestedFramesDumpResult = new List() + { + "http://localhost:/frames/nested-frames.html", + " http://localhost:/frames/two-frames.html (2frames)", + " http://localhost:/frames/frame.html (uno)", + " http://localhost:/frames/frame.html (dos)", + " http://localhost:/frames/frame.html (aframe)" + }; public static LaunchOptions DefaultBrowserOptions() => new LaunchOptions { diff --git a/lib/PuppeteerSharp/FrameTree.cs b/lib/PuppeteerSharp/FrameTree.cs index 093df4525..947ff853d 100644 --- a/lib/PuppeteerSharp/FrameTree.cs +++ b/lib/PuppeteerSharp/FrameTree.cs @@ -47,6 +47,7 @@ private void LoadChilds(FrameTree frame, PageGetFrameTreeItem frameTree) Frame = new FramePayload { Id = childFrame.Id, + Name = childFrame.Name, ParentId = childFrame.ParentId, Url = childFrame.Url }