Looping Through Code

Loop Statements are used for repeat the code

All Loop Structures all most all same, with respect to our convince, we can select and use them

VB Script has 4 types of Loop structures

1) For...Next Statement

A For...Next loop instructs QuickTest to perform one or more statements a specified number of times. It has the following syntax:


For counter = start to end [Step step]

statement

Next

counter: The variable used as a counter for the number of iterations.


Start: The start number of the counter.

End: The last number of the counter.

Step: The number to increment at the end of each loop.Default=1 (optional)


Statement: A statement, or series of statements, to be performed during the loop.


In the following example, QuickTest calculates the factorial value of the number of passengers using the For statement:

passengers = Browser("Mercury Tours").Page("Find Flights").WebEdit("numPassengers").GetROProperty("value")
total = 1
For i=1 To passengers
total = total * i
Next
MsgBox "!" & passengers & "=" & total

2) For...Each Statement

A For...Each loop instructs QuickTest to perform one or more statements for each element in an array or an object collection. It has the following syntax:

For Each item In array

statement

Next


item: A variable representing the element in the array.


Array: The name of the array.


Statement: A statement, or series of statements, to be performed during the loop.

The following example uses a For...Each loop to display each of the values in an array:

MyArray = Array("one","two","three","four","five")
For Each element In MyArray
msgbox element

3) Do...Loop Statement

The Do...Loop statement instructs QuickTest to perform a statement or series of statements while a condition is true or until a condition becomes true. It has the following syntax:

Do [{while} {until} condition]

statement

Loop

condition: A condition to be fulfilled.

statement:A statement or series of statements to be performed during the loop.


In the following example, QuickTest calculates the factorial value of the number of passengers using the Do...Loop:

passengers = Browser("Mercury Tours").Page("Find Flights").WebEdit("numPassengers").GetROProperty("value")
total = 1
i = 1
Do while i <= passengers
total = total * i
i = i + 1
Loop
MsgBox "!" & passengers & "=" & total

4) While...Wend Statement

A While...Wend statement instructs QuickTest to perform a statement or series of statements while a condition is true. It has the following syntax:

While condition

statement

Wend

condition:A condition to be fulfilled.

statement: A statement or series of statements to be executed during the loop.

In the following example, QuickTest performs a loop using the While statement while the number of passengers is fewer than ten. Within each loop, QuickTest increments the number of passengers by one:

passengers = Browser("Mercury Tours").Page("Find Flights").WebEdit("numpassengers").GetROProperty("value")

While passengers < 10

passengers = passengers + 1

Wend

msgbox("The number of passengers in the party is " & passengers)

0 comments:

Post a Comment