5

In thread WatiN in Visual Studio 2008 - second test method fails there is a good solution with IEStaticInstanceHelper (original answer Reusing an IE instance in VS test, sources), but when ClassCleanup fires it fails on AttachToIE. As result IEXPLORAR remain running.

What is the problem?

Of course it is possible to just kill the process like this:

// Id of IEXPLORAR
_ie.ProcessID

Process.GetProcessById(_processId).Kill();
this._ie = null;

But I don't really like this way...

Any ideas?

Community
  • 1
  • 1
Pavlo Neiman
  • 7,438
  • 3
  • 28
  • 28

2 Answers2

2

It fails because MSTest does the class cleanup in a multi-threaded apartment, even thought it runs individual tests in an STA. The way WaitN attaches to IE involves looking up COM objects that aren't thread safe, and aren't exposed to MTA.

Thanks for the process kill workaround, using that too now, although I'm using CloseMainWindow() rather than Kill()

Adnan
  • 25,882
  • 18
  • 81
  • 110
Laurence
  • 10,896
  • 1
  • 25
  • 34
0

You could use a wildcard with WatiN's AttachTo method to grab the existing browser instance. This would let you re-use the browser instance in subsequent tests or close the browser down if that is all you are interested in. For example:

// find first browser matching our wildcard
IE found = Browser.AttachTo<IE>(Find.ByTitle(new Regex(".*")));

// then close just that one
found.Close();

// or close all running IE instances at once
// found.ForceClose();

You can determine if there is an IE instance to attach to using the Exists method with the same Find Constraint. For example:

Constraint browserWildcard = Find.ByTitle(new new Regex(".*"));
if(IE.Exists<IE>(browserWildcard))
{
  // ...
}
stephen
  • 1,200
  • 1
  • 13
  • 16