find a location for property in a new city

Wednesday, 8 June 2011

ThreadStateException thown in WatiN with Internet Explorer

I was using NUnit to Assert some responses from a suite of WatiN tests whilst using the IE browser. Running them caused a ThreadStateException with an error message of 'The CurrentThread needs to have its ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer'

Internet Explorer is not thread safe so WatiN should use a single threaded apartment (STA). To ensure you abide by this WatiN will throw a ThreadStateException if it is not. This can be rectified by setting the ApartmentState to ApartmentState.STA as the error message suggests for Internet Explorer.

How set your CurrentThread to ApartmenState.STA for WatiN in NUnit

If you've haven't already create an App.config in your NUnit testing assembly. You will need to add the following to it:
<?xml version="1.0" encoding="utf-8" ?>
<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>

If you are having problems with different runners other than NUnit you can find tips on how to set STA on them too.

Follow britishdev on Twitter

5 comments:

  1. Latest sports news headlines from India and world. Check out today's most recent & up-to-date news coverage, videos & photos at thesportsrumour.com
    sports news

    ReplyDelete
  2. Awesome blog. I would love to see true life prepared to walk, so please share more informative updates. Great work keeps it up. 3313 exam questions answers

    ReplyDelete
  3. Motifz Designer Lawn. 1, 2 & 3 Piece Unstitched Premium Embroidered Lawn 2019, Premium embroidered Lawn, Premium lawn, Premium lawn 2019, Motifz, Premium lawn in Pakistan Summer collection. Shipping worldwide. Stitching option available.

    ReplyDelete
  4. Norton setup has the best products. Norton antivirus provide high security and protects the device from viruses and hackers. You can get the Norton products from Norton setup. If you are facing any problem regarding Norton then use the Norton removal tool. For further information, please visit Norton Setup

    ReplyDelete