The test is taken from the book Pro ASP.NET MVC 2 Framework
by Steven Sanderson and it's located on page 73-74.
The test is just testing a default MVC 2 application and it looks like this:
using NUnit.Framework;
using WatiN.Core;
namespace IntegrationTestingExample.WaitNTests
{
[TestFixture]
public class UserAccountTests
{
private const string rootUrl = "http://localhost:8080";
[Test]
public void DisplaysUserNameInPageHeader()
{
var userName = "steve2";
var password = "mysecret";
using (var browser = CreateBrowser())
{
// Register a new account
browser.GoTo(rootUrl + "/Account/Register");
browser.TextField("UserName").Value = userName;
browser.TextField("Email").Value = "test@example.com";
browser.TextField("Password").Value = password;
browser.TextField("ConfirmPassword").Value = password;
browser.Forms[0].Submit();
}
using (var browser = CreateBrowser())
{
// Log in and check the page caption
browser.GoTo(rootUrl + "/Account/LogOn");
browser.TextField("UserName").Value = userName;
browser.TextField("Password").Value = password;
browser.Forms[0].Submit();
browser.GoTo(rootUrl);
string actualHeaderText = browser.Element("logindisplay").Text;
StringAssert.Contains("Welcome " + userName + "!", actualHeaderText);
}
}
// Just using IE here, but WatiN can automate Firefox too
private Browser CreateBrowser() { return new IE(); }
}
}
I can run the test in the NUnit GUI, but it fails, I get this error message:
IntegrationTestingExample.WaitNTests.UserAccountTests.DisplaysUserNameInPageHeader:
System.Threading.ThreadStateException : The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer.
This is the stack trace from the NUnit GUI:
at WatiN.Core.IE.CheckThreadApartmentStateIsSTA()
at WatiN.Core.IE.CreateNewIEAndGoToUri(Uri uri, IDialogHandler logonDialogHandler, Boolean createInNewProcess)
at WatiN.Core.IE..ctor()
at IntegrationTestingExample.WaitNTests.UserAccountTests.CreateBrowser() in d:\documents and settings\b\my documents\visual studio 2010\Projects\RuneIntegrationTestingExample\IntegrationTestingExample.WaitNTests\UserAccountTests.cs:line 42
at IntegrationTestingExample.WaitNTests.UserAccountTests.DisplaysUserNameInPageHeader() in d:\documents and settings\b\my documents\visual studio 2010\Projects\RuneIntegrationTestingExample\IntegrationTestingExample.WaitNTests\UserAccountTests.cs:line 17
WHAT SHALL I DO TO MAKE THE WAITN TEST PASS? AM I DO SOMETHING WRONG? AM I USING THE RIGHT TOOL TO RUN WAITN TESTS?
iturune
Member
190 Points
229 Posts
How to run the a WaitN test?
Aug 17, 2010 11:11 AM|LINK
Hi I am trying to run WaitN test using NUnit.
The test is taken from the book Pro ASP.NET MVC 2 Framework by Steven Sanderson and it's located on page 73-74.
The test is just testing a default MVC 2 application and it looks like this:
using NUnit.Framework; using WatiN.Core; namespace IntegrationTestingExample.WaitNTests { [TestFixture] public class UserAccountTests { private const string rootUrl = "http://localhost:8080"; [Test] public void DisplaysUserNameInPageHeader() { var userName = "steve2"; var password = "mysecret"; using (var browser = CreateBrowser()) { // Register a new account browser.GoTo(rootUrl + "/Account/Register"); browser.TextField("UserName").Value = userName; browser.TextField("Email").Value = "test@example.com"; browser.TextField("Password").Value = password; browser.TextField("ConfirmPassword").Value = password; browser.Forms[0].Submit(); } using (var browser = CreateBrowser()) { // Log in and check the page caption browser.GoTo(rootUrl + "/Account/LogOn"); browser.TextField("UserName").Value = userName; browser.TextField("Password").Value = password; browser.Forms[0].Submit(); browser.GoTo(rootUrl); string actualHeaderText = browser.Element("logindisplay").Text; StringAssert.Contains("Welcome " + userName + "!", actualHeaderText); } } // Just using IE here, but WatiN can automate Firefox too private Browser CreateBrowser() { return new IE(); } } }I can run the test in the NUnit GUI, but it fails, I get this error message:
IntegrationTestingExample.WaitNTests.UserAccountTests.DisplaysUserNameInPageHeader:
System.Threading.ThreadStateException : The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer.
This is the stack trace from the NUnit GUI:
at WatiN.Core.IE.CheckThreadApartmentStateIsSTA()
at WatiN.Core.IE.CreateNewIEAndGoToUri(Uri uri, IDialogHandler logonDialogHandler, Boolean createInNewProcess)
at WatiN.Core.IE..ctor()
at IntegrationTestingExample.WaitNTests.UserAccountTests.CreateBrowser() in d:\documents and settings\b\my documents\visual studio 2010\Projects\RuneIntegrationTestingExample\IntegrationTestingExample.WaitNTests\UserAccountTests.cs:line 42
at IntegrationTestingExample.WaitNTests.UserAccountTests.DisplaysUserNameInPageHeader() in d:\documents and settings\b\my documents\visual studio 2010\Projects\RuneIntegrationTestingExample\IntegrationTestingExample.WaitNTests\UserAccountTests.cs:line 17
WHAT SHALL I DO TO MAKE THE WAITN TEST PASS? AM I DO SOMETHING WRONG? AM I USING THE RIGHT TOOL TO RUN WAITN TESTS?
Rune
iturune
Member
190 Points
229 Posts
Re: How to run the a WaitN test?
Aug 17, 2010 11:52 AM|LINK
Hi I just found out that I had missed a file in my IntegrationTestingExample.WaitNTests project, the file was 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> <!-- WatiN can only host IE in STA mode --> <add key="ApartmentState" value="STA"/> </TestRunner> </NUnit> </configuration>I don't know quite what this App.config file does to my project, but when I included it I could run my WaitN tests and make them pass.
Rune