Update: The javascript on this blog has been suppressed by the blogger's parser. Consequently, some of the functionality may not work. Inconvenience is regretted.

Sunday, September 7, 2008

Two nifty tools to increase your productivity on windows

While there are a number of tools to make your life hassle free on windows, the two I found very useful are below:

  • Launchy: No more searching through your all programs to find that program whose location you d0n't exactly remember or don't care to remember. Just type first few letters of the program name and you have it, right from your desktop.
  • USB Disk Ejector: If you keep most of your data on portable drives or happen to do a lot of plug-in and plug-out on your USB drive, this one could save you a lot of time. No more right-clicking that pathetic windows system tray icon and going through all that "Safe Eject" thing. Here is a nice tutorial to get max out of this nice little utility.

 

Enjoy!!

Tuesday, May 20, 2008

Leveraging Asp.Net WebForms Client Library

While working with asp.net, most front end developers have their own javascript library of utility functions like: (get/set)ElementPosition, getScroll(x/y), (add/remove)ClassName, etc. Most of the times there is no need to have your own versions of some of the more commonly used javascript utility functions as they are already present in the Webresourse.axd javascript file embedded (not always) in your asp.net page and most importantly the functions are Cross Browser. The utility functions in this client library are used for asp.net callbacks & postbacks, in addition to being used by asp.net validation, treeview and menu client libraries (yes, there are seperate libraries for all these). So, if you are using asp.net menu/treeview/validators/callbacks on your webpage, you can happily use the functions provided in the library. If you want to ensure that the library is included in your page, just use a nice to have feature in your asp.net page: MaintainScrollPositionOnPostback:

<%@ Page Language="C#" MaintainScrollPositionOnPostBack="true"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>



or appliying it sitewide thru web.config as:



<system.web>
<pages maintainScrollPositionOnPostBack="true">
</pages>
</system.web>




The script file is included in my page as:

<script type="text/javascript" src="/TestWebsite/WebResource.axd?d=X2ApKqYNd-qiVRFcah5bwg2&t=633437918395970000">

Following is the list of some of the commonly used functions present in the library:



    WebForm_GetScrollX

    WebForm_GetScrollY

    WebForm_SetElementX

    WebForm_SetElementY

    WebForm_GetElementById

    WebForm_SetElementWidth

    WebForm_SetElementHeight

    WebForm_RemoveClassName

    WebForm_AppendToClassName

    WebForm_GetParentByTagName

    WebForm_GetElementByTagName

    WebForm_GetElementsByTagName

    WebForm_GetElementPosition - another version here


Hope this is helpful.


pushp

Thursday, April 3, 2008

AJAX Issues With WATIN

Recently I started Automating the UI test cases for our AJAX enabled asp.net website with Watin. It was not long before that I ran into problems as Watin seems to be ajax postback agnostic.

I had a master dropdownlist and a child dropdownlist (this is disabled until something is selected in the master DDL). On the selection of an item in the master DDL there is an AJAX postback that would get data for the child DDL and then an item in the child DDL will be selected. To automate this scenario I wrote the following code:


IE ie = new IE("http://localhost/MySite/Default.aspx");
SelectList ddlMaster = ie.SelectList("ddlMaster");
ddlMaster.Option(Find.ByValue("India")).Select();
SelectList ddlChild = ie.SelectList("ddlChild");
ddlChild.Option(Find.ByValue("Hyderabad")).Select();


The Problem: The first DDL gets selected perfectly and an asynchronous postback happens but I get an exception in the fourth line (which tries to select a value in the child DDL) saying "Could not find OPTION element tag matching criteria: Attribute 'value' with value 'Hyderabad'". The problem was clear - we are trying to select a value in the child DDL before asynchronous postback is over and the child DDL is populated with values.

The first thought that came into my mind was to put a Thread.Sleep but I know it wasn't a good solution since I can never be sure of how long the asynchronous request is going to take. So I looked for something that could tell me if page is still in async postback. Enter the PageRequestManager Class in asp.net ajax. It has a property called isInAsyncPostback that you can query to know if page is in async postback. So all I needed was a way to run some javascript from the code and thank god, there is Eval in Watin to do just that.

The Solution:

1)Updated the aspx page with a method to return true if page is in async postback as below:


<asp:ScriptManager ID="scrptMgr" runat="server" EnablePartialRendering="true">
</asp:ScriptManager> <!-- script manager shown for completeness -->
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
function IsPageInAsyncPostback(){
return prm.get_isInAsyncPostBack();
}

</script>

2) Wrote a helper method to wait until async postback is complete:


private static void WaitForAsyncPostBackToComplete(IE ie)
{
bool isInPostback = true;
while (isInPostback)
{
isInPostback = Convert.ToBoolean(ie.Eval("IsPageInAsyncPostback();"));
if (isInPostback)
Thread.Sleep(200); //sleep for 200ms and query again
}
}


3) Updated the test scenario code as below:


IE ie = new IE("http://localhost/MySite/Default.aspx");
SelectList ddlMaster = ie.SelectList("ddlMaster");
ddlMaster.Option(Find.ByValue("India")).Select();
WaitForAsyncPostBackToComplete(ie);
SelectList ddlChild = ie.SelectList("ddlChild");
ddlChild.Option(Find.ByValue("Hyderabad")).Select();


and all was merry!!!

Hope this is helpful.

pushp