Working with Events in VBScript

Introduction to Events in VBScript: Tutorial #10

The last tutorial in VBScript course was ‘Strings and Cookies’. This tutorial on “Working with Events in VBScript” will brief you all about the Events that are used in the VBScript.

Events are one of the important topics in VBScript and proper understanding is required for better programming experiences as the Events form the basis for performing actions like a mouse click, etc. and hence, they should be handled appropriately and wisely.

This tutorial covers the meaning of Event in VBScript and its various types along with simple examples for your better understanding.

Events in VBScript

What are VBS Events?

Events are the Actions that occur when any activity is performed like any mouse click, pressing keys, mouse hover, etc.

With the help of writing a piece of code in programming languages like VBScript, these events can be captured and actions can be performed as per your requirements by making the best use of the Event Handling mechanism.

Different types of validations and responses can be handled in case of occurrence of an event like a window or a dialog box which appears on clicking some button, in such a case you can write a piece of code to handle such a window or dialog box.

Different Types of Events

There are different types of Events that take place in VBScript programming language. Each event can be handled by writing some code.

In this tutorial, I will explain Events in the context of an HTML code (as every HTML element holds certain types of events) as well as in the context of QTP/UFT to showcase the real implementation and use of Events while working with the scripts in QTP.

Let’s see the working of the some of the important events in the context of HTML.

#1) ‘On Click of Button’ Event

This Event occurs while clicking any button that is present on any HTML page.

You can handle this event as per your requirements. If you want to show some message on the click of the button then you can handle message box or if you want to move the application to the next page on the click of the button then you can write a piece of code to handle that.

Following is an example to show the working of this Event.

Example:

<html> 
<head>
<title>Let’s see the working of ‘On Click of Button’ Event</title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
Function displaymessage(name)
 Msgbox “Hello ” & name 
End Function
</script>
<input type =”button” onclick= “displaymessage(riya)”> 
</body> 
</html>

Output is: Hello riya

When the button is clicked then ‘on click’ the event will get triggered and ‘displaymessage’ function will get invoked and the Msgbox will display the message as mentioned.

Example 1

The same Event that I have discussed above can be handled in the scripts by making a function in the QTP which can get invoked every time when any button in the application gets clicked.

Assume that there is an Application where there is a Window (parent object) inside which there are many buttons (child objects) and you have to click a particular button and you want the same function to get invoked on click of any of the buttons in the application.

In this scenario, you can make one function which can get invoked every time when any button is clicked.

Let’s understand this with the help of an Example.

Example:

<html> 
<head>
<title>Let’s see the functionality of ‘Button Click’ Event</title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
Function buttonclick(Window name,Button name)
 Dim flag,flag1
 flag = WpfWindow(Window name).Exist
 If flag = false then
 Msgbox “Window name object does not exist in the application”
 Exit Function
 End If
 flag1 = WpfWindow(Window name).WpfButton(Button name).Exist
 If flag1 = false then
 Msgbox “Button name object does not exist in the application”
 Exit Function
 Else
 WpfWindow(Window name).WpfButton(Button name).Click
 Msgbox “Button is clicked successfully”
 End If
End Function
Msgbox buttonclick(“Login”,”Add”)
</script>
</body> 
</html>

Output is: Button is clicked successfully

As you can see, a function named as ‘buttonclick’ is created to click the button ‘Add’ inside the Window ‘Login’ in the application having WpfObjects(assumption). When a function is invoked by passing the parameters, the code written inside the function gets invoked and the button will get clicked successfully.

If the window name or button name that is passed is not correct then Msgbox will display an appropriate message based upon which is written inside the function.

example 2

#2) ‘Mouse’ Events – Mouse Over and Mouse Out

These Events occur while placing the mouse over the elements and taking out the mouse from the elements.

Like the other events, you can handle these events also as per your requirements. If you want to show some message while you mouse over an element or when the mouse focus come out of an element then you can handle message box or if you want to move the application to the next element then you can write a piece of code to handle that.

Following is an example to show the working of this Event.

<html> 
<head>
<title>Let’s see the working of ‘On Click of Button’ Event</title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
Function displaymessage(name)
 Msgbox “Hello ” & name 
End Function
Function displaymessage1(name)
 Msgbox “Hello ” & name 
End Function
</script>
<input name =”txtmsg” type =”text” OnMouseOver= “displaymessage(riya)”> <br>
<input name =”txtmsg1” type =”text” OnMouseOut= “displaymessage1(siya)”>
</body> 
</html>

Output is:

Hello riya
Hello siya

When the mouse gets over a text element then the ‘OnMouseOver’ event will get triggered and ‘displaymessage’ function will get invoked and the Msgbox will display the message as mentioned and when the mouse gets out of a text element then ‘OnMouseOut’ event will get triggered and ‘displaymessage1’ function will get invoked and Msgbox will display the message as mentioned.

These are some of the important events, hence I discussed them in detail.

Let’s see a list of some other events as well.

List of Few other Events

Following is the list of some of the other important events that occur while working with the scripts in QTP along with their descriptions.

They are:

  • onchange Event: This event occurs when any change occurs in context to the element in the Script i.e. when element changes, this event gets fired in the Script.
  • onsubmit Event: This event occurs at the Script level when the form gets submitted in the application.
  • onreset Event: This event occurs at the Script level when the form gets at the reset mode in the application.
  • onfocus Event: This event occurs at the Script level when any of the elements gets a focus in the application.
  • onblur Event: This event occurs at the Script level when the element loses its focus in the application.
  • ondblclick Event: This event occurs at the Script level when the mouse gets double-clicked.
  • onmousedown Event: This event occurs at the Script level when the mouse button gets pressed.
  • onmouseup Event: This event occurs at the Script level when the mouse button gets released.
  • onkeyup Event: This event occurs at the Script level when any of the keys that are present in the keyboard button gets released.
  • onkeydown Event: This event occurs at the Script level when any of the keys that are present in the keyboard get pressed.
  • onkeypress Event: This event occurs at the Script level when any of the keys that are present in the keyboard get pressed and released.
  • onmousemove Event: This event occurs at the Script level when the pointer of the mouse moves.

Conclusion:

I hope that this tutorial must have provided you an insight regarding the importance and effectiveness of using Events in VBScript.

Next Tutorial #11: I will cover about ‘Excel Objects’ in my next VBScript tutorial.

Meanwhile, Stay tuned for much more informative tutorials and feel free to share your thoughts.