Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7018559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:04:00+00:00 2026-05-27T23:04:00+00:00

I’m using WatiN for some automated tests and what I found was that creating

  • 0

I’m using WatiN for some automated tests and what I found was that creating an IE instance for every test was not scalable. The creation and shutdown time of each IE instance was eating me alive:

    [TestMethod]
    public void Verify_Some_Useful_Thing()
    {
        using (var browser = new IE())
        {
            browser.GoTo("/someurl");
            // etc..
            // some assert() statements
        }
     }

However, the using statement did prove useful in that I can always ensure that my IE instance would have its dispose() method called which would close the IE window.

Anyway, I created a singleton which maintained a single IE instance which all my tests use across all my test classes:

public class BrowserPool
{      
    private static readonly Lazy<BrowserPool> _instance = new Lazy<BrowserPool>(() => new BrowserPool());        

    private IE _browser;
    private string _ieHwnd;
    private int _threadId;

    public IE Browser
    {
        get
        {
            var currentThreadId = GetCurrentThreadId();
            if (currentThreadId != _threadId)
            {
                _browser = IE.AttachTo<IE>(Find.By("hwnd", _ieHwnd));
                _threadId = currentThreadId;
            }
            return _browser;
        }
        set
        {
            _browser = value;
            _ieHwnd = _browser.hWnd.ToString();
            _threadId = GetCurrentThreadId();
        }
    }

    /// <summary>
    /// private to prevent direct instantiation.
    /// </summary>
    private BrowserPool()
    {
        Browser = new IE();
    }

    /// <summary>
    /// Get the current executing thread's id.
    /// </summary>
    /// <returns>Thread Id.</returns>
    private int GetCurrentThreadId()
    {
        return Thread.CurrentThread.GetHashCode();
    }

    /// <summary>
    /// Accessor for instance
    /// </summary>
    public static BrowserPool Instance
    {
        get
        {
            return _instance;
        }
    }
}

And the test now:

  [TestMethod]
  public void Verify_Some_Useful_Thing()
  {
        var browser = BrowserPool.Instance.Browser;
        browser.GoTo("/someurl");
        // some assertions
  }

This works well and my tests run much faster now that I’m not opening and closing IE every test. However, I have this one problem in that when the tests complete my IE instance will still remain open.

I can’t figure out an elegant way to ensure that BrowserPool.Browser has dispose() or close() called on it before the application ends. I even tried using a finalizer in the BrowserPool class but that didn’t seem to work because the _browser variable had already been reclaimed when my finalizer is called.

How can I ensure that dispose() is called on my IE instance after the test run?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:04:01+00:00Added an answer on May 27, 2026 at 11:04 pm

    Use the [AssemblyCleanupAttribute()] to clean resources after your tests found in the assembly are finished.

    Btw, never rely on the use of destructors in .NET.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.