5.10. Common WatiN Errors

While WatiN is a great framework, when you get started we've found that people run into the same common problems which can be difficult to identify and resolve.

The first issue is regarding missing assemblies. If you're missing the core WatiN assembly then you will get compile time errors; however, if you're missing a reference to SHDocVw you receive runtime errors and your tests fail. The output of one of our failing tests was:

failed: System.IO.FileNotFoundException: Could not load file or assembly 'Interop.
SHDocVw, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null' or one of its
dependencies. The system cannot find the file specified.
     at WatiN.Core.IE.CreateNewIEAndGoToUri(Uri uri, LogonDialogHandler
logonDialogHandler, Boolean createInNewProcess)
     at WatiN.Core.IE.ctor()

Solving this is very simple. In C:WindowsSystem32 you will find an assembly called shdocvw.dll. This needs to be in the bin directory when you execute your tests so watin can detect and use it as required. ShDocVw.dll allows WatiN to automate Internet Explorer. To fix this error, you need to reference this dll, which will then become Interop.SHDocVw.dll due to the fact it is a COM dll. We always take a copy of this dll and place it next to the watin assembly and reference it from there, as demonstrated in our examples.

If your using TestDriven.NET you will never see this exception, however when using NUnit.UI, NUnit.Console or ReSharper you might receive the following error.

System.Threading.ThreadStateException: The CurrentThread needs to have it's
ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer.
at WatiN.Core.IE.CheckThreadApartmentStateIsSTA()
at
WatiN.Core.IE.CreateNewIEAndGoToUri(Uri uri, IDialogHandler logonDialogHandler,
Boolean createInNewProcess)
at WatiN.Core.IE.ctor()
To solve this, the documentation () says that you can include this line of code:
System.Threading.Thread.Currentthread.SetApartmentState(System.Threading.
ApartmentStat.STA);

To solve this you need to include a config xml with your assembly. Simply name the file <your assembly name>.dll.config and ensure that it is copied to the output folder as content. The contents need to be as follows.

<?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>

You will now be able to execute your tests using any test runner.

One of the great advantages of WatiN is that you can use a number of different browsers. However, the first time you try and use Firefox you will receive a similar error message to the following:

failed: WatiN.Core.Mozilla.FireFoxException: Unable to connect to jssh server,
please make sure you have correctly installed the jssh.xpi plugin
--> System.Net.Sockets.SocketException: No connection could be made because the
target machine actively refused it 127.0.0.1:9997
     at WatiN.Core.Mozilla.FireFoxClientPort.Connect()
     at WatiN.Core.Mozilla.FireFox.CreateFireFoxInstance()
     at WatiN.Core.Mozilla.FireFox.ctor()
     at WatiN.Core.BrowserFactory.Create(BrowserType browserType)

While WatiN uses SHDocVw for IE, for Firefox it uses an extension called jSSH. The extension is included within the samples for this chapter. Simply install this and WatiN will be able to interact with Firefox.

Yet IE is not without its issues. If you are running IE7 upwards, it includes a "feature" called Protected Mode. This mode causes problems when WatiN attempts to access the web page. As such, it is wise to disable this via the internet security settings for the browser. NOTE: This can lead to security problems.

These issues are based on our own personal experience. If you experience other issues then WatiN has excellent support documentation on their website (http://watin.sourceforge.net/documentation.html)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.116.51