February 23, 2020

VBScript tutorial 3 | If Statement | Conditional Statements

This is the third tutorial in the VBScript Tutorials for Beginners. This VBScipt beginner tutorial explains VBScript conditional statements. Please view the VBScript tutorial 3 or read on... A VBScript conditional statement has a condition. It runs one code block if the condition is True or another code block if the condition is False. A conditional statement in VBScript is the If Then statement. The example of a VBScript If Then statement is as follows:

' VBScript code
Option Explicit
Dim name
name = InputBox("Please enter a name", "Name Entry Dialog", "enter name here")
' If Then statement can run only one statement (e.g. the MsgBox statement) if the condition is True
' No statement is run if the condition is False
If Len(name)>5 Then MsgBox "The name is " & name & " and it's length is " & Len(name) & " letters."
' We can execute this VBScript as explained in my VBScript tutorial 1.

Next, let us learn about the VBScript If Then EndIf statement. It can run a code block (multiple statements) if the condition is True. No statement is run if the condition is False. The above VBScript example can be modified using the If Then EndIf statement as follows:

' VBScript code
Option Explicit
Dim name
' The VBScript Trim function removes the spaces on either side of a string.
name = Trim(InputBox("Please enter a name", "Name Entry Dialog", "enter name here"))
If Len(name)>5 Then
    ' This code block has two VBScript statements (MsgBox statements).
    ' The VBScript Ucase function converts the name argument to UPPER case.
    MsgBox "The name is " & UCase(name)
    MsgBox " The length of the name is " & Len(name) & " letters."
End If

Now, we may want to run one code block if a condition is True and another code block if that condition is False. A conditional statement in VBScript is the If Then Else EndIf statement. The example of a VBScript If Then Else EndIf statement is as follows:

' VBScript code
Option Explicit
Dim strTime
REM Comments can start with the REM statement.
REM Time function gives the current time.
strTime = Time
REM InStr function finds the string "AM" within strTime's value from the 1st character position.
If InStr(1, strTime, "AM")<>0 Then
    REM If code block starts
    MsgBox "The current time is " & strTime & ". Have a good morning!"
    REM If code block ends
Else
    REM Else code block starts
    MsgBox "The current time is " & strTime & ". Have a good day and good night!"
    REM Else code block ends
End If

The above VBScript If statements are useful when we need to test a single condition. But, we may need to test multiple conditions and run different code blocks accordingly. Another conditional statement in VBScript is the If Then ElseIf EndIf statement. The example of a VBScript If Then ElseIf EndIf statement is as follows:
' VBScript code
Option Explicit
'  intDayOfWeek will contain an integer value.
Dim intDayOfWeek
' Date function returns the current date
' WeekDay function returns the day of the week, 1 for Sunday through 7 for Saturday
intDayOfWeek = WeekDay(Date)
' strMessage will contain a message (string) for the user.
Dim strMessage
If intDayOfWeek = 1 Then 'Instead of 1, we may write vbSunday, which is more readable.
    strMessage = "Sunday"
ElseIf intDayOfWeek = 2 Then 'Instead of 2, we may write vbMonday and so on.
    strMessage = "Monday"
ElseIf intDayOfWeek =  3 Then
    strMessage = "Tuesday"
ElseIf intDayOfWeek = 4 Then
    strMessage = "Wednesday"
ElseIf intDayOfWeek = 5 Then
    strMessage = "Thursday"
ElseIf intDayOfWeek = 6 Then
    strMessage = "Friday"
ElseIf intDayOfWeek = 7 Then
    strMessage = "Saturday"
Else
    strMessage = "Unknown day"
End If
MsgBox "Have a happy " & strMessage

Another conditional statement in VBScript is the nested If statement. When a VBScript If statement is within another If statement, it is the nested If statement. The example of a nested VBScript If is as follows:

' VBScript code
Option Explicit
Dim a, b, c, biggest
a = 100
b = 150
c = 900
If a > b Then 'Outer If code block begins
    If a > c Then
        biggest = a
    Else
        biggest = c
    End If 'Outer If block ends
Else 'Outer Else block begins
    If b > c Then
        biggest = b
    Else
        biggest = c
    End If
End If 'Outer Else block ends
MsgBox "The biggest number is " & biggest

Want to understand these VBScript examples in detail? Please view my VBScript tutorial 3. Thank you.

February 16, 2020

VBScript tutorial 2 | Sub Procedures and Function Procedures

This is the second tutorial in the VBScript Tutorials for Beginners. This VBScript beginner tutorial explains what is procedure in VBScript. Please view the VBScript tutorial 2 or read on... What is procedure? A VBScript procedure is VBScript code that may be re-used wherever we want. The benefit of using procedures in VBScript is that we don't have to code and test the same thing again. Procedures also make our code modular (and more readable). VBScript procedures include Sub procedures and Function procedures.

What is Sub procedure in VBScript? It is VBScript code that starts with the Sub statement and ends with the End Sub statement. A sub procedure may optionally have parameters, which is additional information given during procedure call. Now, let see VBScript sub procedure examples. I have explained Option Explicit statement and InputBox function in my previous VBScript tutorial 1.

' VBScript code - main script starts
Option Explicit
Dim inputWeight
' CDbl function converts a string to a double precision number
inputWeight = CDbl(InputBox("Enter a weight in kilos:"))
' Procedure call to ConvertWeight sub procedure with inputWeight argument
Call ConvertWeight(inputWeight)
'Procedure call to EndScript sub procedure
Call EndScript
' main script ends

' Sub procedure starts
' Kilos is called the parameter in the sub procedure.
Sub ConvertWeight(Kilos)
    Dim Pounds
    Pounds = 2.205 * Kilos
    MsgBox "The weight of " & Kilos & " kg is equivalent to " & Pounds & " lbs."
End Sub
'Sub procedure ends

' Note that the EndScript procedure has no paramater.
Sub EndScript
    MsgBox "Thank you!"
End Sub

Next, let us learn what is Function procedure in VBScript? It is VBScript code that starts with the Function statement and ends with the End Function statement. A function procedure may optionally have parameters. The difference between sub procedure and function procedure is that a VBScript function procedure can provide a return value. This means that we can call a function procedure within a VBScript statement, just as we would call built-in VBScript functions. Now, let see VBScript function procedure examples.
' VBScript code
Option Explicit
Dim inputWeight
inputWeight = CDbl(InputBox("Enter a weight in kilos:"))
' Function call to Pounds function procedure with inputWeight argument
MsgBox "The weight of " & inputWeight & " kg is equivalent to " & Pounds(inputWeight) & " lbs."
' Function call to ExitMessage function, with no argument
MsgBox ExitMessage()

' Function procedure starts
Function Pounds(Kilos)
    Pounds = 2.205 * Kilos
End Function
' Function procedure ends

Function ExitMessage()
    ExitMessage = "Thank you!"
End Function

Want to learn how sub procedures and function procedures work in VBScript? Please view my VBScript tutorial 2. Thank you.

February 09, 2020

Tue, Feb 11 | Anatomy of a Click | Test Automation Webinar

Hello! Here is your invite to an advanced live webinar titled, "Anatomy of a Click". This webinar would be useful for professionals who perform automated web testing or are interested in web test automation. Testim's software developer, Benji Gruenbaum would go into a deep dive into what browsers do, how events work internally and why, and how Selenium and Puppeteer simulate user actions. This webinar should lead to understanding browser events better and motivation to create better test automation code.

Register now (only limited seats are available) at http://bit.ly/2OhTQLF
Tuesday, February 11th
9 am PST/ 12 pm EST/ 5 pm UTC/ 10:30 pm IST
* If you register but cannot join the live webinar, you will still get it's recording later.


February 02, 2020

VBScript tutorial 1 | Introduction and Variables

This is the first tutorial in the VBScript Tutorials for Beginners. This VBScript beginner tutorial explains what is VBScript. Please view the VBScript tutorial 1 or read on...VBScript is a scripting language that is useful in automation of administrative tasks (e.g. sending emails, copying files or listing the installed software in a computer), web server scripting, test automation (in UFT One and TestComplete). A VBScript program can be run directly, without the need of compiling it first. VBScript programs run on all Windows computers. The good thing is that VBScript is simple and therefore, easy to learn.

Let us start with learning VB Script variables. A variable is a name or a reference for a memory location that stores some data. We can define our own variable names with a VBScrpt Dim statement. But a variable name has to begin with an alphabet and be less than 256 characters in length. Variables names are not case-sensitive. Some examples of VBScript variable names are num1, x, sum, strCustomer, address_1. Now, let us see the VBScript code to add two numbers. You can use any text editor to type the VB script code

' This is a comment. A comment starts with a single quote.
' VBScript code
Dim x, y, z
' Assign the right hand side value i.e. 1 to variable, x
x = 1
' Assign the value 2 to variable, y
y = 2
' Assign the value of expression x + y to variable, z
z = x + y
' Display the value of z i.e. display 3
MsgBox z

Next, we need to save the code in a text file with .vbs file extension e.g. sum.vbs. There are multiple ways to execute the VBscript:
1) In File Explorer, we can double-click the script.
2) We can open the command window (cmd command). Then, change directory (cd command) to the folder that has our script. Type the script name e.g. sum.vbs and press the Enter key.

Instead of fixed x and y values, we can ask for the numbers from the user. We can modify the above VBScript to the following:

' VBScript code
' Option Explicit mandates defining variables names before use.
Option Explicit
' Define variable names that are meaningful to the reader.
Dim firstNumber, secondNumber, sum
' Assign the firstNumber using inputBox function
' inputBox shows a dialog box with the given prompt and title
firstNumber = inputBox ("Enter a number", "First Number")
' CInt function converts the string entered by the user to an integer value
firstNumber = CInt(firstNumber)
' The last argument given to the inputBox function is the default value
secondNumber = CInt(inputBox ("Enter another number", "Second Number", 0))
sum = firstNumber + secondNumber' Display the value of the sum variable
MsgBox "The sum is " & sum

Want to learn more details and how to debug your VBScript? Please view my VBScript tutorial 1. Thank you.