• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

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?
0
sapbucket
Asked:
sapbucket
  • 2
  • 2
1 Solution
 
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
0
 
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
0
 
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
0
 
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
0
 
GranModCommented:
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now