How to Debug a UFT script in VBScript Debugger?
How to Debug a UFT script in VBScript Debugger?

How to Debug a UFT script in VBScript Debugger?

For effectively resolve the error, we need to use the debug process of VBScript or UFT. It is a feature given to any IDE (Integrated Development Environment). Debugging refers to an activity to effectively figure out the root cause of any problem. The problem could be anything:

  • A Bug
  • A logic error
  • A mismatch in requirement
  • A wrong understanding.

It is very important while debugging and learning an Automation framework.

What is Debugging?

Debugging is a process by which we try to detect, locate and omit bugs from our code.

What is Debugger?

A debugger is a two-step process:

  • Identifying an issue
  • fix the issue and retest

A debugger is a tool (a set of codes) that provides insight into the running code in line by line manner. It places itself between compiler or runtime engine and real running of the program.

Even though the main objective of a debugger is to debug and find the issues in the code. But it is equally effective to understand the code, in terms of a logic flow. Without debugging if we need to read and understand the code, it would be a time-consuming process.

Let’s see another important process called maintenance. During this phase, most of the time the person who wrote the code does not fix the code or alter the code. Without a proper debug process, maintainability will go for a toss.

UFT Debug Process

UFT does have a solid debugger which will help us to debug. This post will not talk about UFT debugger but I will discuss how we can debug.QFL or.VBS file outside of the UFT IDE.

Microsoft Debugger

Microsoft Script Debugger is a correct tool that can be used as a debugger of a VBScript file on a system where UFT is not installed.

How to get Microsoft Script Debugger?

UFT/QTP has the debugger inbuilt with the IDE. In case if we are using UFT to debug our scripts, we do not need any other component or debugger.

Just in case, if we are debugging the.VBS file outside of UFT or we want to debug the.VBS file where UFT is not installed, We need to download the Microsoft Script Debugger from here.

After downloading, we need to install the debugger. As it is a Microsoft product, it is safer to restart the machine to take effect.

How to activate Microsoft Script Debugger?

The Microsoft debugger upon installation, attached to itself with WSH.Once we double click on the required.VBS file, the script will execute it via Microsoft Script Debugger.

We have one more option to use debugger via command line. To activate this feature we need to go to Start->Run->CScript.exe and open it.

Once we have opened it, we can use different switches to execute. Switches can be found here.

Microsoft Script Debugger has a great visual representation along with the components mentioned below.


WScript //x {path of the script}
//or
WScript //d {path of the script}

//x switches ask WSH to bring debugger and breaks at the first line.
//d switches ask WSH to bring the debugger if it is needed. It activates the debugger at the preset breakpoint.

Components of Microsoft Script Debugger

BreakPoint:

This is the important component of the Microsoft Debugger. This will pause execution at a certain line if we use this component and enable it. The objective of the breakpoint is to instruct the WSH engine to stop the execution at the given breakpoint. This component is very useful as it only halts where we want it to halt leaving other lines of code unhalted but executed.

We can open the debugger manually and click on the suspected line. This is the line where the execution will halt. This feature is also available in UFT IDE. This line will be highlighted in yellow and a red circle or dot will indicate the execution is paused.

If we open the script using //x switch, the debugger automatically stops at the first line of the script.

We can set up multiple breakpoints in the script.

With Breakpoint enabled WSH will only stop if one of the below occurs:

  • It encounters an unhandled error
  • It reaches the end of the script.
  • It gets a breakpoint provided by the user.
Stepping

While breakpoint pauses the execution, these stepping features enable us to go slowly over the line of codes to identify the issue or understand the code.

Stepping is divided into three parts:

  1. Step into
  2. Step over
  3. Step out
Step Into

This component enables us to go into a user-defined procedures, sub or a function. However, we can not go into a function or sub defined by VBScript.Like Msgbox().So to step into a function or sub, the function or sub has to be in the scope of the debugger.

This component goes inside of a custom function or sub or procedure. It starts executing line by line.

Step Over

Once we know the function is good to go, we can use this feature/component. This component instead of going inside the function or sub or procedure, executes them and provides the output directly to the user.

Step Out

This feature finishes the execution of a lower-level procedure or sub or function. It then pauses at the starting of the next line. This is extremely useful when we step into a function or procedure and decide to leave or go out of it.

Note:

Step into, Step over, Step Out are just visual representation and stoppage of execution but underlying execution still takes place even if we skip from one component to another.

View Call Stack

This component stacks the nested sequence of function or procedure calls. It provides a hierarchical list of the chain during the execution of our script. It is useful when we have multiple function calls in a chained manner.

WSH engine keeps track of all the calling function’s order until these functions get completed. WSH traverses through the stack to determine the completion of the called functions.

How to Activate Call Stack?

Below steps needs to be performed while activating the call stack.

  • Put a breakpoint in the script.
  • Run the script in the debug mode.
  • While the control stops at the breakpoint, click on the call stack option under view Menu.
  • The call stack window will appear and displays the stack information.

As it is a stack, we need to read from the bottom towards up.

Command Window

This component enables us to query and view active variables. This is very useful when we want to check some value of the variables, functions or sub. So theoretically, it is another strong debugging feature. The command window supports a special technique by which we can query the WSH.

We need to use the “?” question mark to query the WSH. The “?” operator fetches the required information if available in the scope of WSH.The breakpoint and? operator combo provides a powerful technique to view the insight of the script.

Command window can access the intrinsic objects like Err Object. The WSH also has a set of other intrinsic objects available.

Inserting temporary code

Debug mode in Microsoft Script debugger supports to insert temporary custom test code during execution in debug mode. This helps us to test the logic, variable value, flags, etc. This is a very popular technique while debugging and testing a large codebase.

 

How to Use Debug Message?

The debugger provides a clear insight into a code. While watching/view are temporary and only available during test run/debug session, the debugger provides other ways to save the debug message.

    • Put the debug message to a log file for future reference. A redirecting debug message to a log file is an efficient way for framework development.
    • This is running commentary of the script execution about how and what the script performing from running an Excel macro to a sendkey function.
    • Debug messages enable us to track changes of a variable which we can configure and see how the values are getting changed.
    • The simplest form of debug message is Msgbox() function. It is a great tool to see values. It also pauses the script execution in a modal way until and unless we click on the “Ok” button. The script execution remains halted until then.
      
      Msgbox "The value is "+myVar
      
    • The WSH Script uses the Echo() method to display detailed information. It is more preferable over the Msgbox() function. In WScript.exe, Echo method will display the message in a dialog box. If we use several Echo() messages, in our script, we need to click ok again and again.

WScript.Echo "The value is "+myVar
  • The Stdout property of WSH is another feature to transfer TextStream to console. This is only applicable to CScript.exe. In the case of Wscript.exe, Stdout property is not applicable and throws an error.
  • LogEvent() is another method of WSHShell object to log the WindowsEvent log. This method is supported by both WScript.exe and CScript.exe.

What is not supported by Microsoft Script Debugger?

The Microsoft Script Debugger does not support the followings:

  1. A local window to showcase active variables at the breakpoint.
  2. It does not have a watch window
  3. It does not support dynamic breakpoint

Note:

UFT/QTP does have these features, so if we are using UFT/QTP instead of Microsoft Script Debugger, these components will be automatically available

The omission of Assertion in UFT and VbScript Debugger

VBScript Debugger or UFT surprisingly does not support the assertion. Also, there is no support for AssertThat() function. We need to depend on if-else and custom message to implement assertion.

Conclusion:

While UFT provides a great debugging option, Microsoft Script Debugger is a lightweight debugger that can debug any VBScript used in UFT. It can also work in a UFT neutral system. This feature is a major boost for the Script review process. Surely and certainly Microsoft Script Debugger will increase the speed of review, execution and debug process.

 

 

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on [email protected]

Write A Comment