DevPinoy.org
A Filipino Developers Community
      

Approach to testing/developing pages that rely on lots of context

rated by 0 users
This post has 6 Replies | 2 Followers

Top 10 Contributor
Posts 951
Points 22,725
cruizer Posted: 12-11-2006 5:57 PM

Hi everyone!

I've noticed while developing e-commerce oriented web sites (ASP.NET 2.0 of course) that it's hard to develop and test pages that relies a lot on context (meaning past actions or data that accumulate in one's session). What I mean is that, for instance, in our e-commerce web site, one has to log in, add products to the shopping cart, check out, choose a shipping address, choose a shipping method, enter credit card information, etc. So when I am developing the pages near the end of the process (e.g. the credit card info entry page), I have to go through EVERYTHING just to get to the page that I am interested in testing/developing. Doing this repetitively is tedious and boring.

What is your strategy or approach in developing pages like this?

The approach I've taken for now is to put a conditional #define and have code sections marked with #ifdef ... #endif. So if I normally only do something like this:

1
2
3
4
5
6
7
protected void Page_Init(object sender, EventArgs e)
{
if (!SessionHelper.UserIsLoggedIn()) {
Response.Redirect("loginpage.aspx");
}
ProcessMyEventsHere();
}

I replace that with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
protected void Page_Init(object sender, EventArgs e)
{
#ifdef TESTING_ONLY
if (!SessionHelper.UserIsLoggedIn()) {
SetUpContextActions();
}
#endif
if (!SessionHelper.UserIsLoggedIn()) {
Response.Redirect("loginpage.aspx");
}
ProcessMyEventsHere();
}

#ifdef TESTING_ONLY
protected void SetUpContextActions() {
// do stuff here like manipulation of Session store and other
// objects in order to create the illusion that I did stuff in the
// other (previous) pages of the site before going here
}
#endif

in that way I can do a #define TESTING_ONLY at the top of the code and the page will execute SetUpContextActions() in order to set up the context that the page expects (like being logged in, having products in the shopping cart, etc.). I can remove the #define once I'm done testing in order for the compiler to skip over those portions with #ifdef ... #endif, so as not to bloat the output assembly/code.

What's your take on this? If you have something elegant and useful it would be well-appreciated Smile Thanks!

http://devpinoy.org/blogs/cruizer
Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
Top 10 Contributor
Posts 1,965
Points 39,160

Nice question cruizer.

My usual approach is using the Conditional Attribute which is the same as the #if DEBUG directive. Below is a sample code structure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System; 
using System.Diagnostics;

class Program {
static void Main(string[] args) {

    #if DEBUG
      DebugMethod();
#endif

AnotherDebugMethod();
    
    NonDebugMethod();

    Console.ReadLine();
}

  static void DebugMethod() {
    //gets called only on debug mode
Console.WriteLine("Called DebugMethod");
}

[Conditional("DEBUG")]
static void AnotherDebugMethod() {
    //only gets called when in debug mode same as #if DEBUG but a little bit less messy
Console.WriteLine("Called AnotherDebugMethod");
}
  
  static void NonDebugMethod() {
    //this gets called all the time
Console.WriteLine("Called NonDebugMethod");
}
}

devpinoy sig

Top 10 Contributor
Posts 1,100
Points 17,955
i haven't done much test driven development on the UI,  but having those line of codes just for testing is kind of a code smell to me.  just like application configuration, ideally, i would want those context values to be wrapped by another class which can be easily setup by the test fixture
  • | Post Points: 20
Top 10 Contributor
Posts 951
Points 22,725
well yes. the reason i brought this up was that my code looks ugly and I wanted to solicit opinion from all of you Smile

note though that what I am doing is not TDD nor user interface testing but, in a way, short-circuiting actual testing of my web pages. i wanted to do something like Model-View-Presenter on this one, but MVP is about testing UI logic (the presenter), not getting feedback on the actual look of the pages. what I plan to do to avoid ugly code is to put that code only while I'm developing/testing the page but to remove it completely once I'm done. yun nga lang, sayang naman kung kailanganin ko ulit...
http://devpinoy.org/blogs/cruizer
Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
  • | Post Points: 35
Top 10 Contributor
Posts 266
Points 4,955
cruizer:
well yes. the reason i brought this up was that my code looks ugly and I wanted to solicit opinion from all of you Smile

note though that what I am doing is not TDD nor user interface testing but, in a way, short-circuiting actual testing of my web pages. i wanted to do something like Model-View-Presenter on this one, but MVP is about testing UI logic (the presenter), not getting feedback on the actual look of the pages. what I plan to do to avoid ugly code is to put that code only while I'm developing/testing the page but to remove it completely once I'm done. yun nga lang, sayang naman kung kailanganin ko ulit...


OT:
Actually master cruizer, its when I do an e-commerce app when I realized how important MVC pattern on web is. There a lot times I thinks the UI logic must be controlled by some other parts.

For instance, when customer Checkout, Registration must be shown, if customer is logged in, checkout page will be shown.
Also, when customer selects Credit Card, show the credit card form, else commit the order.
and a lot of cases like this where I realized something has to control the flow. NOT the code in code behind.

Now i know i thinks its not too late to do MVC on next project. My code is dirty, smelly mixed with p0loniom-210, you can't take it joey, you might need a gas mask Stick out tongue
Rodel E. Dagumampan Software Engineer - ASP.NET/C# Bel-air, Makati, Philippines Email : dehranphATgmail.com Tel No : +63 918 2025 694
  • | Post Points: 5
Top 10 Contributor
Posts 1,100
Points 17,955
cruizer:
but MVP is about testing UI logic (the presenter), not getting feedback on the actual look of the pages.


ummm, di ko get's to bossing, di ba kasama na rin sa presenter on how to configure the page? like disabling this button, etc.
  • | Post Points: 20
Top 10 Contributor
Posts 951
Points 22,725
exactly. but how the output LOOKS like...well, i'm afraid MVP doesn't deal with that. and that's what I need Big Smile unfortunately i'm no web designer and as such it takes me quite some time of trial and error to get pages to look right.
http://devpinoy.org/blogs/cruizer
Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
  • | Post Points: 5
Page 1 of 1 (7 items) | RSS

Copyright DevPinoy 2005-2008