Sunday 26 October 2014

Parameterizing Tests in Junit Selenium

Parameterizing Tests :
Parameterizing tests in Junit selenium is a flexible thing. When we need test data to be supplied from outside not to be hard-coded then here we use parameterizing tests.

Parametrizing tests needs 4 steps :
Step 1:
Standard annotation should be written above the class
@RunWith(Paramterized.class)

Step 2:
Declare some global variables which will be the input data to the function in test.

Step 3:
Build constructor of that class, pass variables inside it to initialize the class with those declared global variables means whenever the object of the class is made then the object will have the passed parameters.

Step 4:
Make another annotation known as @Parameters, it should be static method.

Here is one parameterized test showing all steps :
import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

//step 1
@RunWith(Parameterized.class)
public class SecondTest {
   
    //step 2
    public String bankName;
    public int actNum;
   
    //step 3
    public SecondTest(String bankName,int actNum){ //here , whenever the object of class is made,then the object will have a bankName and actNum.
        this.bankName=bankName;
        this.actNum=actNum;
        //here, this keyword is used to distinguish between the local & global variable, it means here we are initializing the global variable to local variable.
      }
   
    @Test
    public void checkBalance(){
       
        /*String bnk="citi";
        int act=1233;*/
       
        //bnk name
        //act number
       
        System.out.println("Executing the test with "+ bankName+ "--" +actNum);
    }
   
    //step 4
    @Parameters
    public static Collection<Object[]> getData(){ //here Collection<Object[]> is the return type
        Object data[][] = new Object[2][2];
       
        //first row
        data[0][0] = "HSBC";
        data[0][1] = 1234;
       
        //second row
        data[1][0] = "Citi";
        data[1][1] = 123411;       
               
        return Arrays.asList(data); //It will convert the 2-Dimensional array into arraylist
        //returning the object of arraylist over here and here arraylist is of type collection
        //Arrays-inbuilt class
        //aslist - static function
    }

}



No comments:

Post a Comment