QTP VBScript Input/Output Operations


6.1 InputBox Function


Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns the contents of the text box.

Example:

Dim Input
Input = InputBox("Enter your name")
MsgBox ("You entered: " & Input)

6.2 MsgBox Function


Displays a message in a dialog box, waits for the user to click a button, and returns a value indicating which button the user clicked.

Example:

Dim MyVar
MyVar = MsgBox ("Hello World!", 65, "MsgBox Example")

' MyVar contains either 1 or 2, depending on which button is clicked.

VB Script Constants

A constant is a meaningful name that takes the place of a number or string and never changes.

7.1 Creating Constants

We create user-defined constants in VBScript using the Const statement. Using the Const statement, we can create string or numeric constants with meaningful names and assign them literal values.

Const statement

Declares constants for use in place of literal values.

Example:

Const MyString = "This is my string."
Const MyAge = 49
Const CutoffDate = #6-1-97#

Note that String literal is enclosed in quotation marks (" ").

Represent Date literals and time literals by enclosing them in number signs (#).

We declare multiple constants by separating each constant name and value with a comma. For example:
Const price= 100, city= “Hyderabad”, x= 27

Conditional Statements

We can control the flow of our script with conditional statements and looping statements.

Using conditional statements, we can write VBScript code that makes decisions and repeats actions. The following conditional statements are available in VBScript:

1) If…Then…Else Statement
2) Select Case Statement

8.1 Making Decisions Using If...Then...Else


The If...Then...Else statement is used to evaluate whether a condition is True or False and, depending on the result, to specify one or more statements to run.

Usually the condition is an expression that uses a comparison operator to compare one value or variable with another.

If...Then...Else statements can be nested to as many levels as you need.

8.1.1 Running a Statement if a Condition is True (single statement)


To run only one statement when a condition is True, use the single-line syntax for the If...Then...Else statement.

Dim myDate
myDate = #2/13/98#
If myDate < Now Then myDate = Now
8.1.2 Running Statements if a Condition is True (multiple statements)


To run more than one line of code, we must use the multiple-line (or block) syntax. This syntax includes the End If statement.
Dim x
x= 20
If x>10 Then
msgbox "Hello G.C.Reddy"
msgbox "x value is: "&x
msgbox "Bye Bye"
End If

8.1.3 Running Certain Statements if a Condition is True and Running Others if a Condition is False


We can use an If...Then...Else statement to define two blocks of executable statements: one block to run if the condition is True, the other block to run if the condition is False.
Example:

Dim x
x= Inputbox (" Enter a value")
If x>100 Then
Msgbox "Hello G.C.Reddy"
Msgbox "X is a Big Number"
Msgbox "X value is: "&X
Else
Msgbox "GCR"
Msgbox "X is a Small Number"
Msgbox "X value is: "&X
End If

8.1.4 Deciding Between Several Alternatives


A variation on the If...Then...Else statement allows us to choose from several alternatives. Adding ElseIf clauses expands the functionality of the If...Then...Else statement so we can control program flow based on different possibilities.
Example:
Dim x
x= Inputbox (" Enter a value")

If x>0 and x<=100 Then Msgbox "Hello G.C.Reddy" Msgbox "X is a Small Number" Msgbox "X value is "&x Else IF x>100 and x<=500 Then Msgbox "Hello GCR" Msgbox "X is a Medium Number" Else IF x>500 and x<=1000 Then Msgbox "Hello Chandra Mohan Reddy" Msgbox "X is a Large Number" Else Msgbox "Hello Sir" Msgbox "X is a Grand Number" End If End If End If
8.1.5 Executing a certain block of statements when two / more conditions are True (Nested If...)


Example:

Dim State, Region
State=Inputbox ("Enter a State")
Region=Inputbox ("Enter a Region")

If state= "AP" Then
If Region= "Telangana" Then
msgbox "Hello G.C.Reddy"
msgbox "Dist count is 10"

Else if Region= "Rayalasema" Then
msgbox "Hello GCR"
msgbox "Dist count is 4"

Else If Region= "Costal" Then
msgbox "Hello Chandra mohan Reddy"
msgbox "Dist count is 9"

End If
End If
End If
End If

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.