|
| 1 | +using Xunit.Abstractions; |
| 2 | +using Xunit; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using CefSharp.OffScreen; |
| 5 | +using CefSharp.Example; |
| 6 | +using Nito.AsyncEx; |
| 7 | +using System; |
| 8 | +using System.Threading; |
| 9 | + |
| 10 | +namespace CefSharp.Test.Navigation |
| 11 | +{ |
| 12 | + //NOTE: All Test classes must be part of this collection as it manages the Cef Initialize/Shutdown lifecycle |
| 13 | + [Collection(CefSharpFixtureCollection.Key)] |
| 14 | + public class WaitForNavigationAsyncTests |
| 15 | + { |
| 16 | + |
| 17 | + private readonly ITestOutputHelper output; |
| 18 | + private readonly CefSharpFixture fixture; |
| 19 | + |
| 20 | + public WaitForNavigationAsyncTests(ITestOutputHelper output, CefSharpFixture fixture) |
| 21 | + { |
| 22 | + this.fixture = fixture; |
| 23 | + this.output = output; |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public async Task CanWork() |
| 28 | + { |
| 29 | + const string expected = CefExample.HelloWorldUrl; |
| 30 | + |
| 31 | + using (var browser = new ChromiumWebBrowser(CefExample.DefaultUrl)) |
| 32 | + { |
| 33 | + var response = await browser.WaitForInitialLoadAsync(); |
| 34 | + |
| 35 | + Assert.True(response.Success); |
| 36 | + |
| 37 | + var navigationTask = browser.WaitForNavigationAsync(); |
| 38 | + var evaluateTask = browser.EvaluateScriptAsync($"window.location.href = '{expected}';"); |
| 39 | + |
| 40 | + await Task.WhenAll(navigationTask, evaluateTask); |
| 41 | + |
| 42 | + var navigationResponse = navigationTask.Result; |
| 43 | + |
| 44 | + var mainFrame = browser.GetMainFrame(); |
| 45 | + Assert.True(mainFrame.IsValid); |
| 46 | + Assert.Equal(expected, mainFrame.Url); |
| 47 | + Assert.Equal(200, navigationResponse.HttpStatusCode); |
| 48 | + |
| 49 | + output.WriteLine("Url {0}", mainFrame.Url); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public async Task CanWaitForInvalidDomain() |
| 55 | + { |
| 56 | + const string expected = "https://notfound.cefsharp.test"; |
| 57 | + using (var browser = new ChromiumWebBrowser(CefExample.DefaultUrl)) |
| 58 | + { |
| 59 | + var response = await browser.WaitForInitialLoadAsync(); |
| 60 | + |
| 61 | + Assert.True(response.Success); |
| 62 | + |
| 63 | + var navigationTask = browser.WaitForNavigationAsync(); |
| 64 | + var evaluateTask = browser.EvaluateScriptAsync($"window.location.href = '{expected}';"); |
| 65 | + |
| 66 | + await Task.WhenAll(navigationTask, evaluateTask); |
| 67 | + |
| 68 | + var navigationResponse = navigationTask.Result; |
| 69 | + |
| 70 | + var mainFrame = browser.GetMainFrame(); |
| 71 | + Assert.True(mainFrame.IsValid); |
| 72 | + Assert.False(navigationResponse.Success); |
| 73 | + Assert.Contains(expected, mainFrame.Url); |
| 74 | + Assert.Equal(CefErrorCode.NameNotResolved, navigationResponse.ErrorCode); |
| 75 | + |
| 76 | + output.WriteLine("Url {0}", mainFrame.Url); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public async Task CanTimeout() |
| 82 | + { |
| 83 | + const string expected = "The operation has timed out."; |
| 84 | + |
| 85 | + using (var browser = new ChromiumWebBrowser(CefExample.DefaultUrl)) |
| 86 | + { |
| 87 | + var response = await browser.WaitForInitialLoadAsync(); |
| 88 | + |
| 89 | + Assert.True(response.Success); |
| 90 | + |
| 91 | + var exception = await Assert.ThrowsAnyAsync<TimeoutException>(async () => |
| 92 | + { |
| 93 | + await browser.WaitForNavigationAsync(timeout:TimeSpan.FromMilliseconds(100)); |
| 94 | + }); |
| 95 | + |
| 96 | + Assert.Contains(expected, exception.Message); |
| 97 | + |
| 98 | + output.WriteLine("Exception {0}", exception.Message); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public async Task CanCancel() |
| 104 | + { |
| 105 | + const string expected = "A task was canceled."; |
| 106 | + |
| 107 | + using (var browser = new ChromiumWebBrowser(CefExample.DefaultUrl)) |
| 108 | + { |
| 109 | + var response = await browser.WaitForInitialLoadAsync(); |
| 110 | + |
| 111 | + var cancellationTokenSource = new CancellationTokenSource(); |
| 112 | + cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(1)); |
| 113 | + |
| 114 | + Assert.True(response.Success); |
| 115 | + |
| 116 | + var exception = await Assert.ThrowsAnyAsync<TaskCanceledException>(async () => |
| 117 | + { |
| 118 | + await browser.WaitForNavigationAsync(cancellationToken: cancellationTokenSource.Token); |
| 119 | + }); |
| 120 | + |
| 121 | + Assert.Contains(expected, exception.Message); |
| 122 | + |
| 123 | + output.WriteLine("Exception {0}", exception.Message); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments