Skip to content

Commit

Permalink
Test refactors (#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok authored Mar 19, 2019
1 parent 23a6434 commit e71b6e9
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 95 deletions.
49 changes: 24 additions & 25 deletions lib/PuppeteerSharp.TestServer/wwwroot/frames/nested-frames.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
<style>
body {
display: flex;
}

body iframe {
flex-grow: 1;
flex-shrink: 1;
}
::-webkit-scrollbar{
display: none;
}
</style>
<script>
async function attachFrame(frameId, url) {
var frame = document.createElement('iframe');
frame.src = url;
frame.id = frameId;
document.body.appendChild(frame);
await new Promise(x => frame.onload = x);
return 'kazakh';
}
</script>
<iframe src='./two-frames.html'></iframe>
<iframe src='./frame.html'></iframe>
<style>
body {
display: flex;
}

body iframe {
flex-grow: 1;
flex-shrink: 1;
}

::-webkit-scrollbar {
display: none;
}
</style>
<script>async function attachFrame(frameId, url) {
var frame = document.createElement('iframe');
frame.src = url;
frame.id = frameId;
document.body.appendChild(frame);
await new Promise(x => frame.onload = x);
return 'kazakh';
}</script>
<iframe src='./two-frames.html' name='2frames'></iframe>
<iframe src='./frame.html' name='aframe'></iframe>
26 changes: 13 additions & 13 deletions lib/PuppeteerSharp.TestServer/wwwroot/frames/two-frames.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<style>
body {
display: flex;
flex-direction: column;
}

body iframe {
flex-grow: 1;
flex-shrink: 1;
}
</style>
<iframe src='./frame.html'></iframe>
<iframe src='./frame.html'></iframe>
<style>
body {
display: flex;
flex-direction: column;
}

body iframe {
flex-grow: 1;
flex-shrink: 1;
}
</style>
<iframe src='./frame.html' name='uno'></iframe>
<iframe src='./frame.html' name='dos'></iframe>
1 change: 1 addition & 0 deletions lib/PuppeteerSharp.TestServer/wwwroot/title.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<title>Woof-Woof</title>
60 changes: 31 additions & 29 deletions lib/PuppeteerSharp.TestServer/wwwroot/wrappedlink.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
<style>
:root {
font-family: monospace;
}

body {
display: flex;
align-items: center;
justify-content: center;
}

div {
width: 10ch;
word-wrap: break-word;
border: 1px solid blue;
transform: rotate(33deg);
line-height: 8ch;
padding: 2ch;
}

a {
margin-left: 7ch;
}
</style>
<div>
<a href='#clicked'>123321</a>
</div>
<script>
</script>
<style>
:root {
font-family: monospace;
}

body {
display: flex;
align-items: center;
justify-content: center;
}

div {
width: 10ch;
word-wrap: break-word;
border: 1px solid blue;
transform: rotate(33deg);
line-height: 8ch;
padding: 2ch;
}

a {
margin-left: 7ch;
}
</style>
<div>
<a href='#clicked'>123321</a>
</div>
<script>
document.querySelector('a').addEventListener('click', () => {
window.__clicked = true;
});</script>
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp.Tests/FrameTests/FrameManagementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
14 changes: 10 additions & 4 deletions lib/PuppeteerSharp.Tests/FrameUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PuppeteerSharp.Tests
Expand Down Expand Up @@ -26,12 +27,17 @@ await page.EvaluateFunctionAsync(@"function detachFrame(frameId) {
}", frameId);
}

public static string DumpFrames(Frame frame, string indentation = "")
public static IEnumerable<string> DumpFrames(Frame frame, string indentation = "")
{
var result = indentation + Regex.Replace(frame.Url, @":\d{4}", ":<PORT>");
var description = indentation + Regex.Replace(frame.Url, @":\d{4}", ":<PORT>");
if (!string.IsNullOrEmpty(frame.Name))
{
description += $" ({frame.Name})";
}
var result = new List<string>() { description };
foreach (var child in frame.ChildFrames)
{
result += "\n" + DumpFrames(child, " " + indentation);
result.AddRange(DumpFrames(child, " " + indentation));
}

return result;
Expand Down
1 change: 0 additions & 1 deletion lib/PuppeteerSharp.Tests/InputTests/InputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 3 additions & 13 deletions lib/PuppeteerSharp.Tests/PageTests/EvaluateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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<bool>("() => document.execCommand('copy')"));

[Fact]
public async Task ShouldThrowANiceErrorAfterANavigation()
Expand Down
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp.Tests/PageTests/TitleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>("7 * 8");
Assert.Equal(56, response);
}
Expand Down
14 changes: 9 additions & 5 deletions lib/PuppeteerSharp.Tests/TestConstants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -26,11 +27,14 @@ public static class TestConstants

public static ILoggerFactory LoggerFactory { get; private set; }

public static readonly string NestedFramesDumpResult = @"http://localhost:<PORT>/frames/nested-frames.html
http://localhost:<PORT>/frames/two-frames.html
http://localhost:<PORT>/frames/frame.html
http://localhost:<PORT>/frames/frame.html
http://localhost:<PORT>/frames/frame.html";
public static readonly IEnumerable<string> NestedFramesDumpResult = new List<string>()
{
"http://localhost:<PORT>/frames/nested-frames.html",
" http://localhost:<PORT>/frames/two-frames.html (2frames)",
" http://localhost:<PORT>/frames/frame.html (uno)",
" http://localhost:<PORT>/frames/frame.html (dos)",
" http://localhost:<PORT>/frames/frame.html (aframe)"
};

public static LaunchOptions DefaultBrowserOptions() => new LaunchOptions
{
Expand Down
1 change: 1 addition & 0 deletions lib/PuppeteerSharp/FrameTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit e71b6e9

Please sign in to comment.