Sponsored Ad

Tuesday, April 13, 2010

How to Debug/Unit Test JavaScript Functions in VS2005

Debugging with JavaScript is easy now in VS2005. Let’s start with a simple example.

Prerequisite for JavaScript Debugging:

Go to tools -> Internet Options -> Advance tab

Please note that these settings for IE 7 and can vary for other versions of IE.

How to Debug/Unit Test JavaScript Functions in VS2005

Uncheck the Disable Script Debugging (Internet Explorer)

And Uncheck the Disable Script Debugging (Other)

How to Debug/Unit Test JavaScript Functions in VS2005

And for Microsoft VM

Uncheck Java console enabled

Uncheck logging enabled

Check JIT compiler for virtual machine enabled

 

Lets create a JavaScript function and start debugging/Unit testing

So we have a JavaScript function:

<html>

<head>

<script language="javascript" type="text/javascript">

function jFunction()

{

debugger;

var str1 = "How to do Unit Testing ";

var str2 = "How to use Debugger in JavaScript ";

alert(str1);

}

</script>

</head>

<body>

<form id="Form1" runat="server">

<asp:button runat="server" text="Button" OnClientClick="jFunction()" />

</form>

</body>

</html>

debugger is kind of breakpoint in JavaScript and when this function will execute our execution pointer will stop here. Alert() is used to display message in alert box.

Just run your web application by pressing the F5 button from keyboard.

Click on the Button.

How to Debug/Unit Test JavaScript Functions in VS2005

A debugging window is opened and execution stops at debugger;

How to Debug/Unit Test JavaScript Functions in VS2005

These are debugging buttons used to debug the code.

clip_image010 Break to next statement

clip_image012 Step Into: It is used to send the execution, inside the function code.

clip_image014 Step Over: it execute the current statement and send execution pointer to next statement.

clip_image016 Step out: it send execution pointer to out of function and execute all function code.

So as the execution goes on , at any point of time you can check the intermediary values of variables by mouse over the variable or right click the variable - > Quick watch

0 comments:

Post a Comment

Sponsored Ad

Related Software Testing Articles

Development Updates

Tech Updates