2011-12-25 39 views
1

問題:「CurrentThread需要將ApartmentState設置爲ApartmentState.STA才能夠自動運行Internet Explorer。」Watin和ApartmentState.STA

首先,我已閱讀上述問題的所有解決方案,並沒有適用於我。可能是我錯過了一些東西。我已經嘗試將執行線程條目添加到我的app.config中,也嘗試設置STAThread屬性,並且我仍然面臨與上述相同的異常。

工具:Visual Studio 2010中,華廷2.1,C#

場景:試圖運行時從一個按鈕,點擊一個Web應用程序的單元測試[在C#華廷腳本。但是腳本即將在以下行上啓動IE時會引發上述異常: IE mybrowser = new IE(「SomeURL here」);

有什麼想法?

回答

2

從朋友處得知它。我們實際上不必添加任何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(); 
} 
1

你的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需要更改。

+0

添加上述行到我的app.config,現在它看起來像這樣(在我的第一篇聲明仍然無法正常工作,並拋出了同樣的錯誤!): <配置> < - 的URL主頁 - > <添加鍵= 「AllClubsPage」 值= 「http://www.somehomepaggeurl.com」/> <! - 有效值是STA,MTA。其他人忽略。 - > <添加鍵= 「的ApartmentState」 值= 「STA」/> 2011-12-29 21:03:48

1

只需添加[大會:RequiresSTA]在你的文件的頂部或項目的入口點。

0

我已經做了的app.config改變

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <sectionGroup name="NUnit"> 
     <section name="TestRunner" type="System.Configuration.NameValueSectionHandler" /> 
    </sectionGroup> 
    </configSections> 
    <NUnit> 
    <TestRunner> 
     <add key="ApartmentState" value="STA" /> 
    </TestRunner> 
    </NUnit> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="nunit.framework" publicKeyToken="96d09a1eb7f44a77" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-2.6.3.13283" newVersion="2.6.3.13283" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
</configuration> 

,但沒有骰子。我打開AssemblyInfo並添加了

[assembly: RequiresSTA] 

突然間,宇宙開始再次正常工作。