Friday, 27 December 2013

DataDrivenFramework Part 2


Working with Flat Files:
The format for Excel Data is shown below :

The Following Example Demonstrates how to access data from excel
Selenium Code along with TestNG Framework:
For Reading Flat Files : Excel and CSV
package sample;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import atu.ddf.exceptions.DataDrivenFrameworkException;
import atu.ddf.file.CSVFile;
import atu.ddf.file.ExcelFile;
import atu.ddf.selenium.SeleniumTestNGHelper;

 * @author Automation Tester
public class ReadingFromFaltFile {
       WebDriver driver;

       public void init() {
              driver = new FirefoxDriver();
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

       // login into WordPress web site and publish a new Post
       // Another Example of Data Driven while working with Excel files having .xls extension
       @Test(dataProvider = "iteratorTestData")
       public void addPost(String username, String password, String title,
                     String content) throws InterruptedException {
              // Open App
              // Enter UserName
              // Enter Password
              // Click on Submit button
              // Enter Title
              // Enter Content
              // Click on Publish Button

       //Another Example of Data Driven Using CSV File
       @Test(dataProvider = "csvData")
       public void csvDataExample(String username, String password, String title)
                     throws InterruptedException {
              System.out.println("Username: " + username + ": password: " + password
                           + ": title: " + title);

       // Another Example of Data Driven while working with Excel files having .xlsx extension
       @Test(dataProvider = "objectTestData")
       public void registerUser(String username, String gender, String location,
                     String language, String age, String email) {
              System.out.println("Username: " + username + ": Gender: " + gender
                           + ": Location: " + location + ": Language: " + language
                           + ": Age: " + age + ": Email: " + email);

       //Example of 2-Dimensional Object Array DataProvider
       @DataProvider(name = "objectTestData")
       public Object[][] data() throws DataDrivenFrameworkException {
              String excelResource = System.getProperty("user.dir")
                           + System.getProperty("file.separator") + "resources"
                           + System.getProperty("file.separator") +"ExcelTestData.xlsx";
              // provide your excel file path here
              ExcelFile excelFile = new ExcelFile(excelResource);
              // provide the Sheet name
              // provide the Column Name where Test Case names will be given
              // provide the Test Case Name
              List<List<String>> data = excelFile
              // SeleniumTestNGHelper is a utility class that will help in converting
              // the Test data into TestNG Data Provider supported format
              // One such format here is a 2-Dimensional Object Array
              return SeleniumTestNGHelper.toObjectArray(data);

       //Example of DataProvider whose return type is Object Array Iterator
       @DataProvider(name = "iteratorTestData")
       public Iterator<Object[]> data1() throws DataDrivenFrameworkException {
              String excelResource = System.getProperty("user.dir")
                           + System.getProperty("file.separator") + "resources"
                           + System.getProperty("file.separator") +"ExcelTestData.xls";
              ExcelFile excelFile = new ExcelFile(excelResource);
              List<List<String>> data = excelFile.getDataUsingTestCaseName("AddPost");
              return SeleniumTestNGHelper.toObjectArrayIterator(data);

       @DataProvider(name = "csvData")
       public Object[][] csvData() throws DataDrivenFrameworkException {
              String csvResource = System.getProperty("user.dir")
                           + System.getProperty("file.separator") + "resources"
                           + System.getProperty("file.separator") +"CSVTestData.csv";
              CSVFile csvFile = new CSVFile();
              List<List<String>> data = csvFile.getTestData(csvResource);
              return SeleniumTestNGHelper.toObjectArray(data);

Go To: DataDrivenFramework Part 3  - Contains Details regarding Upcoming Releases and Features


  1. can you please provide the SeleniumTestNGHelper class

    1. Hi,

      The download links are provided in the main page of Data Driven Framework.


  2. I needed to thank you for this fantastic read!! I
    definitely enjoyed every little bit of it. I have got you book marked to check out
    new stuff you post…

    Also visit my website - how to open csv file in excel, ,

  3. Hi,

    I used your code for CSV DDF testing, but failed with error:
    java.lang.RuntimeException: java.lang.NoSuchMethodError: org.apache.commons.csv.CSVFormat.parse(Ljava/io/Reader;)Ljava/lang/Iterable;

    I've downloaded commons-csv-1.0-20140721.202737-298.jar and added it in Build path. Not clear if this is right one. Could you please advise? Thanks.


    1. Hi,

      The initial version was commons-csv-1.0-SNAPSHOT , which is a snapshot ! I suppose the method is no more available in the new release. we will fix the issue. Thank you :)


    2. this is "The Best" data driven tutorial which i have seen so far:)
      Congratz.. Can we have same kind for keyword driven as well :)

  4. Hi,

    SeleniumTestNGHelper class is missing from ATU_DDF_Example. Can you please provide a download link for it?


  5. Engineering students who are interested in inplant training in chennai can contact us. We provide real time inplant training in chennai for aspiring students.

  6. Hello Admin, thank you for the informative post. At Fita academy, we provide the best IT trainings for various courses. You can check our Fita Chennai review page to know more about our quality and standards.

  7. HI I have just tried to use the above code to execute reading data from xl file like (.xlsx) but it is showing an error as

    *DataDrivenFrameworkException cannot be resolved to a type* and
    *CSVFile cannot be resolved to a type* *SeleniumTestNGHelper cannot be resolved*. I can not able to resolve it. could you please help me out to resolve and execute it smoothly.

    Thank you.

  8. Also the error *ExcelFile cannot be resolved to a type*

  9. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.

  10. Well explained. Got to learn new things from your Blog on Appium.Appium training in chennai