2

I'm writing UI-tests with Specflow and Watin. I have done the following to ensure Watin to work

  1. Setting the Thread.ApartmentState in the App.config

    <configuration>
      <configSections>
        <section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
        <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>
    
  2. In the Properties for InteropSHDocVw I've set the "Embeded Interop Types" to False and the "Copy Local" to True

My code looks like this... code in () is because I'm not allowed to share credentials and urls to this site

  1. The specflow feature

    Feature: Company
    In order to earn money
    As a company
    I want the suer to be able to shop our products
    Scenario: Log In
        Given I have navigated to (a companys test website)
        And I have entered (a username) and (a password) as credentials
        When I press Log in
        Then then I should have the (permissions)
    
  2. The teststeps written in Watin

    using NUnit.Framework;
    using System;
    using TechTalk.SpecFlow;
    using WatiN.Core;
    
    namespace Projectname.Companyname.Specs
    {
        [Binding]
        [RequiresSTA]
         public class CompanySteps
        {
    
        IE browser = new IE();
    
        [Given]
        public void Given_I_have_navigated_to_URL(string url)
        {
            browser.GoTo(url);
        }
    
        [Given]
        public void Given_I_have_entered_EMAIL_and_PASSWORD_as_credentials(string email, string password)
        {
            browser.TextField(Find.ByName("UserName")).TypeText(email);
            browser.TextField(Find.ByText("Password")).TypeText(password);
        }
    
        [When]
        public void When_I_press_Log_in()
        {
            browser.Button(Find.ByText("Log in »")).Click();
        }
    
        [Then]
        public void Then_then_I_should_have_the_PERMISSION_permission(string permission)
        {
            browser.GoTo("the page where the permissions is showing as plain text");
    
            Assert.IsTrue(browser.ContainsText(permission));
        }
    }
    

Now, my probelm, I've done all the things mentioned online to fix the STA or the Interop exceptions, but now when I run my Specflow/Watin test, I still get this exception for the STA thread:

System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. ----> System.Threading.ThreadStateException : The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer.

Anyone who have had the same problem and has a solution? Have I forgotten any Code parts? is the syntax wrong somewhere?

Kindest regards and thanks in advance for your help.

LenaS
  • 21
  • 1

0 Answers0