Tuesday 3 July 2018

TestNG Data Provider

When you need to pass complex parameters for example created from Java (complex objects, objects read from a property file or a database, etc…), in such cases parameters can be passed using Dataproviders. A Data Provider is a method annotated with @DataProvider. A Data Provider returns an array of objects.

Let us check out the same Search examples using Dataproviders.

Video Tutorial -
 

How to do it ?


1)  Define the method searchValue() which is defined as a Dataprovider using the annotation. This method returns array of object array.

2) Add a method test() to your DataProviderTest class. This method takes two strings as input parameters.

3) Add the annotation @Test(dataProvider = “search”) to this method. The attribute dataProvider is mapped to “search”.

4) Run the below code as testNG test case then the test will execute two times as we are passing two Data Values.



package test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
 
import org.testng.annotations.DataProvider;
 
import org.testng.annotations.Test;
 
public class TestNGDataProvider {
 
 private static WebDriver driver;
 
  @DataProvider(name = "search")
 
  public static Object[][] searchValue() {
 
        return new Object[][] { { "appium" }, { "selenium" }};
 
  }
 
  // Here we are calling the Data Provider object with its Name
 
  @Test(dataProvider = "search")
 
  public void test(String searchtext) throws Exception {
 
   System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  
   driver=new ChromeDriver();
      
      driver.get("http://www.qaautomated.com");
 
      Thread.sleep(5000);
      driver.findElement(By.xpath("*//input[@class='search-field']")).sendKeys(searchtext);
      
      driver.findElement(By.xpath("*//input[@class='search-submit']")).click();
 
      driver.quit();
  }
 
}

21 comments:

  1. Your site has a lot of useful information for myself. I visit regularly. Hope to have more quality items.
    ludo king

    ReplyDelete
  2. Nice Sharing..! I have been following you for a couple of months now but this is my first time commenting on a blog post. Thank you for sharing your knowledge and experience with us. Keep up the good work. Already bookmarked for future reference.

    Regards

    software quality assurance companies

    ReplyDelete
  3. Awesome blog. Thanks for sharing such a valuable information.Keep giving like this.
    Best Selenium Training Institute In Hyderabad | Online Selenium Training

    ReplyDelete
  4. Hello,
    I just come through your blog while searching more about the software testing. Read your blog and love the way you have implemented the unique content about software testing and other testing related information. Thanks for sharing this and I will wait for your next updates. Keep it up!

    Regards


    software development company

    ReplyDelete
  5. Wonderful blog!!! Thanks for your information… Waiting for your upcoming data.

    Article submission sites
    Guest posting sites

    ReplyDelete
  6. Good Post and thanks for sharing this things with us. Get the Best Selenium Training Institute in Bangalore

    ReplyDelete
  7. This is very great thinks. It was very comprehensive post and powerful concept. Thanks for your sharing with as. Keep it up...
    Software Testing Training in Chennai | Software Testing Training Institute in Chennai

    ReplyDelete

  8. Thanks for sharing this information!
    I totally agree with you. Your information is very interesting and important. I really like this information.
    Our easy web plans company is famous in Advanced AWS Online Training Institutes in Hyderabad.
    If you want to see our training venue then click on links:
    https://www.futureqtech.com/aws-online-training.php
    Call Now: 9581111796
    Drop Mail: online@futureqtech.com

    ReplyDelete
  9. Thank you for excellent article.You made an article that is interesting.
    Best AWS certification training courses. Build your AWS cloud skills with expert instructor- led classes. Live projects, Hands-on training,24/7 support.
    https://onlineidealab.com/aws-certification/


    ReplyDelete
  10. Thanks for sharing,you have done a very good job.Keep it up.The content is very interesting and impressive.If you are looking for any online training visit our site.
    Best Big Data Hadoop Online Training

    ReplyDelete