forked from cefsharp/CefSharp
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebBrowserTestExtensions.cs
154 lines (120 loc) · 5.6 KB
/
WebBrowserTestExtensions.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright © 2017 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Reflection;
using System.Threading.Tasks;
using CefSharp.OffScreen;
using Xunit;
namespace CefSharp.Test
{
public static class WebBrowserTestExtensions
{
public static int PaintEventHandlerCount(this CefSharp.Wpf.ChromiumWebBrowser browser)
{
var field = typeof(CefSharp.Wpf.ChromiumWebBrowser).GetField("Paint", BindingFlags.NonPublic | BindingFlags.Instance);
if(field == null)
{
throw new Exception("Unable to obtain Paint event handler");
}
var handler = field.GetValue(browser) as Delegate;
if (handler == null)
{
return 0;
}
var subscribers = handler.GetInvocationList();
return subscribers.Length;
}
public static int PaintEventHandlerCount(this ChromiumWebBrowser browser)
{
var field = typeof(ChromiumWebBrowser).GetField("Paint", BindingFlags.NonPublic | BindingFlags.Instance);
if (field == null)
{
throw new Exception("Unable to obtain Paint event handler");
}
var handler = field.GetValue(browser) as Delegate;
if (handler == null)
{
return 0;
}
var subscribers = handler.GetInvocationList();
return subscribers.Length;
}
public static Task<LoadUrlAsyncResponse> LoadRequestAsync(this IWebBrowser browser, IRequest request)
{
if(request == null)
{
throw new ArgumentNullException("request");
}
//If using .Net 4.6 then use TaskCreationOptions.RunContinuationsAsynchronously
//and switch to tcs.TrySetResult below - no need for the custom extension method
var tcs = new TaskCompletionSource<LoadUrlAsyncResponse>(TaskCreationOptions.RunContinuationsAsynchronously);
EventHandler<LoadErrorEventArgs> loadErrorHandler = null;
EventHandler<LoadingStateChangedEventArgs> loadingStateChangeHandler = null;
loadErrorHandler = (sender, args) =>
{
//Ignore Aborted
//Currently invalid SSL certificates which aren't explicitly allowed
//end up with CefErrorCode.Aborted, I've created the following PR
//in the hopes of getting this fixed.
//https://bitbucket.org/chromiumembedded/cef/pull-requests/373
if (args.ErrorCode == CefErrorCode.Aborted)
{
return;
}
//If LoadError was called then we'll remove both our handlers
//as we won't need to capture LoadingStateChanged, we know there
//was an error
browser.LoadError -= loadErrorHandler;
browser.LoadingStateChanged -= loadingStateChangeHandler;
tcs.TrySetResult(new LoadUrlAsyncResponse(args.ErrorCode, -1));
};
loadingStateChangeHandler = (sender, args) =>
{
//Wait for while page to finish loading not just the first frame
if (!args.IsLoading)
{
var host = args.Browser.GetHost();
var navEntry = host?.GetVisibleNavigationEntry();
int statusCode = navEntry?.HttpStatusCode ?? -1;
//By default 0 is some sort of error, we map that to -1
//so that it's clearer that something failed.
if (statusCode == 0)
{
statusCode = -1;
}
browser.LoadingStateChanged -= loadingStateChangeHandler;
//This is required when using a standard TaskCompletionSource
//Extension method found in the CefSharp.Internals namespace
tcs.TrySetResult(new LoadUrlAsyncResponse(CefErrorCode.None, statusCode));
}
};
browser.LoadingStateChanged += loadingStateChangeHandler;
browser.GetMainFrame().LoadRequest(request);
return tcs.Task;
}
public static Task<QUnitTestResult> CreateBrowserAndWaitForQUnitTestExeuctionToComplete(this ChromiumWebBrowser browser)
{
//If using .Net 4.6 then use TaskCreationOptions.RunContinuationsAsynchronously
//and switch to tcs.TrySetResult below - no need for the custom extension method
var tcs = new TaskCompletionSource<QUnitTestResult>(TaskCreationOptions.RunContinuationsAsynchronously);
EventHandler<JavascriptMessageReceivedEventArgs> handler = null;
handler = (sender, args) =>
{
dynamic msg = args.Message;
//Wait for while page to finish loading not just the first frame
if (msg.Type == "QUnitExecutionComplete")
{
browser.JavascriptMessageReceived -= handler;
var details = msg.Details;
var total = (int)details.total;
var passed = (int)details.passed;
tcs.TrySetResult(new QUnitTestResult { Passed = passed, Total = total });
}
};
browser.JavascriptMessageReceived += handler;
browser.CreateBrowser();
return tcs.Task;
}
}
}