We have a WPF application that loads some content in a <WebBrowser/>
control and then makes some calls based on what was loaded. With the right mocks, we think we can test this inside a displayless unit test (NUnit in this case). But the WebBrowser
control doesn't want to play nicely.
The problem is that we never receive the LoadCompleted
or Navigated
events. Apparently this is because a web-page is never "Loaded" until it is actually rendered (see this MSDN thread). We do receive the Navigating
event, but that comes far too early for our purposes.
So is there a way to make the WebBrowser
control work "fully" even when it has no output to display to?
Here is a cut-down version of the test case:
[TestFixture, RequiresSTA]
class TestIsoView
{
[Test] public void PageLoadsAtAll()
{
Console.WriteLine("I'm a lumberjack and I'm OK");
WebBrowser wb = new WebBrowser();
// An AutoResetEvent allows us to synchronously wait for an event to occur.
AutoResetEvent autoResetEvent = new AutoResetEvent(false);
//wb.LoadCompleted += delegate // LoadCompleted is never received
wb.Navigated += delegate // Navigated is never received
//wb.Navigating += delegate // Navigating *is* received
{
// We never get here unless we wait on wb.Navigating
Console.WriteLine("The document loaded!!");
autoResetEvent.Set();
};
Console.WriteLine("Registered signal handler", "Navigating");
wb.NavigateToString("Here be dramas");
Console.WriteLine("Asyncronous Navigations started! Waiting for A.R.E.");
autoResetEvent.WaitOne();
// TEST HANGS BEFORE REACHING HERE.
Console.WriteLine("Got it!");
}
}