34
VB Script for QTP I) Introduction: a) Scripting Languages VS Programming Languages b) Basic features of VBScript. c) Hosting Environments & Script Engines d) Creating & running a script e) Comments II) Variables and Constants: a) Vbscript data types b) Assigning values to variables c) Option Explicit Statement d) Naming restrictions for variables e) Scope of Variables f) Array Variables g) Dynamic Arrays h) Dimensional Arrays i) Constants III) Vbscript Operators: a) Operator precedence. b) Arithmetic operators c) Comparison Operators d) Concatenation operators e) Logical operators IV) Flow Control (a. Conditional Statements) V) Flow Control (b. Looping through code) 1

VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

  • Upload
    builien

  • View
    251

  • Download
    0

Embed Size (px)

Citation preview

Page 1: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

VB Script for QTP

I) Introduction: a) Scripting Languages VS Programming Languagesb) Basic features of VBScript.c) Hosting Environments & Script Enginesd) Creating & running a scripte) Comments

II) Variables and Constants:a) Vbscript data typesb) Assigning values to variablesc) Option Explicit Statementd) Naming restrictions for variablese) Scope of Variablesf) Array Variablesg) Dynamic Arraysh) Dimensional Arraysi) Constants

III) Vbscript Operators:a) Operator precedence.b) Arithmetic operatorsc) Comparison Operatorsd) Concatenation operatorse) Logical operators

IV) Flow Control (a. Conditional Statements)V) Flow Control (b. Looping through code)

1

Page 2: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

Chapter- 1 (Introduction)Scripting language vs programming Languages.

S.No Scripting languages Programming languages1 These are interpreted based

languages. These are Compiler based languages.

2 These are light weight languages These are weightful languages.3 Line by line execution happens Total code at a time compilation

happens4. Reduces development time Increases development time5. Creating user interface (UI) is

difficult in scripting languages.Creating user interface (UI) is easy in programming languages.

2

Page 3: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

6 Ex. Scripting language are : VB, Java Script, perl scrip, shell script, python script, Ruby script etc.

Ex programming language are: Basic,COBOL, Portran, c, c++,VC++, Java, VB, etc.

7 Scripting languages have limited support for Graphics Design

Programming languages have rich Support for Graphics Design.

Note: Compilation: Converting high-level language such as English into Machine level language equivalence.

Note2: compilation required for programming languages not for scripting languages.

Note 3: In order to achieve any task (S) less codes are enough in scripting languages than programming languages.

II. VBScript Basic Features.

VB Script was launched in 1997 by Microsoft It is a lightweight version of Microsoft VB programming language. VB script is a platform dependent language. (Works on windows

environment like Windows client operating systems, windows server operating systems, internet explorer, Internet information service (IIS) which is also know as Microsoft web server.

It has huge amount of operators support It supports file system operations. (Creating /updating/viewing/deleting

folders, flat files, word documents, PDF, Xml documents and Excel Sheets.

It supports data base operations.(Data base connections, data validations, Data integrate , ETC.)

It can be integrated with MS Access, SQL Server and oracle etc database management system.

III Purposes of VBScript/Benefits of VBScript

It has several purposes. (It is global concept)

3

Page 4: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

It is used in client side scripting in the web.(We can embed VBScript into HTML Pages, through this we can perform form validations, user validations, etc. ( Internet explorer provides host environment for running VBScript.)

It can be used for server side scripting in ASP (Active Server pages) (IIS Internet information service provides host environment for running Vbscript.)

It is used for Net work Administration. It is used for system administration. It supports file system and data base operations. It can be used for Test automation (QTP adapted VB Script for Test

automation. It can be used for user interface development.

IV WSH (Window Script Host)

WSH is a Microsoft administration tool, WSH creates host environment for running scripts. It is built-in feature in Windows 98 and higher versions.

WSH creates script host environment on windows for VBScript and Jscript (Microsoft(Scripts). If we want to run perl, python like scripts, we have install those scripting engines separately.

For VB ScriptEnvironment Host environment providerMs- windows WSH

Client side (WEB) like form validation Code in HTML

IE

Server side (Web)code in ASP IISTest Automation QTP

V- Creating a Script

1. Open or launch or Start an editor (Ex notepad, VBS Edit and HTML editor etc.)

4

Page 5: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

2. Type the Script (ex; Msgbox “HelloVBScript”3. Save the text file with (. Vbs extension) (Instead of .txt extension)4. Navigate to the file u saved and double click it.5. WSH invokes the vb script engine and runs the script. A message box is

displayed with the message “Hello VB Script”

Chapter 2 (Variables)

Variable: Variable is convenient place holder that refers to a computer memory location where we can store program information.

Def 2: Variable is a named location in computer memory where we can store programming information.

Variable stores in primary storage device such as RAM (Random Access memory).

Variable values may change during the time the script is running.

Declaring Variables:

We declare variables in our script using the DIM, Public and private statements.

We can declare variables explicitly or implicitly.Explicitly declaration Implicitly declarationDim x, city X =100City =”Hyderabad”

X=100City = “Hyderabad”

Implicit declaration is simple but it is not a good practice.

Option Explicit Statement:

It forces explicit declaration of all variables in a split.The option explicit declaration should be the first statement in our script.

5

Page 6: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

Option Explicit ‘ forces declaring of all variables.Dim x, city ‘declaring variablesX=100 ‘declared variableY=200 ‘undeclared variable generates error (if u observe in Dim X y is missing that is why it shows error)

Note: If we use option explicit statement it generates error for undeclared variables, sometimes we may misspell variable names that time option explicit generates error, other wise script does not show any error and provide proper result.

Assigning Values to Variables:

A. Initiation: Assigning values directly in the script. Option explicitDim X, city X=100City = "Hyderabad"Msgbox "value is: "&x

B. Reading: Reading value from input devices.EX:

Option explicitDim xX=Inputbox ("enter a value")Msgbox "Value is : "& X

Purposes of variables or benefits of variables.

For storing results that generated by calculations in the scriptEx: Total =Tickets *price

For storing Data that returned by functions

Ex: x=now

6

Page 7: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

msgbox x

Now is a function we can also pass a comment.For storing any program information.

City = “Chenai”

We can declare multiple variables in a line

Ex: dim a,b,c, city

Or Dim a Dim bDim cDim city

We can assign values to variables in a line.

EX;

X =100Y=200City =”hyderabad’

OrX=100:y=200:city =”Hyderabad”

Naming Restrictions for variables:

Must begin with alphabetic character:

Ex: X, y, city, X9 is correct.9x, 9 wrong.

7

Page 8: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

Cannot contain embedded period. For ex: XYZ.com wrong. Xyzcom, xyx-com correct.

Must not exceed to 255 characters

Must be unique in the scope.

For ex: Case 1: x,y,z, city is correct.

Case 2: x,y,x,city –wrong (with the same name it cannot create a variable must be unique)

Data Types:

Vbscript does not have explicit data types. It has only one data types called a variant. It is a special kind of data type that can contain different kinds of information, depending on how it is used.

For converting data from one data type to another conversion functions available in Vbscript.

Explicit declaration of Data typesEx: (C,c++ etc)

Implicit declaration of Data types(VBScript)

Char Name (trigun)Int Num (99)Float Num (12.47)

Char/Int/float/currency/date etc.Dim statement only.

Scope of Variables:

A variable’s scope is determined by the way we declare it. When we declare a variable within a procedure, only code within the procedure can access or change the value of that variable.

VBscriptDim a, b, n

8

Page 9: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

a=100: b=200n=a+bMsgbox n

Function mul Dim x,y,res x=50: y=23 res=x*a mul=res Msgbox "result is "&mulEnd Function

Call mult=x+bMsgbox t

Note1: In the above example a, b, n, t are script level variables, those can be used in all procedures in the script.(we can use inside the procedure or outside the procedure)

Note2: In the above example x, y, res are procedure level variables, those can be used in that procedure only. (If we use procedure level variable in script level Vbscript generates that value as zero.)

Types of Variable:

Vbscript has two types of variable.

1. Scalar variables : These contain a single value 2. Array Vairbales: These contain a series of values.

Array Variables:

Array variables and Scalar variables are declared in the same way, Expect that the declaration of an array variable uses ( ) following the variable name.

Ex: Dim a(3) Array variable (Dim a (3) stores 4 values starts from ZeroDim b scalar variable (stores one value)

9

Page 10: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

In Vbscript arrays are Zero based.

Assigning values to variables:

We assign data to each of the elements of the array using an index:Ex: Dim a(3)Dim ba(0)=100: a(1)=200: a(2)=300: a(3)=345b=a(2)Msgbox b

Dimensional Arrays:

Arrays are not limited to single dimension. We can have as many as 60 dimensions, although most people can’t comprehend more than 3 or 4 dimensions.

EX:Dim table (5,10)In the above example the table variable is a 2 dimensional array consisting of 6 rows and 11 columns.In a Two-Dimensional array, the first number is always a number of rows; The second number is the number of columns.

Dynamic Arrays:

We can also declare an array whose size changes during the time our script is running. This is called dynamic array.

EX:Dim myarray ()Redim anotherarray ()

Dim a,b, X()ReDim x(3)x(0)=100: x(1)=200: x(2)=300: x(3)=400a=x(3)

10

Page 11: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

msgbox aReDim Preserve x(5)x(4)=999: x(5)=888b=x(2)+x(5)msgbox b

In the above example Redim sets initial value 3.A subsequent Redim statement resizes the array to 5 We have to use Preserve keyword to preserve the contents of the array as the resizing takes place.

Note: There is no limit to number of times we can resize a dynamic array, although if we make an array smaller, we lost the data in the eliminated elements.

Note : Redim is a statement and Preserve is a keyword.

Comments

The comment or argument is the text of any comment we want to include.

Purposes of Comments:

1. For making the script/code understandable 2. For making one or more statements disable for execution.

Syntax:

Rem comment (after the rem keyword, a space is required before comment)Or

Apostrophe (') symbol before the comment

Comment/uncomment a block of statements:

Select block of statements and use short cut key Control +M (for comment)

11

Page 12: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

Select comment block and use short cut key Control + Shift+M (for uncomment).

Constants:

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

Constant can be used for replacing literal (exact) values.

Declaring Constants:

We can create user-defined constants using Const statement.

Ex:

Const myname =”Trigun”Const number=100Const mydate = # 10-10-99 #

We can declare multiple constants by separating each constant name and value with a comma.

Ex:

Const myname =”trigun”, num=100, mydate = #10-10-99#

Variable VS Constantans

Dim mynamemyname =”Trigun”Msgbox mynamemyname =”pinky”Msgbox myname

Const myname =”Trigun”Msgbox mynamemyname =”pinky”*Generates error

Chapter 3 (Operators)

12

Page 13: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

Operators are used for performing mathematical, comparison and logical operations.

Vbscript has full range of operators including arithmetic operators, Comparison operators, Concatenation operators and logical operators.

Operator Precedence:

When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence.

We can use () parentheses to override the order of precedence and force some parts of an expression to be evaluated before others.

When expressions contain operators from more than one category, arithmetic operators are evaluated first, comparison operators are evaluated next and logical operators are evaluated last.

Comparison operators all have equal precedence, this is they are evaluated in the left to right order.

Arithmetic and logical operators are evaluated in the following order of precedence.

1.Arithmetic Operators:

S.No Operator Description1 Exponentiation (^) Raises a number to the power of an

exponent. For ex (10^3)=1000

13

Page 14: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

2 Multiplication (*) Multiplies two numbers. For ex: (10*20)=200

3 Division (/) Divides two numbers and returns a floating-point results. For ex: (10/3)=3.3333333333

4 Integer division (\) Divides two numbers and returns an Integer result. For ex: (10\3)=3

5 Mod operator Divides two numbers and returns only the reminder number. For ex: 10 mod 3=1

6 Addition (+) Sums two numbers for ex: 10+5=157 Subtraction (-) Finds the difference between two numbers

are indicates negative value of a numeric expression. For ex: 10-5 = 5

8 Concatenation (&) Forces string concatenation of two expressions (For ex: trigun&pinky=trigunpinky

2.Comparison Operators:

Operator Description1. Equal to (=) Used to compare expressions

a=100b=200res= a=bmsgbox resIt generates true/false

2. Not equal to (<>)

a=100b=200res= a<>bmsgbox resIt generates true/false

3. Less Than (<) a=100b=200res= a<bmsgbox resIt generates true/false result

4. Greater than (>) a=100b=200res= a>b

14

Page 15: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

msgbox resIt generates true or false results

5. less than or equal to (<=)

a=201b=200res= a<=bmsgbox resIt generates true or false results

6.Greater than or equal to (>=)

a=201b=200res= a>=bmsgbox resIt generates true or false results.

7. Is Operator Object equivalence

3.Concatenation Operators:

Operator Description1. Addition operator (+)

a. If both expressions are numeric then it addsb. If both expressions are strings then it concatenates.c. One expression is numeric and the other is string then it adds

2. Concatenation operator (&)

Forces string concatenation of two expressions.Ex: a="trigun"b="pinky"c=a&bmsgbox c

3. Logical Operators:

Operator Description1. Not Performs logical negation on an expression2. And Performs a logical conjunction on two expressions.

a=10b=20c=15If a<b and c<b Then

msgbox "b is big number"

15

Page 16: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

else msgbox "big is not a big number"

End If3. Or Performs a logical disjunction on two expressions

a=10b=20c=25If a<b or c<b Then

msgbox "b is big number"else msgbox "big is not a big number"

End If4. xor Performs a logical exclusion on two expressions.

a=100b=20c=15If a<b xor c<b Then

msgbox "Pass"else msgbox "Fail"

End If5. Eqv Performs a logical equivalence on two expressions.

a=100b=20c=150If a<b eqv c<b Then

msgbox "Pass"else msgbox "Fail"

End If6. Imp Performs a logical implication on two numbers.

IV) Flow Control (a. Conditional Statements)

16

Page 17: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

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

Using conditional statements, we can use Vbscript code that makes decisions.

In Vbscript two types of conditional statements are available.

1. If…then…else statement2. Select case statement

Usage:

1. Running a statement if a condition is true (single statement)

Dim mydatemydate=#10-10-08#msgbox mydateIf mydate <now Then mydate =nowmsgbox mydate

2. Running a block of statements if condition is true (multiple statements)Dim x,yx=15If x>10 Theny=x+100msgbox ymsgbox "trigun is correct"End If

3. Running a block of statements if a condition is true or running other block of statements if condition is false.

Dim x,yx=1000y=200If x>y Then

msgbox "X is a big number"else msgbox "Y is a big number"

End If

17

Page 18: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

4. Deciding among Several alternatives( if statement)

Dim xx=inputbox ("enter a value")

If x>0 and x<=100 Thenmsgbox "x is a small number"

else if x>100 and x<=500 Thenmsgbox "x is a medium number"

else if x>500 and x<=1000 Thenmsgbox "x is a big number"

else msgbox "x is a grand number"End Ifend ifend if

5. Executing a certain block of statements when two or more conditions are true. (Nested if)

Dim state, regionstate =inputbox ("enter a state")region =inputbox ("enter a region")

If state="ap" Then If region ="telangana" Then

msgbox "dist count is 10"

else if region ="rayalasema" Thenmsgbox "dist count is 4"

else if region ="costal" Thenmsgbox "dist count is 9"elsemsgbox "invalid data"

18

Page 19: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

End IfEnd IfEnd ifEnd if

6. Deciding among Several alternatives (select case statement)

Syntax:

Select case expression

Case “expression” Statements-------------------------

Case “expression”Statements-------------------------

Case else statements---------------------------

End select

Ex:

Option explicitDim x,y,operation,resx=inputbox ("enter x value")y=inputbox ("enter y value")operation=inputbox ("enter an operation")x=cdbl(x)y=cdbl(y)

Select Case operation

Case "add"res =x+y

19

Page 20: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

msgbox "additon of x,y is: "&res

Case "sub"res =x-ymsgbox "sub of x,y is: "&res

Case "mul"res =x*ymsgbox "mul of x,y is: "&res

Case "div"res =x/ymsgbox "div of x,y is: "&res

Case "expo"res =x^ymsgbox "expo of x,y is: "&res

case elsemsgbox "invalid data"End Select

V. Flow Control (a. Looping through code)

20

Page 21: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

o Lopping allows us to run a group of statements repeatedly.

o Some loops repeat statements until a condition is true.o Others repeat statements until a condition is false.o There are also loops that repeat statements a specific

number of times.

The following looping statements are available in Vbscript.

1. Do … Loop: Loops while or until a condition is true.2. While… Wend: Loops while a condition is true.3. For… Next: Uses a counter to run statements a specific

number of times.4. For each … Next: Repeats a group of statements for

each item in a collection or in an array.

1.Do…Loop:1. Repeating statements while a condition is true.Syntax1: Do while conditionStatements - - - - - - - -LoopEx 1:Do while x <5 x=x+1msgbox "hi trigun"&xLoopEx 2:

21

Page 22: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

Do while x<5 x=x+1Window("Flight Reservation").ActivateWindow("Flight Reservation").WinButton("Button").ClickWindow("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set xWindow("Flight Reservation").Dialog("Open Order").WinButton("OK").ClickLoop

Syntax2: Do Statements - - - - - - - -Loop while conditionEx 1:Do msgbox "hi trigun"&xx=x+1Loop while x <5Ex 2:x=1Do Window("Flight Reservation").ActivateWindow("Flight Reservation").WinButton("Button").ClickWindow("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set xWindow("Flight Reservation").Dialog("Open Order").WinButton("OK").Clickx=x+1Loop while x<=5

Ex 3:Do Until x =4 x=x+1

msgbox "hi trigun"&XLoop

Ex4: Do

msgbox "hi trigun"&Xx=x+1

22

Page 23: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

Loop Until x =4

Ex 5:

Do until x=5 x=x+1Window("Flight Reservation").ActivateWindow("Flight Reservation").WinButton("Button").ClickWindow("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set xWindow("Flight Reservation").Dialog("Open Order").WinButton("OK").ClickLoop

2.While … wend statement

Executes a series of statements as long as given condition is true

Syntax:

While conditionStatements----------Wend

Ex1:

While x<5 x=x+1msgbox "hello hyderabad"&x

Wend

Ex 2: While x<5 x=x+1Window("Flight Reservation").ActivateWindow("Flight Reservation").WinButton("Button").ClickWindow("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set xWindow("Flight Reservation").Dialog("Open Order").WinButton("OK").ClickWend

3. For Next Statement

Repeats a group of statements for a specific number of times.

For counter =start to end step (Increment)Statements__--------------------Next

Ex 1:

23

Page 24: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

For x=1 to 5 step 1msgbox "hello hyderabad"&x

Next

Ex 2:For x=1 to 5 step 1Window("Flight Reservation").ActivateWindow("Flight Reservation").WinButton("Button").ClickWindow("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set xWindow("Flight Reservation").Dialog("Open Order").WinButton("OK").ClickNext

3. For Each Next Statement

Repeats a group of statements of each element in an array or a collection

Syntax:

For each item in array Statements---------------Next

Ex 1:

Dim a,b, x(3)a=10: b= 20x(0)="Addition of a,b is: "&a+b x(1)="Sub of a,b is: "&a-b x(2)="Division of a,b is: "&a/b x(3)="Mul of a,b is: "&a*b For each element in xmsgbox elementNext

Ex 2:

Dim mycitymycity =array("hyderabad","mumbai","Delhi", "chennai")For each element in mycity

msgbox elementNext

24

Page 25: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

V. General Examples.Ex 1:Write a script for reading a number & finding out whether the given number is leap year or not?Dim numnum=inputbox ("enter a number")If (num mod 4)=0 Then

msgbox "this is a leap year"else msgbox "this is not a leap year"

End If

Ex 2: Write a program for reading & finding out whether a given number is even number or odd number?Ex :Dim numnum=inputbox ("enter a number")If (num mod 2)=0 Then

msgbox "this is an even number"else msgbox "this is an odd number "

End IfEx 3: display even numbers up to N?Dim num,nn=inputbox ("enter a number")For num=2 to n step 2

25

Page 26: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

msgbox numNextEx 4: display odd numbers up to N?Dim num,nn=inputbox ("enter a number")For num=1 to n step 2msgbox numNext

Ex 5: Read a number and find out length of that number?Dim numnum=inputbox ("enter a number")x=len(num)msgbox x

Ex 6: Display Natural numbers in reverse order up to N?Dim nn=inputbox ("enter a number")For num= n to 1 step -1msgbox numNext

Ex: Dim nn=inputbox ("enter a number")

26

Page 27: VB Script for QTP - Ready To Test.€¦  · Web viewI) Introduction: Scripting Languages VS Programming Languages. Basic features of VBScript. Hosting Environments & Script Engines

For num= 10 to n step 10msgbox numNext

27