sapbucket
asked on
AxWebBrowser Control test fixture - InvalidActiveXStateException
Hello,
I'm wondering if you can help me by getting the following test fixture to pass:
//InnerHtmlDownloaderTestF ixture.cs
using System;
using System.Threading;
using AxSHDocVw;
using NUnit.Framework;
using Sidebet.Gui.Tools;
namespace Sidebet.Gui.TestFixtures
{
[TestFixture]
public class InnerHtmlDownloaderTestFix ture
{
private bool _downloadComplete;
private InnerHtmlDownloader downloader;
private string url = "http://www.google.com";
private AxWebBrowser axWebBrowser1;
[SetUp]
public void SetUp()
{
_downloadComplete = false;
downloader = new InnerHtmlDownloader();
axWebBrowser1 = new AxWebBrowser();
}
[TearDown]
public void TearDown()
{
}
[Test]
public void AxWebBrowserDownloadEvent( )
{
axWebBrowser1.DocumentComp lete +=new DWebBrowserEvents2_Documen tCompleteE ventHandle r(axWebBro wser1_Docu mentComple te);
try
{
axWebBrowser1.Navigate("http://www.google.com") ;
}
catch
{
_downloadComplete = false;
}
Assert.IsTrue(_downloadCom plete, "Download event should have occured.");
}
private void axWebBrowser1_DocumentComp lete(objec t sender, DWebBrowserEvents2_Documen tCompleteE vent e)
{
_downloadComplete = true;
}
}
}
When I run the test, I get the following exception caught by the try...catch block above"
"Exception of type 'System.Windows.Forms.AxHo st+Invalid ActiveXSta teExceptio n' was thrown."
However, I have another application that this works fine with. I would like to write unit tests for my downloader class, but I cannot because I keep getting this error.
Any suggestions?
I'm wondering if you can help me by getting the following test fixture to pass:
//InnerHtmlDownloaderTestF
using System;
using System.Threading;
using AxSHDocVw;
using NUnit.Framework;
using Sidebet.Gui.Tools;
namespace Sidebet.Gui.TestFixtures
{
[TestFixture]
public class InnerHtmlDownloaderTestFix
{
private bool _downloadComplete;
private InnerHtmlDownloader downloader;
private string url = "http://www.google.com";
private AxWebBrowser axWebBrowser1;
[SetUp]
public void SetUp()
{
_downloadComplete = false;
downloader = new InnerHtmlDownloader();
axWebBrowser1 = new AxWebBrowser();
}
[TearDown]
public void TearDown()
{
}
[Test]
public void AxWebBrowserDownloadEvent(
{
axWebBrowser1.DocumentComp
try
{
axWebBrowser1.Navigate("http://www.google.com")
}
catch
{
_downloadComplete = false;
}
Assert.IsTrue(_downloadCom
}
private void axWebBrowser1_DocumentComp
{
_downloadComplete = true;
}
}
}
When I run the test, I get the following exception caught by the try...catch block above"
"Exception of type 'System.Windows.Forms.AxHo
However, I have another application that this works fine with. I would like to write unit tests for my downloader class, but I cannot because I keep getting this error.
Any suggestions?
ASKER
That makes sense...
How would someone be able to test it using Test Driven Development? It is frustrating to be constrained by the type of project I am developing. My unit tests exist in a class library - whereas what I want to test is in a Windows Forms project.
I also tried creating a windows form with a AxWebBrowser in it. I then added a event handler that "wraps" the event produced by the AxWebBrowser. This had the added bonus of allowing me to pass further arguments too. I then created a testFixture class (this time in a windows form project) where I instance the form containing the AxWebBrowser, wired up a callback method to the wrappered event, then tried to have the windows form use the AxWebBrowser to download, and then I waited for the DocumentLoaded event to occur so that I could Assert that it had happened. This also did not work! I received the same error.
It would seem instancing and using a AxWebBrowser in any other place other than a windows form does not work. The windows form cannot be instanced from any other place - with the intent of using the AxWebBrowser - except for in another windows form (such as main()).
I have learned about another unit testing util called NUnitForms. Going to try it out this afternoon. However, if you know more about this type of thing please share!
Thanks,
sapbucket
How would someone be able to test it using Test Driven Development? It is frustrating to be constrained by the type of project I am developing. My unit tests exist in a class library - whereas what I want to test is in a Windows Forms project.
I also tried creating a windows form with a AxWebBrowser in it. I then added a event handler that "wraps" the event produced by the AxWebBrowser. This had the added bonus of allowing me to pass further arguments too. I then created a testFixture class (this time in a windows form project) where I instance the form containing the AxWebBrowser, wired up a callback method to the wrappered event, then tried to have the windows form use the AxWebBrowser to download, and then I waited for the DocumentLoaded event to occur so that I could Assert that it had happened. This also did not work! I received the same error.
It would seem instancing and using a AxWebBrowser in any other place other than a windows form does not work. The windows form cannot be instanced from any other place - with the intent of using the AxWebBrowser - except for in another windows form (such as main()).
I have learned about another unit testing util called NUnitForms. Going to try it out this afternoon. However, if you know more about this type of thing please share!
Thanks,
sapbucket
Can you show me what you mean by instancing a form with a WebBrowser control on it? Also, what .NET version do you have?
Bob
Bob
ASKER
The solution is that you just can't do it. It creates a circular reference. There are many blogs that complain about this and talk about why you can't do it.
The future
The future
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Bob