AxWebBrowser Control test fixture - InvalidActiveXStateException

Hello,

I'm wondering if you can help me by getting the following test fixture to pass:

//InnerHtmlDownloaderTestFixture.cs
using System;
using System.Threading;
using AxSHDocVw;
using NUnit.Framework;
using Sidebet.Gui.Tools;

namespace Sidebet.Gui.TestFixtures
{
    [TestFixture]
    public class InnerHtmlDownloaderTestFixture
    {
        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.DocumentComplete +=new DWebBrowserEvents2_DocumentCompleteEventHandler(axWebBrowser1_DocumentComplete);

            try
            {
                axWebBrowser1.Navigate("http://www.google.com");
            }
            catch
            {
                _downloadComplete = false;
            }
           

            Assert.IsTrue(_downloadComplete, "Download event should have occured.");
        }

        private void axWebBrowser1_DocumentComplete(object sender, DWebBrowserEvents2_DocumentCompleteEvent 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.AxHost+InvalidActiveXStateException' 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?
LVL 3
sapbucketAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
That means that the ActiveX control needs to be hosted (sited) on a form, and cannot be instantiated the way that you are doing it.

Bob
sapbucketAuthor Commented:
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
Bob LearnedCommented:
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
sapbucketAuthor Commented:
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
GranModCommented:
PAQed with points refunded (500)

GranMod
Community Support Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.