NUnitASP and Portals

As a followup to my blog post yesterday, my boss decided to throw a new one at me - give an example of how we can use NUnitASP for our projects as another form of TDD during the project life cycle. Well, I hadn't used NUnitASP before, so I jumped right in following their step-by-step tutorial for newbies. Sure, the tutorial wasn't a real-world example, it did give you an idea of how things work and what needs to be done.

So, after my success with that, I decided it was time to take one of our applications for a test ride. Well, this application so happened to be built on a custom portal framework we developed, and things started to go wrong for me from the very beginning. I first tried to get to the first main page, the login page, and grab the username & password text boxes in a test.

Problem 1: NUnitASP took me to the default page, which sets a cookie and redirects the user to their homepage. Well, NUnitASP didn't do the redirect, so I was stuck with a page that had no controls. Solution was to grab the URL manually (which have a few different query string parameters that vary depending on your user/role/application using/page viewing).

Problem 2: NUnitASP needs to grab the controls via HTML ID. Ahh, so I just plopped in "tbUsername" and "tbPassword" - what? It can't find them? Solution was to fully qualify them as "_ctl5_tbUsername" and "_ctl5_tbPassword". This doesn't really make writing tests easy for data driven portals that vary on the modules that are displayed...I mean, how am I supposed to know the unique prefix that is added.

So, from my two problems I just listed, I've come to the conclusion that NUnitASP cannot (as of this blog posting) be used in Portal based webpages. My reasons are as follows:

  • You need to know the exact URL to a page to test, initially at least. If you're page's are dynamically put together with different modules based on you're user, role, and query string parameters, this could vary from time to time, thus breaking your tests which require modifying your tests...not a good practice.
  • You need to know the exact HTML ID of the element you're trying to write tests against. This is a good design if you're pages are darn near static (in the sense that the way they're loaded is not data driven), and are not contained in User Controls (this may work, but the same logic applies) because you know what the HTML ID should be when writing a test. However, if you're using a portal, you will most commonly not know the unique name of the control, which could in turn change from time to time, again (revisit my last statement above, it applies here too).

Although I say NUnitASP is not a good framework for portal based solutions, I do see it as being a very viable framework for pages that are not dynamically created. Maybe this is something that will possibly change in the future, however I'm not sure if or how it would. What are your thoughts on TDD of portal based application UIs?

Update: I missed the "UserControl" tester object that can be used for accessing User Controls and controls within those. However, this will still not work for dynamically created UC's that are added that don't have a distinct ID that is set, IE - portals.

14 Comments

  • In regards to the second bullet point - you don't need to have the exact HTML id. You need to setup a user control container and then place your textbox controls in there. That way NUnitAsp can figure out the HTML id for you.

  • Is there any documentation that you can point me to for this? Is that limited for only UserControls that you know the ID of, and does it work with dynamically loaded UC's?

  • I didn't even know this existed (shows how much I pay attention). I'm a Web jockey at heart, but in my current gig it's all back-end stuff and we couldn't get along without NUnit. This sonuds good conceptually, but if you figure out how to overcome the limitations you mention, I'd be glad to hear how.

  • The problem with my situation is that we dont set the ID of the dynamically generated UC's, so they do get _ctl1 and _ctl2, etc. I'm thinking that this can change for my situation, though, so we have an actual name to use.

  • I'm beginnging to explore NUnit and NUnitAsp for our development as well and would love to see a good set of tips for using NUnitAsp. The biggests issues I'm having right now is simply that the support for a few of our frequently used controls is not supported (ie: Repeater, CheckBoxList, and RadioButtonList). I'm trying to get familiar with the source, actually working to add a RadioButtonListTester to the project but until I have time to really look into the guts of how everything works it's tough.

  • As a point of interest you can obtain the clientside ID programmatically via the ClientID property either from the codebehind(MyUserControl.ClientID) or within the ASPx with <%=MyUserControl.ClientID%>



    for dynamic controls you can iterate across the page with:



    foreach (System.Web.UI.Control currentControl in this.Controls)

    {

    TheUserControl currentUserControl = currentControl as TheUserControl;



    if (currentUserControl != null)

    {

    // do somthing with currentUserControl.ClientID

    }

    }



  • Nice to see posts on NUnitAsp. We use it all the time but on a regular web app not a portal. We have had similar problems testing custom controls - what we decided was to have a separate test web project that hosts each control in its own page where it can be tested in a known state. This doesn't test the interaction of that control with other controls - but after all, it is unit testing.



    Another approach we use when we can't figure out a better way is to just use IndexOf(expectedString) tests on the resulting HTML (Browser.CurrentPageText) - ugly but it keeps the TDD cycle going ...

  • Hi, Matt. I'm the maintainer of NUnitAsp. NUnitAsp was developed for a portal project, so I know that it can be used in that environment. The trick is to remember that it's meant to be used for test-driven development and for unit tests. If you try to test pre-existing code at integration-test level, as you are, you're bound to be disappointed (as you were).



    Jonathan Cogley has the answer: focus on testing individual units and put them in test web pages so that you can get at them easily. That's what we did on our portal project and it worked well.



    I'd like NUnitAsp to work more smoothly. I'm not currently developing in ASP.NET, so I'm relying on the (very sporadic) contributions of the community to keep it up to date. In this situation, NUnitAsp is only as good as the people who use it.

  • Jim,



    Thanks for the answer and explanation. It does seem like it would work in that instance, however my division has chosen to not proceed with UI level TDD, so its not really an issue anymore.

  • If I have a html table that doesn't have an ID, how can I initiate click on the cell data ?

  • Hi,

    I am facing a problem when i try to test an HTML Control. Is NUnitASP limited to Web Controls alone?



    Thanks In Advance,

    Jefferey Rayan

  • First I tried to test our MSCMS Application with NUnitAsp, I was also disappionted. But a second look helped me to understand how it works and now I'm using it more and more. Yes, I had to modify the pages and some controls but in future I will design my pages to work well with NUnitAsp. The benefit of testing tens or hundreds of use cases on one button click before delivering a release or patch is very big. It is really embarrassing to deliver a software and the customer notices that some older components don't work anymore. There is a lot to improve in NUnitAsp but we can contribute...

  • how exactly can i use aspid or htmlid of controls for their validation or reference.. please help

  • Can anyone tell me how to:



    1. get access to rendered HTML. Someone mentioned using Browser.CurrentPageText, but can you go into more detail or provide some sort of reference material.



    2. how to address controls NUnitAsp does not support, other than just creating custom controls to handle them.



    3. how to address actual objects instead of proxy objects (or have complete implementation of properties and methods of actual objects)



    4. is there away to execute client side script using NUnitAsp



    5. how to drive AspRunTime externally or get a handle on the actual webpage, and thus all elements on that page



    Thanks.

Comments have been disabled for this content.