Development

Using Selenium with Java and TestNG (2)

Using Selenium with Java and TestNG (2)

DataProvider in TestNG

In this blog, I‘ll introduce how to use TestNG’s DataProvider to create a data driven test. DataProvider is a feature of TestNG which makes TestNG easier to create data driven tests than Junit. It’s pretty easy to use DataProvider. First, let’s add the DataProvider into a test annotation like this:

@Test (dataProvider=”product_to_search”)

public void searchProduct(String keyword) throws InterruptedException

Using Selenium with Java and TestNG (2)That means when the testNG runs the test method “searchProduct”, it will try to find a data provider which is named “product_to_search”. And then, let’s create a data provider method, there are two style of DataProvider like these:

@DataProvider(name=”product_to_search”)public Iterator<Object[]> createData1()  @DataProvider(name=”product_to_search”)public Object[][] createData1()

The first one is returning a Iterator<Object[]>, the other one is returning Object[][] . You can feel free to choose one style of above, let me give you some example code:

The first style:

@DataProvider(name=”product_to_search”)

     public Iterator<Object[]> createData1() {        

//an Array        

String[] astringarray={“chocolate”,”pie”,”cake”,”egg”};        

//chang it to a list of String        

List<String> lines=Arrays.asList(astringarray);                   

//a list of Object and it’s empty        

List<Object[]> data = new ArrayList<Object[]>();                    

 //put something into this list, and return        

for (String line :lines )            

 {                

data.add(new Object[]{line});            

 }            

return data.iterator();         

 }

 The Second style:

 @DataProvider(name=”product_to_search”)    

public Object[][] createData1() {         

return new Object[][] {           

{ “chocolate” },           

{ “pie”},           

{ “cake”},          

};        

}

And the dataProvider supports parallel execution:

@DataProvider(name=”product_to_search”,parallel = true)

This will make the tests run much more quickly, but do remember that web driver is not thread safe and you can use the code I provided in my last blog to make it become thread safe.

Some further questions and thoughts about data driven testing:

  1. What if I want to put the test data into some files such as a properties file or an excel file, or even put the test data into xml, yaml, or any other format?

My thought is: if we would like to put the data in some files, we can simply create some file read/write helper class in our data provider, and we use these classes to read the file. However, although there are benefits to put the data into separate files, there are also some problems: The people who read your code (and maybe he need to maintain or update your code) will get confused about how to find the test data. When the test scripts grow large, put everything in one file will be a bad solution. Even you choose to put the data in several files, you may face hundreds of files and it makes you headache. So please do consider what will happen when some other people read your code, could they find the relationship between test case and test data easily? If the test data is not very large, I will suggest to put data provider in the same file of the test case and hard code data. This at least will reduce the confusion. And fortunately, we write test scripts but not web application, we can afford the time of rebuilding the java code when we need to modify the data. I think you should make the decision carefully according to the situation of your project.

  1. What if I put all data provider into one class?

Someone like to put everything in one class, that is ok, but make sure that testNG can find it. And usually I’m not suggest to create a huge class with thousands of lines. In test script creating, the basic principle is to make the people who read your code feel happy but not confused.

  1. Could I create one data provider to provide test data for many test methods?

Yes, Data Provider can receive an argument “Method” which is the name of the method called data provider. You can check this argument and provide different data for different methods. Again, I’ll not suggest to make the dataProvider too huge. And you can get more information in TestNG’s official documents.

The full code of my small test framework build with selenium is here: https://github.com/zhangting85/simpleWebtest

 

Thoughts on “Using Selenium with Java and TestNG (2)”

  1. i am created website..now i have to perform automation testing on my website..for that i am using selenium Webdriver ..so an u tell me how i check weathr my expected result is same as actual? and print actual result??

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Colin Zhang

More from this Author

Subscribe to the Weekly Blog Digest:

Sign Up
Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram