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();
  }
 
}

7 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. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.


    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

    ReplyDelete
  5. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.

    ccna training in chennai



    ccna training in bangalore


    ccna training in pune

    ReplyDelete
  6. I feel that this blog article contains very nice information. Thanks for sharing this kind of nice artices.Happy to read this blog. Kindly Vist us @ Ramanathapuram Hospital List Hospitals in Ramanathapuram

    ReplyDelete