Friday, November 7, 2014

Selenium:TestNG factory class with example

TestNG factory class with example 

 http://roadtoautomation.blogspot.in/2014/08/testng-factory-class-with-example.html


@DataProvider is used to pass different test data to a test method
@Factory is used to create different instance of Test class and is useful annotation when you run test multiple time.
you can find examples with description for both at : http://testng.org/doc/index.html


DataProvider Example:

public class DataProviderDemo {

    @DataProvider(name="myDataProvider")
    public Object[][] data(){
        return new Object[][]{ {"adf",5.0f}, {"dfds",4.0f} };
    }
    
    @Test(dataProvider="myDataProvider")
    public void method1( String strData ,float floatData){
        Reporter.log("String "+strData);
        Reporter.log("float "+floatData);
    }
  
}


Factory Example:

public class FactoryDemo {
   
    String strData;
    float floatData;
   
    public FactoryDemo(String string, float f){    
        strData = string;
        floatData = f;      
    }
   
    @Factory
    public static Object[] data(){
        return new Object[]{  new FactoryDemo("adf",5.0f),  new FactoryDemo("dfds",4.0f)   };
    }
      
    @Test()
    public void method1(){
        Reporter.log("String "+strData);
        Reporter.log("float "+floatData);
    }  
}

No comments:

Post a Comment