Part 4: QTP and VBSCript | Conditional Statements in VBScript
Usually, the test cases you work on wouldn’t have a straight linear flow. You would normally come across many conditions in your test case flow and based on the results of these conditions you would be required to perform different actions. In VBScript this can be achieved using the following 2 conditional statements –
- # If…Then…Else statement
- # Select Case statement
Let’s see how to use these conditional statements in our scripts.
If… Then… Else statement
The If Then Else statement evaluates whether a condition is true or false, and then based upon the result certain set of statements are run. Let’s see a basic example for this.
dtSomeDate = #01/01/2012# 'Check if the given date was in the past If dtSomeDate < Now Then msgbox "The given date is in past" End If
In the above example, only one condition was checked in the If statement. If you want, you can check multiple conditions using multiple ElseIf statements. Let’s see an example for this.
'Code to check multiple conditions using Else If statement Dim sMonth 'Find the month from today's date sMonth = Month(Now) If sMonth = 1 Then msgbox "Month is January" ElseIf sMonth = 2 Then msgbox "Month is February" ElseIf sMonth = 3 Then msgbox "Month is March" '..... so on .... End If
You can check multiple conditions within a single If statement using And & Or operators.
'Find if a given number is given number if even and is less than 100 Dim iNum iNum = 50 If (iNum mod 2 = 0) and (iNum < 100) Then msgbox iNum & " is a even number less than 100" End If
Nested If Statements Nested If statement is where you can have an If statement within an If statement.
'Find if a given number is given number if even and is less than 100 Dim iNum iNum = 50 If iNum mod 2 = 0 Then 'Check If number is less than 100 or not If iNum < 100 Then msgbox iNum & " is an even number less than 100" Else msgbox iNum & " is an even number greater than 100" End If Else msgbox iNum & " is an odd number" End If
Select Case statement
Select Case statement works on a single expression which is evaluated at the beginning on the Select Case statement. The result of the expression is then compared with the values for each Case in the structure. Select Case statement is similar to If Then Else but it makes the code more readable. Let’s see an example of Select Case statement.
Dim iWeek iWeek= 3 Select Case iWeek Case 1 msgbox "Today is Sunday" Case 2 msgbox "Today is Monday" Case 3 msgbox "Today is Teusday" Case 4 msgbox "Today is Wednesday" Case 5 msgbox "Today is Thursday" Case 6 msgbox "Today is Friday" Case 7 msgbox "Today is Saturday" Case Else msgbox "Invalid Number" End Select
Similar to nested if statements, you can also have nested select case statement. You can also create select case within an if statement and vice-verse.
This was all about the conditional statements in VBScript. You can visit QTP VBScript Tutorials page to read all the tutorials on VBScript.
If you enjoyed this article, you can join our blog to get new articles delivered directly in your inbox.
To check out more tutorials, visit our QTP Tutorials page. You can also check the Archives page to view the list of all our articles.