Search code examples
visual-studio-2010nunitmstestwatinwebtest

Setting apartment state for using WatIn with MSTest


I am getting the following error in:

The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer.

With the following code:

    [TestClass]
    public class UnitTest1
    {

    [AssemblyInitialize]
    public static void AssemblySetup(TestContext context)
    {

    }

    [TestMethod]
    [HostType("ASP.NET")]
    [AspNetDevelopmentServerHost("C:\\SomePath", "/")]
    [UrlToTest("http://localhost/HomeView.aspx")]
    public void TestMethod1()
    {
        using(IE ie = new IE("http://localhost/HomeView.aspx",true))
        {
            ie.TextField(Find.ById("MainContent_txtDLNumber")).TypeText("a235801945550");
        }
    }
}

Is there a different approach for using WatIn with MsTest?


Solution

  • You will probably need to adjust your config accordingly, below should give you a clue

    <configuration>
      <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>
    
    
    </configuration>