I'm trying out WatiN and it works fine when I run my test method from
a Main method marked with [STAThread].
When I run my test method using TestDriven.Net 2.6.2096 beta, I get
this:
failed: The CurrentThread needs to have it's ApartmentState set to
ApartmentState.STA to be able to automate Internet Explorer.
System.Threading.ThreadStateException
Message: The CurrentThread needs to have it's ApartmentState set to
ApartmentState.STA to be able to automate Internet Explorer.
Source: WatiN.Core
StackTrace:
at WatiN.Core.IE.CheckThreadApartmentStateIsSTA()
at WatiN.Core.IE.CreateNewIEAndGoToUri(Uri uri, LogonDialogHandler
logonDialogHandler)
at WatiN.Core.IE..ctor(String url)
and calling SetApartmentState in the beginning of the test (or in
TestFixtureSetup) fails because the apartment state has already been
set to MTA.
I get the same behavior in TestDriven 2.5.2708 beta, and the same when
I test in debugger, or with coverage, or whatever.
Googling around, I seem to see Jamie and the WatiN website and others
in agreement that TestDriven applies STA by default. Anyone know why
it might behave differently? Am I doing something dumb?
The test code is really trivial:
[TestFixture]
public class WatinTest
{
[TestFixtureSetUp]
public void Setup()
{
if (Thread.CurrentThread.GetApartmentState() ==
ApartmentState.Unknown)
{
Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
}
else if (Thread.CurrentThread.GetApartmentState() ==
ApartmentState.MTA)
{
throw new InvalidOperationException("apartment state has
been set to MTA");
}
}
[Test]
public void BasicWatinTest()
{
using (IE ie = new IE("http://localhost/testuri"))
{
ie.Button(Find.ByValue("submit")).Click();
}
}
}
Regards,
- Gulli
(Excuse the noise!)
Regards,
- Gulli
When executing tests using TestDriven.NET the ApartmentState should
only set to MTA if your App.config file contains the following:
<configuration>
<appSettings>
<add key="ApartmentState" value="MTA" />
</appSettings>
</configuration>
Otherwise it should default to STA. Could you try running the
following test and let me know what it outputs:
[Test]
public void ThreadTest()
{
Console.WriteLine(Thread.CurrentThread.ApartmentState);
}
Thanks, Jamie.
--
http://www.testdriven.net
http://weblogs.asp.net/nunitaddin
Cheers,
Jamie.
The WatiN documentation addresses this.
http://watin.sourceforge.net/apartmentstateinfo.html
We fixed this by adding the following into our app.config for each
project that uses NUNit and WatiN
<configSections>
<sectionGroup name="NUnit">
<section name="TestRunner"
type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<NUnit>
<TestRunner>
<!-- Valid values are STA,MTA. Others ignored. -->
<add key="ApartmentState" value="STA" />
</TestRunner>
</NUnit>