forked from hardkoded/puppeteer-sharp
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ElementHandleQuerySelectorEvalTests.cs
66 lines (61 loc) · 3.1 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
using System;
using System.Threading.Tasks;
using CefSharp;
using CefSharp.Dom;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;
namespace PuppeteerSharp.Tests.QuerySelectorTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class ElementHandleQuerySelectorEvalTests : DevToolsContextBaseTest
{
public ElementHandleQuerySelectorEvalTests(ITestOutputHelper output) : base(output)
{
}
async Task Usage(IWebBrowser chromiumWebBrowser)
{
#region Evaluate
await using var devtoolsContext = await chromiumWebBrowser.CreateDevToolsContextAsync();
var seven = await devtoolsContext.EvaluateExpressionAsync<int>("4 + 3");
var someObject = await devtoolsContext.EvaluateFunctionAsync<dynamic>("(value) => ({a: value})", 5);
Console.WriteLine(someObject.a);
#endregion
}
[PuppeteerTest("queryselector.spec.ts", "ElementHandle.$eval", "should work")]
[PuppeteerFact]
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")
.AndThen(x => x.EvaluateFunctionAsync<string>("node => node.innerText"));
Assert.Equal("100", content);
}
[PuppeteerTest("queryselector.spec.ts", "ElementHandle.$eval", "should retrieve content from subtree")]
[PuppeteerFact]
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<HtmlElement>(".a")
.AndThen(x => x.EvaluateFunctionAsync<string>("node => node.innerText"));
Assert.Equal("a-child-div", content);
}
[PuppeteerTest("queryselector.spec.ts", "ElementHandle.$eval", "should throw in case of missing selector")]
[PuppeteerFact]
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<HtmlDivElement>("#myId");
var exception = await Assert.ThrowsAsync<NullReferenceException>(
() => elementHandle.QuerySelectorAsync<HtmlElement>(".a")
.AndThen(x => x.GetInnerTextAsync())
);
Assert.Equal("Object reference not set to an instance of an object.", exception.Message);
}
}
}