Visually testing unusual code paths in ASP.NET WebForms Website - asp.net

I have a large complex ASP.net WebForm website that I'm working on a visual redesign and am trying to think of good ways to exercise all the code paths in the website so I can see how things look with the redesign.
For example lets say I have a message that only gets displayed if there is an error which rarely happens. Here is an example of what my code might look like:
if (someErrorCondition) {
someControl.Visible = true;
} else {
someOtherControl.Visible = true;
}
This might not be a good way of doing things, but this is a good example of my existing code base I have to work with.
Let us assume for the sake of simplicity that I already have a way of testing one part of the if. The problem is exercising the other part without going through a lot of trouble to setup my environment to create an error.
One idea I had was to extract someErrorCondition into a method and in that method check for some session or request key to see if I want to fake a failure. Maybe wrap it in an #if DEBUG block so that it won't be compiled for production.
Any other ideas for how I might go about testing unusual code blocks on an ASP.net website so I can make sure nothing got left out in the redesign?

I believe the best solution is always the most simple. Since you obviously have access to the code, do a search for the Visible property for each form element within Visual Studio and set each one to true to see how it looks. Once you make the design change then un-comment the original code.
Example:
if (someErrorCondition) {
someControl.Visible = true;
} else {
someOtherControl.Visible = true;
}
TO
/* if (someErrorCondition) {
someControl.Visible = true;
} else {
someOtherControl.Visible = true;
}*/ someControl.Visible = true;
This is not good for testing proper behavior of the form, but will let you see how each element looks for visual design purposes.

Related

Google maps API v3 - externally controlling radius on distanceWidget

this seems pretty specific to ask - but there was no other alternative to the expertbase of stackoverflow!
I'm stuck in a problem where I need to control the radius of the circle by an external means such as a dropdown or a textbox.
I had a hard time doing the circle and had to copy stuff from the Twitter search example on:
http://code.google.com/apis/maps/articles/mvcfun/twittersearch.html
Ideally, I would like to know how to bind an external even to the radiusWidget, the problem is - the sizer inside the radiusWidget seems a private entity.
It would be awesome if someone could hit me in the face with a bang!
Thx
distanceWidget.set('distance', XXX) should work. If you read the MVC article you can see how the binding is bi-directional.
You can look at the source of the Twitter example by downloading it to your computer, as you likely already did. When you have the source you can adapt it to your purposes as you like. The software usage rights (terms of usage, license, etc), if they are any, have to be respected of course.
In the case of the RadiusWidget you would just do:
function RadiusWidget(opt_distance) {
// ...
this.sizer = this.addSizer_(); // sizer is now accessible in RadiusWidget
}
RadiusWidget.prototype.addSizer_ = function() {
var sizer = ...
// ...
return sizer;
}

Are there solutions for streamlining the update of legacy code in multiple places?

I'm working in some old code which was originally designed for handling two different kinds of files. I was recently tasked with adding a new kind of file to this code. Most of my problems were solved by filling out an extensive XML file with a new entry that handled everything from what lists were named to how the file is written in plural lower case. But this ended up being insufficient, as there were maybe 50 different places in 24 different code files where I had to update hardcoded switch-statements that only branched for the original two file types.
Unfortunately there is no consistency in this; there are methods which operate half from the XML file, and half off of hardcode. Some of the files which look like they would operate off of the XML file don't, and some that I would expect that I'd need to update the hardcode don't need it. So the only way to find the majority of these is to run through testing the whole system when only part of it is operational, finding that one step to fix (when I'm lucky that error logging actually tells me what is going on), and then running the whole thing again. This wastes time testing the parts of the code which are already confirmed to work, time better spent testing the new parts I have to add on top of it all.
It's a hassle and a half, and to my luck I can expect that I will have to add yet another new kind of file in the near future.
Are there any solutions out there which can aid in this kind of endeavour? Something which I can input some parameters of current features, document what points in a whole code project actually need to be updated, and run something nice the next time I need to add a new feature to the code. It needn't even be fully automated, something that'll help me navigate straight to the specific points in everything and maybe even record what kind of parameters need to be loaded.
Doubt it matters specifically, but the code is comprised of ASP.NET pages, some ASP.NET controls, hundreds of C# code files, and a handful of additional XML files. It's all currently in a couple big Visual Studio 2008 projects.
Not exactly what you are describing, but if you can introduce a seam into the code and lay down some interfaces you can break out and mock, a suite of unit/integration tests would go a long way to helping you modify old code you may not fully understand well.
I completely agree with the comment about using Michael Feathers' book to learn how to wedge new tests into legacy code. I'd also strongly recommend Refactoring, by Martin Fowler. What it sounds like you need to do for your code is to implement the "Replace conditionals with polymorphism" refactoring.
I imagine your code today looks somewhat like this:
if (filetype == 23)
{
type23parser.parse(file);
}
else if (filetype == 69)
{
filestore = type69reader.read(file);
File newfile = convertFSto23(filestore);
type23parser.parse(newfile);
}
What you want to do is to abstract away all the "if (type == foo)" kinds of logic into strategy patterns that are created in a factory.
class FileRules : pReader(NULL), pParser(NULL)
{
private:
FileReaderRules *pReader;
FileParserRules *pParser;
public:
void read(File* inFile) {pReader->read(inFile);};
void parse(File* inFile) {pParser->parse(inFile);};
};
class FileRulesFactory
{
FileRules* GetRules(int inputFiletype, int parserType)
{
switch (inputFiletype)
{
case 23:
pReader = new ASCIIReader;
break;
case 69:
pReader = new EBCDICReader;
break;
}
switch (parserType)
... etc...
then your main line of code looks like this:
FileRules* rules = FileRulesFactory.GetRules(filetype, parsertype);
rules.read(file);
rules.parse(file);
Pull off this refactoring, and adding a new set of file types, parsers, readers, etc., becomes as simple as writing one exclusive to your new type.
Of course, go read the book. I vastly oversimplified it here, and probably got stuff wrong, but you should get the general idea of how to approach it from this. I can also recommend another book, "Head First Design Patterns", which has a great section on the Factory patterns (if you like those "Head First" kinds of books.)

Conditional compilation "else"

In AS3 you can pass a constant to the compiler
-define+=CONFIG::DEBUG,true
And use it for conditional compilation like so:
CONFIG::DEBUG {
trace("This only gets compiled when debug is true.");
}
I'm looking for something like #ifndef so I can negate the value of debug and use it to conditionally add release code. The only solution I've found so far was in the conditional compilation documentation at adobe and since my debug and release configurations are mutually exclusive I don't like the idea of having both DEBUG and RELEASE constants.
Also, this format works, but I'm assuming that it's running the check at runtime which is not what I want:
if (CONFIG::DEBUG) {
//debug stuff
}
else {
//release stuff
}
I also considered doing something like this but it's still not the elegant solution I was hoping for:
-define+=CONFIG::DEBUG,true -define+=CONFIG::RELEASE,!CONFIG::DEBUG
Thanks in advance :)
This works fine and will strip out code that won't run:
if (CONFIG::DEBUG) {
//debug stuff
}
else {
//release stuff
}
BUT this will be evaluated at runtime:
if (!CONFIG::DEBUG) {
//release stuff
}
else {
//debug stuff
}
mxmlc apparently can only evaluate a literal Boolean, and not any kind of expression, including a simple not.
Use the if / else construct : the dead code will be removed by the compiler and it will not be tested at runtime. You will have only one version of your code in your swf.
If you are not sure use a decompiler or a dump tool to see what really happens.
http://apparat.googlecode.com/files/dump.zip
http://www.swftools.org/
...
While Patrick's answer fulfills the question's criteria, it does not cover all use cases. If you are in an area of code that allows you to use an if/else statement then this is a good answer. But if you are in a place where you cannot then you will need a better solution. For example, you may want to do something like this to declare a constant in a class:
private var server:String = "http://localhost/mystagingenvironment";
or for a live release:
private var server:String = "http://productionserver.com";
(this is an example and I'm not advocating this as production code).
I use xml configs and use the loadConfig+="myconfig.xml" to do my configuration instead of passing large numbers of command line params. So in the <compiler> section of your xml config:
<define>
<name>CONFIG::debug</name>
<value>false</value>
</define>
<define>
<name>CONFIG::release</name>
<value>!CONFIG::debug</value>
</define>
This works well for all use cases:
CONFIG::debug
{
private var server:String = "http://localhost/mystagingenvironment";
}
CONFIG::release
{
private var server:String = "http://productionserver.com";
}
This has the additional benefit of working consistently across applications. It also does not rely on the 'optimize' flag being true, like Patrick's answer (although I think we can assume that 99.999999% of all swfs have optimize=true, I only set it to false when the optimizer breaks my AS3).
It does have the drawback that it doesn't compile all code paths, just the ones that are included. So if you're not using a build server to create release builds and tell you when things break, be prepared for surprise errors when you do your release build ("But it compiled in debug! Crap, I need this to launch now!").
Just my two cents about Chris Hill's answer (which is the solution I also use regularly): it seems that using the loadConfig+="myconfig.xml" option makes the compiler searching for the myconfig.xml file in the Flex SDK directory whereas the -load-config+=myconfig.xml option makes it searching for the myconfig.xml file in the project's directory, which is the behavior I strongly prefer as you can then easily distribute this file with your project sources...

WatiN - getting past the certificate error page

Anyone know how to use CertificateWarningHandler in WatiN?
I've got as far as...
IE ie = new IE("https://mysite.aspx");
CertificateWarningHandler cwh = new CertificateWarningHandler(CertificateWarningHandler.ButtonsEnum.Yes);
cwh.HandleDialog(new Window(ie.hWnd));
... which does precisely nothing.
On a more general note, how on earth do you people manage to use this tool? The documentation is nearly useless, and there doesn't seem to be any decent resource online. I must be missing something because it's taken me about half an hour to write 3 lines of code that don't even work.
I'm using something similar to what Saar is using and it works fine (my tests are cross-browser).
//Override security warning in browser
{
if (Browser.Link(Find.ById("overridelink")).Exists)
{
Browser.Link(Find.ById("overridelink")).Click();
Browser.WaitForComplete();
}
else
{
Browser.WaitForComplete();
} //end else
}
I'm not a developer, and I've found that there's plenty of information out there on WatiN and others post code samples and the like that are really helpful. Google is one of my best friends when it comes to finding WatiN help. You'll get the hang of it.
have you tried following already?
ie.DialogWatcher.Add(cwh);
or just
ie.DialogWatcher.Add(new CertificateWarningHandler());
Update: After comment.
Actually this works for me.
further may be following will help
Browser browser = ie;
if (browser.Links.Exists("overridelink"))
{
browser.Link("overridelink").Click();
}

drupal's hook_preprocess_page not working as expected

i am having an issue where hook_preprocess_page 's changes to &$variables is not being rendered, even though it is the last item under $theme_registry['page']['preprocess functions']. logging contents of $variables to a file show the contents changed, but contents appear unchanged on the site. flushed all cache on drupal, flushed all browser caches and still the same result.
/**
* Implementation of hook_preprocess_page().
*/
function grinchlist_preprocess_page(&$variables) {
if (grinchlist_usercheck($variables['user']['uid'])) {
$variables['scripts'] = preg_replace('/<script[^>]*christmas_snow.*<\/script>/','',$variables['scripts']);
}
file_put_contents('/tmp/vars.txt',print_r($variables,true));
}
the /tmp/vars.txt shows the variables properly, but the browser still show the script being loaded.
this may be a silly example, but i've had this issue with the hook_preprocess_page in other instances and it would really help out to understand what is going on here...
thanks.
The reported code contains an error. The IF-statement should be corrected from
if (grinchlist_usercheck($variables['user']['uid'])) {
// ...
}
to
if (grinchlist_usercheck($variables['user']->uid)) {
// ...
}
I am using hook_preprocess_page() in one of my modules, and the invoked function does change the content of the variables.
Then, as also Richard M reported, the function should get the list of the included JavaScript files from drupal_get_js().
I think you probably (assuming this works in the same way as CSS includes) need to call drupal_get_js at the end of your function, like so: $variables['scripts'] = drupal_get_js();.
I know this is an old question but I just struck it and I think I know the answer.
I think jquery_update is causing this.
jquery_update implements hook_theme_registry_alter which changes $theme_registry so that jquery_update_preprocess_page runs last. This is despite what Peter sees in $theme_registry because the alter happens after he looks at it.
jquery_update gets $scripts from drupal_add_js(), fiddles with the array and then resets $variables['scripts'] which overwrites any changes made earlier.
I'm not sure what the perfect solution is. I don't think we're really supposed to mess with the scripts string directly. I have a special one page case so I'm probably going to do the somewhat bad thing of calling my code from jquery_update_preprocess_page. jquery_update for Drupal 6 is unlikely to updated now. That seems better than getting into a dueling battle of who comes last.

Resources