/
WaitForXPathTests.cs
109 lines (97 loc) · 4.59 KB
/
WaitForXPathTests.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;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace PuppeteerSharp.Tests.FrameTests
{
[Collection("PuppeteerLoaderFixture collection")]
public class WaitForXPathTests : PuppeteerPageBaseTest
{
const string addElement = "tag => document.body.appendChild(document.createElement(tag))";
public WaitForXPathTests(ITestOutputHelper output) : base(output)
{
}
[Fact]
public async Task ShouldSupportSomeFancyXpath()
{
await Page.SetContentAsync("<p>red herring</p><p>hello world </p>");
var waitForXPath = Page.WaitForXPathAsync("//p[normalize-space(.)=\"hello world\"]");
Assert.Equal("hello world ", await Page.EvaluateFunctionAsync<string>("x => x.textContent", await waitForXPath));
}
[Fact]
public async Task ShouldRunInSpecifiedFrame()
{
await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);
await FrameUtils.AttachFrameAsync(Page, "frame2", TestConstants.EmptyPage);
var frame1 = Page.Frames[1];
var frame2 = Page.Frames[2];
var waitForXPathPromise = frame2.WaitForXPathAsync("//div");
await frame1.EvaluateFunctionAsync(addElement, "div");
await frame2.EvaluateFunctionAsync(addElement, "div");
var eHandle = await waitForXPathPromise;
Assert.Equal(frame2, eHandle.ExecutionContext.Frame);
}
[Fact]
public async Task ShouldThrowIfEvaluationFailed()
{
await Page.EvaluateOnNewDocumentAsync(@"function() {
document.evaluate = null;
}");
await Page.GoToAsync(TestConstants.EmptyPage);
var exception = await Assert.ThrowsAsync<EvaluationFailedException>(()
=> Page.WaitForXPathAsync("*"));
Assert.Contains("document.evaluate is not a function", exception.Message);
}
[Fact]
public async Task ShouldThrowWhenFrameIsDetached()
{
await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);
var frame = Page.Frames[1];
var waitPromise = frame.WaitForXPathAsync("//*[@class=\"box\"]");
await FrameUtils.DetachFrameAsync(Page, "frame1");
var exception = await Assert.ThrowsAnyAsync<Exception>(() => waitPromise);
Assert.Contains("waitForFunction failed: frame got detached.", exception.Message);
}
[Fact]
public async Task HiddenShouldWaitForDisplayNone()
{
var divHidden = false;
await Page.SetContentAsync("<div style='display: block;'></div>");
var waitForXPath = Page.WaitForXPathAsync("//div", new WaitForSelectorOptions { Hidden = true })
.ContinueWith(_ => divHidden = true);
await Page.WaitForXPathAsync("//div"); // do a round trip
Assert.False(divHidden);
await Page.EvaluateExpressionAsync("document.querySelector('div').style.setProperty('display', 'none')");
Assert.True(await waitForXPath);
Assert.True(divHidden);
}
[Fact]
public async Task ShouldReturnTheElementHandle()
{
var waitForXPath = Page.WaitForXPathAsync("//*[@class=\"zombo\"]");
await Page.SetContentAsync("<div class='zombo'>anything</div>");
Assert.Equal("anything", await Page.EvaluateFunctionAsync<string>("x => x.textContent", await waitForXPath));
}
[Fact]
public async Task ShouldAllowYouToSelectATextNode()
{
await Page.SetContentAsync("<div>some text</div>");
var text = await Page.WaitForXPathAsync("//div/text()");
Assert.Equal(3 /* Node.TEXT_NODE */, await (await text.GetPropertyAsync("nodeType")).JsonValueAsync<int>());
}
[Fact]
public async Task ShouldAllowYouToSelectAnElementWithSingleSlash()
{
await Page.SetContentAsync("<div>some text</div>");
var waitForXPath = Page.WaitForXPathAsync("/html/body/div");
Assert.Equal("some text", await Page.EvaluateFunctionAsync<string>("x => x.textContent", await waitForXPath));
}
[Fact]
public async Task ShouldRespectTimeout()
{
var exception = await Assert.ThrowsAsync<WaitTaskTimeoutException>(()
=> Page.WaitForXPathAsync("//div", new WaitForSelectorOptions { Timeout = 10 }));
Assert.Contains("waiting for XPath '//div' failed: timeout", exception.Message);
}
}
}