Sunday, 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:

Saturday, February 15, 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.

Sunday, February 9, 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.


Sunday, February 2, 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.