forked from ghasctl/puppeteer-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetContentTests.cs
156 lines (131 loc) · 5.92 KB
/
SetContentTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Threading.Tasks;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;
namespace PuppeteerSharp.Tests.PageTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class SetContentTests : PuppeteerPageBaseTest
{
const string ExpectedOutput = "<html><head></head><body><div>hello</div></body></html>";
public SetContentTests(ITestOutputHelper output) : base(output)
{
}
async Task Usage(IBrowser browser)
{
#region SetContentAsync
await using var page = await browser.NewPageAsync();
await page.SetContentAsync("<div>My Receipt</div>");
var result = await page.GetContentAsync();
#endregion
}
[PuppeteerTest("page.spec.ts", "Page.setContent", "should work")]
[PuppeteerFact]
public async Task ShouldWork()
{
await Page.SetContentAsync("<div>hello</div>");
var result = await Page.GetContentAsync();
Assert.Equal(ExpectedOutput, result);
}
[PuppeteerTest("page.spec.ts", "Page.setContent", "should work with doctype")]
[PuppeteerFact]
public async Task ShouldWorkWithDoctype()
{
const string doctype = "<!DOCTYPE html>";
await Page.SetContentAsync($"{doctype}<div>hello</div>");
var result = await Page.GetContentAsync();
Assert.Equal($"{doctype}{ExpectedOutput}", result);
}
[PuppeteerTest("page.spec.ts", "Page.setContent", "should work with HTML 4 doctype")]
[PuppeteerFact]
public async Task ShouldWorkWithHtml4Doctype()
{
const string doctype = "<!DOCTYPE html PUBLIC \" -//W3C//DTD HTML 4.01//EN\" " +
"\"http://www.w3.org/TR/html4/strict.dtd\">";
await Page.SetContentAsync($"{doctype}<div>hello</div>");
var result = await Page.GetContentAsync();
Assert.Equal($"{doctype}{ExpectedOutput}", result);
}
[PuppeteerTest("page.spec.ts", "Page.setContent", "should respect timeout")]
[PuppeteerFact]
public async Task ShouldRespectTimeout()
{
const string imgPath = "/img.png";
Server.SetRoute(imgPath, _ => Task.Delay(-1));
await Page.GoToAsync(TestConstants.EmptyPage);
var exception = await Assert.ThrowsAnyAsync<TimeoutException>(async () =>
await Page.SetContentAsync($"<img src='{TestConstants.ServerUrl + imgPath}'></img>", new NavigationOptions
{
Timeout = 1
}));
Assert.Contains("Timeout of 1 ms exceeded", exception.Message);
}
[PuppeteerTest("page.spec.ts", "Page.setContent", "should respect default navigation timeout")]
[PuppeteerFact]
public async Task ShouldRespectDefaultTimeout()
{
const string imgPath = "/img.png";
Server.SetRoute(imgPath, _ => Task.Delay(-1));
await Page.GoToAsync(TestConstants.EmptyPage);
Page.DefaultTimeout = 1;
var exception = await Assert.ThrowsAnyAsync<TimeoutException>(async () =>
await Page.SetContentAsync($"<img src='{TestConstants.ServerUrl + imgPath}'></img>"));
Assert.Contains("Timeout of 1 ms exceeded", exception.Message);
}
[PuppeteerTest("page.spec.ts", "Page.setContent", "should await resources to load")]
[PuppeteerFact]
public async Task ShouldAwaitResourcesToLoad()
{
var imgPath = "/img.png";
var imgResponse = new TaskCompletionSource<bool>();
Server.SetRoute(imgPath, _ => imgResponse.Task);
var loaded = false;
var waitTask = Server.WaitForRequest(imgPath);
var contentTask = Page.SetContentAsync($"<img src=\"{TestConstants.ServerUrl + imgPath}\"></img>")
.ContinueWith(_ => loaded = true);
await waitTask;
Assert.False(loaded);
imgResponse.SetResult(true);
await contentTask;
}
[PuppeteerTest("page.spec.ts", "Page.setContent", "should work fast enough")]
[PuppeteerFact]
public async Task ShouldWorkFastEnough()
{
for (var i = 0; i < 20; ++i)
{
await Page.SetContentAsync("<div>yo</div>");
}
}
[PuppeteerTest("page.spec.ts", "Page.setContent", "should work with tricky content")]
[PuppeteerFact]
public async Task ShouldWorkWithTrickyContent()
{
await Page.SetContentAsync("<div>hello world</div>\x7F");
Assert.Equal("hello world", await Page.QuerySelectorAsync("div").EvaluateFunctionAsync<string>("div => div.textContent"));
}
[PuppeteerTest("page.spec.ts", "Page.setContent", "should work with accents")]
[PuppeteerFact]
public async Task ShouldWorkWithAccents()
{
await Page.SetContentAsync("<div>aberración</div>");
Assert.Equal("aberración", await Page.QuerySelectorAsync("div").EvaluateFunctionAsync<string>("div => div.textContent"));
}
[PuppeteerTest("page.spec.ts", "Page.setContent", "should work with emojis")]
[PuppeteerFact]
public async Task ShouldWorkWithEmojis()
{
await Page.SetContentAsync("<div>🐥</div>");
Assert.Equal("🐥", await Page.QuerySelectorAsync("div").EvaluateFunctionAsync<string>("div => div.textContent"));
}
[PuppeteerTest("page.spec.ts", "Page.setContent", "should work with newline")]
[PuppeteerFact]
public async Task ShouldWorkWithNewline()
{
await Page.SetContentAsync("<div>\n</div>");
Assert.Equal("\n", await Page.QuerySelectorAsync("div").EvaluateFunctionAsync<string>("div => div.textContent"));
}
}
}