-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ElementHandleQuerySelectorEvalTests.cs
71 lines (63 loc) · 3.53 KB
/
ElementHandleQuerySelectorEvalTests.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
using System.Threading.Tasks;
using WebView2.DevTools.Dom.Tests.Attributes;
using Xunit;
using Xunit.Abstractions;
namespace WebView2.DevTools.Dom.Tests.QuerySelectorTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class ElementHandleQuerySelectorEvalTests : DevTooolsContextBaseTest
{
public ElementHandleQuerySelectorEvalTests(ITestOutputHelper output) : base(output)
{
}
async Task Usage(Microsoft.Web.WebView2.WinForms.WebView2 webView2Browser)
{
#region Evaluate
// Add using WebView2.DevTools.Dom; to get access to the
// CreateDevToolsContextAsync extension method
await webView2Browser.EnsureCoreWebView2Async();
// WebView2DevToolsContext implements IAsyncDisposable and can be Disposed
// via await using or await devToolsContext.DisposeAsync();
// https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync#using-async-disposable
await using var devToolsContext = await webView2Browser.CoreWebView2.CreateDevToolsContextAsync();
await devToolsContext.IgnoreCertificateErrorsAsync(true);
var seven = await devToolsContext.EvaluateExpressionAsync<int>("4 + 3");
// Can evaluate a function that returns a Promise
var fourtyTwo = await devToolsContext.EvaluateFunctionAsync<int>("() => Promise.resolve(42)");
// Pass in arguments to a function, including references to HtmlElements and JavascriptHandles
var someObject = await devToolsContext.EvaluateFunctionAsync<dynamic>("(value) => ({a: value})", 5);
System.Console.WriteLine(someObject.a);
#endregion
}
[WebView2ContextFact]
public async Task QuerySelectorShouldWork()
{
await DevToolsContext.SetContentAsync("<html><body><div class='tweet'><div class='like'>100</div><div class='retweets'>10</div></div></body></html>");
var tweet = await DevToolsContext.QuerySelectorAsync<HtmlDivElement>(".tweet");
var content = await tweet.QuerySelectorAsync(".like")
.EvaluateFunctionAsync<string>("node => node.innerText");
Assert.Equal("100", content);
}
[WebView2ContextFact]
public async Task QuerySelectorShouldRetrieveContentFromSubtree()
{
var htmlContent = "<div class='a'>not-a-child-div</div><div id='myId'><div class='a'>a-child-div</div></div>";
await DevToolsContext.SetContentAsync(htmlContent);
var elementHandle = await DevToolsContext.QuerySelectorAsync<HtmlDivElement>("#myId");
var content = await elementHandle.QuerySelectorAsync(".a")
.EvaluateFunctionAsync<string>("node => node.innerText");
Assert.Equal("a-child-div", content);
}
[WebView2ContextFact]
public async Task QuerySelectorShouldThrowInCaseOfMissingSelector()
{
var htmlContent = "<div class=\"a\">not-a-child-div</div><div id=\"myId\"></div>";
await DevToolsContext.SetContentAsync(htmlContent);
var elementHandle = await DevToolsContext.QuerySelectorAsync("#myId");
var exception = await Assert.ThrowsAsync<WebView2DevToolsSelectorException>(
() => elementHandle.QuerySelectorAsync(".a").EvaluateFunctionAsync<string>("node => node.innerText")
);
Assert.Equal("Error: failed to find element matching selector", exception.Message);
}
}
}