开发者

Setting apartment state for using WatIn with MSTest

开发者 https://www.devze.com 2023-01-30 03:47 出处:网络
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.

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:

 开发者_如何学C   [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?


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>


Consider updating your code to use NUnit 2.5 with RequiresSTA attribute.


Try this instead:

[STAThread]
static void Main(string[] args)
{
}
0

精彩评论

暂无评论...
验证码 换一张
取 消