|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace PuppeteerSharp.Tests.Frame |
| 6 | +{ |
| 7 | + [Collection("PuppeteerLoaderFixture collection")] |
| 8 | + public class WaitForFunctionTests : PuppeteerPageBaseTest |
| 9 | + { |
| 10 | + [Fact] |
| 11 | + public async Task ShouldPollOnInterval() |
| 12 | + { |
| 13 | + var success = false; |
| 14 | + var startTime = DateTime.Now; |
| 15 | + var polling = 100; |
| 16 | + var watchdog = Page.WaitForFunctionAsync("() => window.__FOO === 'hit'", new WaitForFunctionOptions { PollingInterval = polling }) |
| 17 | + .ContinueWith(_ => success = true); |
| 18 | + await Page.EvaluateExpressionAsync("window.__FOO = 'hit'"); |
| 19 | + Assert.False(success); |
| 20 | + await Page.EvaluateExpressionAsync("document.body.appendChild(document.createElement('div'))"); |
| 21 | + await watchdog; |
| 22 | + Assert.True((DateTime.Now - startTime).TotalMilliseconds > polling / 2); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public async Task ShouldPollOnMutation() |
| 27 | + { |
| 28 | + var success = false; |
| 29 | + var watchdog = Page.WaitForFunctionAsync("() => window.__FOO === 'hit'", |
| 30 | + new WaitForFunctionOptions { Polling = WaitForFunctionPollingOption.Mutation }) |
| 31 | + .ContinueWith(_ => success = true); |
| 32 | + await Page.EvaluateExpressionAsync("window.__FOO = 'hit'"); |
| 33 | + Assert.False(success); |
| 34 | + await Page.EvaluateExpressionAsync("document.body.appendChild(document.createElement('div'))"); |
| 35 | + await watchdog; |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public async Task ShouldPollOnRaf() |
| 40 | + { |
| 41 | + var watchdog = Page.WaitForFunctionAsync("() => window.__FOO === 'hit'", |
| 42 | + new WaitForFunctionOptions { Polling = WaitForFunctionPollingOption.Raf }); |
| 43 | + await Page.EvaluateExpressionAsync("window.__FOO = 'hit'"); |
| 44 | + await watchdog; |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public async Task ShouldThrowNegativePollingInterval() |
| 49 | + { |
| 50 | + var exception = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() |
| 51 | + => Page.WaitForFunctionAsync("() => !!document.body", new WaitForFunctionOptions { PollingInterval = -10 })); |
| 52 | + |
| 53 | + Assert.Contains("Cannot poll with non-positive interval", exception.Message); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public async Task ShouldReturnTheSuccessValueAsAJSHandle() |
| 58 | + { |
| 59 | + Assert.Equal(5, await (await Page.WaitForFunctionAsync("() => 5")).JsonValue<int>()); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public async Task ShouldReturnTheWindowAsASuccessValue() |
| 64 | + { |
| 65 | + Assert.NotNull(await Page.WaitForFunctionAsync("() => window")); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public async Task ShouldAcceptElementHandleArguments() |
| 70 | + { |
| 71 | + await Page.SetContentAsync("<div></div>"); |
| 72 | + var div = await Page.GetElementAsync("div"); |
| 73 | + var resolved = false; |
| 74 | + var waitForFunction = Page.WaitForFunctionAsync("element => !element.parentElement", div) |
| 75 | + .ContinueWith(_ => resolved = true); |
| 76 | + Assert.False(resolved); |
| 77 | + await Page.EvaluateFunctionAsync("element => element.remove()", div); |
| 78 | + await waitForFunction; |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments