-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
SetContentTests.cs
109 lines (91 loc) · 4.4 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
using System.Threading.Tasks;
using Xunit.Abstractions;
using Xunit;
using WebView2.DevTools.Dom.Tests.Attributes;
using Microsoft.Web.WebView2.Core;
namespace WebView2.DevTools.Dom.Tests.DevToolsContextTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class SetContentTests : DevTooolsContextBaseTest
{
const string ExpectedOutput = "<html><head></head><body><div>hello</div></body></html>";
public SetContentTests(ITestOutputHelper output) : base(output)
{
}
#pragma warning disable IDE0051 // Remove unused private members
async Task Usage(CoreWebView2 coreWebView2)
#pragma warning restore IDE0051 // Remove unused private members
{
#region SetContentAsync
// Add using WebView2.DevTools.Dom; to get access to the
// CreateDevToolsContextAsync extension method
// WebView2DevToolsContext implements IAsyncDisposable and can be Disposed
// via await using or await devToolsContext.DisposeAsync();
// Only DisposeAsync is supported. It's very important the WebView2DevToolsContext is Disposed
// When you have finished. Only create a single instance at a time, reuse an instance rather than
// creaeting a new WebView2DevToolsContext. Dispose the old WebView2DevToolsContext instance before
// creating a new instance if you need to manage the lifespan manually.
// https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync#using-async-disposable
await using var devtoolsContext = await coreWebView2.CreateDevToolsContextAsync();
await devtoolsContext.SetContentAsync("<div>My Receipt</div>");
var result = await devtoolsContext.GetContentAsync();
#endregion
}
[WebView2ContextFact]
public async Task ShouldWork()
{
await DevToolsContext.SetContentAsync("<div>hello</div>");
var result = await DevToolsContext.GetContentAsync();
Assert.Equal(ExpectedOutput, result);
}
[WebView2ContextFact]
public async Task ShouldWorkWithDoctype()
{
const string doctype = "<!DOCTYPE html>";
await DevToolsContext.SetContentAsync($"{doctype}<div>hello</div>");
var result = await DevToolsContext.GetContentAsync();
Assert.Equal($"{doctype}{ExpectedOutput}", result);
}
[WebView2ContextFact]
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 DevToolsContext.SetContentAsync($"{doctype}<div>hello</div>");
var result = await DevToolsContext.GetContentAsync();
Assert.Equal($"{doctype}{ExpectedOutput}", result);
}
[WebView2ContextFact]
public async Task ShouldWorkFastEnough()
{
for (var i = 0; i < 20; ++i)
{
await DevToolsContext.SetContentAsync("<div>yo</div>");
}
}
[WebView2ContextFact]
public async Task ShouldWorkWithTrickyContent()
{
await DevToolsContext.SetContentAsync("<div>hello world</div>\x7F");
Assert.Equal("hello world", await DevToolsContext.QuerySelectorAsync("div").EvaluateFunctionAsync<string>("div => div.textContent"));
}
[WebView2ContextFact]
public async Task ShouldWorkWithAccents()
{
await DevToolsContext.SetContentAsync("<div>aberración</div>");
Assert.Equal("aberración", await DevToolsContext.QuerySelectorAsync("div").EvaluateFunctionAsync<string>("div => div.textContent"));
}
[WebView2ContextFact]
public async Task ShouldWorkWithEmojis()
{
await DevToolsContext.SetContentAsync("<div>🐥</div>");
Assert.Equal("🐥", await DevToolsContext.QuerySelectorAsync("div").EvaluateFunctionAsync<string>("div => div.textContent"));
}
[WebView2ContextFact]
public async Task ShouldWorkWithNewline()
{
await DevToolsContext.SetContentAsync("<div>\n</div>");
Assert.Equal("\n", await DevToolsContext.QuerySelectorAsync("div").EvaluateFunctionAsync<string>("div => div.textContent"));
}
}
}