Pages

Wednesday, 23 September 2015

for Loop -Selenium WebDriver

for Loop - For Selenium WebDriver


Loops(for loop, while loop, do while loop) have very important role in selenium webdriver test case development with java or any other languages.

As you know, sometimes you need to perform same action multiple times(Example : 100 or more times) on your web page. Now if you will write multiple lines of code to perform same action multiple times then it will increase your code size. For the best practice, you need to use loops in this kind of situations.

for Loop

There are three parts inside for loop.
1. Variable Initialization
2. Condition To Terminate
3. Increment/Decrements variable.
for loop will be terminated when condition to terminate will becomes false.

Example :


for(int i=0; i<=3; i++){
 System.out.println("Value Of Variable i is " +i);
}

Bellow given example is simple example of for loop. run it in your eclipse and verify the result.

public class forloop {

 public static void main(String[] args) {
  for(int i=0; i<=3; i++){ //This loop will be executed 4 times
   System.out.println("Value Of Variable i is " +i);
  }
 
  System.out.println("");
  int i=0;
  int k = 200;
  for(int j=3; j>=i; j--){ //This loop will be executed 4 times
   System.out.println("Value Of Variable j is " +j);
   k = k-10;
  }
                System.out.println("");
  System.out.println("Value Of Variable k is " +k);

 }

}

When you will run above given example, you will get bellow given output.
Value Of Variable i is 0
Value Of Variable i is 1
Value Of Variable i is 2
Value Of Variable i is 3

Value Of Variable j is 3
Value Of Variable j is 2
Value Of Variable j is 1
Value Of Variable j is 0

Value Of Variable k is 160

No comments:

Post a Comment