Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Using NUnitAsp to test pages w/Forms Authentication

NUnitAsp is a great tool for running web "smoke tests" from your continuous integration environment.  (You do have your CI env't setup, don't you? ;)  NUnitAsp is also a handy tool to automatically preload and check all of your web pages after a deployment has been performed.  It only becomes a little tricky when you want to test secure web pages protected with Forms Authentication.  If your site is using Windows Integrated security, the security is trivially bypassed by passing credentials to the framework's virtual browser.  But with Forms Authentication, the actual login page should to be invoked to setup the user credentials and perform other application-specific actions.

The following derived class from NUnitAsp's WebFormTestCase class creates a Forms Authentication session by invoking the login page of the application, populating the username and password fields with real data, and submitting the form to allow the web application to set up the authentication session.  As a limitation of the framework, before every NUnitAsp unit test, the virtual browser used by NUnitAsp, along with the current Forms Authentication session, gets trashed.  At first, this implies that you must invoke the login page before every unit test; but by grabbing the cookies created by the first invocation of the login page and reusing them on subsequent tests, you only need to invoke the login page once.  This significantly cuts down on the time it takes to run a number of NUnitAsp tests at once.

public class FormsAuthenticatedWebFormTestCase : WebFormTestCase { protected override void SetUp() { base.SetUp(); if (FormsAuthenticationCookies != null) { Browser.Cookies.Add(FormsAuthenticationCookies); } else { LoginToApplication(); FormsAuthenticationCookies = Browser.Cookies.GetCookies(Browser.CurrentUrl); } } private CookieCollection FormsAuthenticationCookies { get { return (CookieCollection) CallContext.GetData("FormsAuthenticationCookies"); } set { CallContext.SetData("FormsAuthenticationCookies", value); } } private void LoginToApplication() { Browser.GetPage(LOGIN_PAGE_URL); TextBoxTester txtUsername = new TextBoxTester(ID_OF_USERNAME_TEXTBOX, CurrentWebForm); TextBoxTester txtPassword = new TextBoxTester(ID_OF_PASSWORD_TEXTBOX, CurrentWebForm); ButtonTester btnLogin = new ButtonTester(ID_OF_SUBMIT_BUTTON, CurrentWebForm); txtUsername.Text = APP_USERNAME; txtPassword.Text = APP_PASSWORD; btnLogin.Click(); } }

I'd rather use composition over inheritance of the WebFormTestCase but since the WebFormTestCase's Browser object is protected, it's not accessible unless you inherit from it, use reflection, or use another technique to bypass the protected accessor.

With this extension to NUnitAsp to test secure applications, it's a no-brainer to include NUnitAsp in your test suite.  You'll find that it takes a lot of guess work out of knowing if a deployment was successful.

Billy McCafferty

kick it on DotNetKicks.com


Posted 07-09-2007 2:37 PM by Billy McCafferty

[Advertisement]

Comments

27 Links Today (2007-07-12) wrote 27 Links Today (2007-07-12)
on 07-12-2007 11:20 AM
Billy McCafferty wrote Configuring IIS Extension Mappings with NAnt
on 08-13-2007 4:01 PM

NAnt is a build tool that may be employed to automatically build your ASP.NET project, run unit tests

vvpnkapil wrote re: Using NUnitAsp to test pages w/Forms Authentication
on 08-30-2007 1:42 AM

I am unable to get the namespace like nunitasp even though I imported name spaces like

using NUnit.Framework.SyntaxHelpers;

using NUnit.Extensions.Asp.AspTester;

using NUnit.Extensions.Asp;

using NUnit.Framework;

I reffered some sites inorder to overcome this problem finally, I found that the above specified namespaces are sufficient to test the controls and their functionalities on the web page

one think that even, I am unable to acces the method like assertvisibility()

could any body pls help me out which dll is required to get the namespace like nunitasp

Regards

v v p n kapil..........

Billy McCafferty wrote re: Using NUnitAsp to test pages w/Forms Authentication
on 09-06-2007 8:27 PM

There is no "NUnitAsp" namespace, just NUnit.Extensions.Asp.  Check out the NUnitAsp quick start guide at nunitasp.sourceforge.net/quickstart.html .

Thorin wrote re: Using NUnitAsp to test pages w/Forms Authentication
on 09-13-2007 10:46 PM

How do you handle setting the Password textbox if said textbox has been marked as a Password field using TextMode="Password" (this will cause stars to appear in the textbox when the user sees it)?

Billy McCafferty wrote re: Using NUnitAsp to test pages w/Forms Authentication
on 09-17-2007 1:26 PM

You have to use attributes since you can't set that directly:  txtPassword.Attributes["value"]="password";

Ravi wrote re: Using NUnitAsp to test pages w/Forms Authentication
on 11-05-2007 8:37 AM

Hi kapil u have to add reference NUnitASP.dll to ur Project

http://devlicio.us/blogs/billy_mccafferty/archive/2007/07/09/using-nunitasp-to-test-forms-authentication-pages.aspx wrote http://devlicio.us/blogs/billy_mccafferty/archive/2007/07/09/using-nunitasp-to-test-forms-authentication-pages.aspx
on 03-26-2008 4:54 AM
Sam Gentile's Blog wrote New and Notable 177
on 12-02-2008 8:38 PM

Multithreading and Concurrency Software Transactional Memory Part IV - Thread-Bound Transactions Software Transactional Memory Part V - Integration with System.Transactions Parallel LINQ Restating the Concurrency Problem Herb Sutter is starting a new

devlicio.us wrote re: Using NUnitAsp to test pages w/Forms Authentication
on 06-04-2011 11:19 PM

Using nunitasp to test forms authentication pages.. He-he-he :)

devlicio.us wrote re: Using NUnitAsp to test pages w/Forms Authentication
on 06-20-2011 12:22 AM

Using nunitasp to test forms authentication pages.. Awesome :)

7rw4 sex yh1y wrote re: Using NUnitAsp to test pages w/Forms Authentication
on 07-02-2011 5:04 AM

Using nunitasp to test forms authentication pages.. Great idea :)

5bb8 sex videos 6s7b wrote re: Using NUnitAsp to test pages w/Forms Authentication
on 07-02-2011 9:18 PM

Using nunitasp to test forms authentication pages.. Ho-o-o-o-t :)

joirti wrote re: Using NUnitAsp to test pages w/Forms Authentication
on 02-22-2012 9:54 PM

aqfzok

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)