问题:“CurrentThread需要将其ApartmentState设置为ApartmentState.STA,才能使Internet Explorer自动化。”
首先,我已经阅读了上述问题的所有解决方案,但没有一个对me.May有效,因为我遗漏了一些东西。我已经尝试将执行线程条目添加到我的app.config中,也尝试了设置STAThread属性,但我仍然面临如上所述的相同异常。
工具: Visual Studio2010、Watin2.1、c#
场景:点击按钮后,尝试在c#中从web应用程序运行单元测试watin脚本。但是当脚本将要启动IE时,会抛出上面的异常: IE mybrowser = new IE ("SomeURL here");
有什么想法吗?
发布于 2011-12-30 20:43:55
从一个朋友那里得到的。我们实际上不需要添加任何app.config条目。只需在单个状态下启动线程即可。在我的例子中,我在我的按钮点击处理程序中编写了以下代码:
System.Threading.Thread th = new Thread(new ThreadStart(Test));
th.SetApartmentState(ApartmentState.STA);
th.Start();
th.Join();
and i moved the call to unit test in the private. TEST method as follows:
private void Test()
{
var som = new Project.ClassName();
som.MethodToExecute();
}发布于 2011-12-29 02:04:27
你的App.Config是什么样子的?
<NUnit>
<TestRunner>
<!-- Valid values are STA,MTA. Others ignored. -->
<add key="ApartmentState" value="STA"/>
</TestRunner>
</NUnit>上面的代码适用于Win7、IE9 (32位)和Watin2.1。它也可以在WinXP,IE8,WatiN 2.1上运行。我99%确定它在以前的WatiN版本上也运行得很好。不需要其他ApartmentState更改。
发布于 2014-03-20 05:37:42
只需在文件顶部或项目入口点添加assembly: RequiresSTA即可。
https://stackoverflow.com/questions/8631953
复制相似问题