Road to data driven testing in webdriver java TestNg Part 1

In this post I am going to see you, how to perform data driven testing in java webdriver. In this post I read data from external excel file and use those data in test script.
For this I use java “poi” jar file to read excel file. Using poi java library I created below java class to read data from excel file and return data as a array list.


package com.webdriver.test;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadXLS {

     public List getData() {

           String path = "DataSheet.xlsx";
           List dataList = new ArrayList();
           FileInputStream fis = null;
           try {
               fis = new FileInputStream(new File(path));
               XSSFWorkbook workbook = new XSSFWorkbook(fis);
               XSSFSheet sheet = workbook.getSheet("TestData");
               java.util.Iterator rows = sheet.rowIterator();

               while (rows.hasNext()) {
                   XSSFRow row = ((XSSFRow) rows.next());
                   // int r=row.getRowNum();
                   java.util.Iterator cells = row .cellIterator();
                   int i = 0;
                   String[] testData= new String[3];
                   while (cells.hasNext()) {

                       XSSFCell cell = (XSSFCell) cells.next();
                       String value = cell.getStringCellValue();
                       if (!value.equals(null)) {
                            testData [i] = value;
                            i++;
                       }
                   }
                   dataList.add(testData);
               }
           }
           catch (Exception e) {
               e.printStackTrace();
           }
           return dataList;
     }

}


Note: you need to add all poi jar file to class path of your project.
Here I take a example where I search and verify country by using wikipedia. For country I have created a data sheet ”DataSheet.xlsx” as below. 

Country Description
India This article is about the Republic of India
Brazil Brazil bounded by the Atlantic Ocean on the east
Canada Canada is a North American country 
England England is a country that is part of the United Kingdom

Below is java webdriver test case for wikipedia country search. In this test I call “ReadXLS” function to read data from xls file.
package com.webdriver.test;

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.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class SearchCountry {

    private WebDriver driver;
    private String baseUrl;

    @BeforeSuite
    public void setUp() throws Exception {
         driver = new FirefoxDriver();
         baseUrl = "http://www.wikipedia.org/";
         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testSearchCountry() throws Exception {
         driver.get(baseUrl + "/wiki/Main_Page");
         ReadXLS readXls = new ReadXLS();
         List dataList = readXls.getData();
         for (int i = 1; i < dataList.size(); i++) {
             String[] testCase = new String[5];
             String[] test = (String[]) dataList.get(i);
             String countryName = test[0];
             String countryDesc = test[1];
             driver.findElement(By.id("searchInput")).clear();
             driver.findElement(By.id("searchInput")).sendKeys(countryName);
             driver.findElement(By.id("searchButton")).click();
             String str = driver.findElement(
             By.xpath("//h1[@id='firstHeading']/span")).getText();
             System.out.println(countryDesc);
             Assert.assertTrue(str.contains(countryName));
         }
    }
                   

    @AfterSuite
    public void tearDown() throws Exception {
        driver.quit();
    }
}

When we execute above test case all country search and description should be print on console.

31 comments:

  1. Thanks for posting, this is verfy helpful for reading xls file and data driven testing

    ReplyDelete
  2. Hi,

    I want to tell you once I executed above program as u explained I got following error. Please help me to overcome.

    java.lang.NullPointerException
    at selenium.ReadXLS.getData(ReadXLS.java:47)
    at selenium.SearchCountry.testSearchCountry(SearchCountry.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:644)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:546)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:700)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1002)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:137)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:121)
    at org.testng.TestRunner.runWorkers(TestRunner.java:908)
    at org.testng.TestRunner.privateRun(TestRunner.java:617)
    at org.testng.TestRunner.run(TestRunner.java:498)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:329)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:324)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:296)
    at org.testng.SuiteRunner.run(SuiteRunner.java:201)
    at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:915)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:879)
    at org.testng.TestNG.run(TestNG.java:787)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:75)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)

    error is coming from following line which is in the ReadXLS class.
    :-> java.util.Iterator rows = sheet.rowIterator();

    ReplyDelete
  3. Thanks its working fine !!

    Fernando try below steps
    1)Give the correct path of the excel sheet in ReadXLS.java class.

    String path = "YOUR EXCEL SHEET PATH IN SYSTEM";

    2)Your excel workbook should have a sheet name as "TestData" in which content should be there.
    (Genrally if you create new excel you will have (sheet1,sheet2,sheet3....so rename it)

    XSSFSheet sheet = workbook.getSheet("TestData");

    ReplyDelete
  4. You are right its working correctly without an error.
    Thank you for the wonderful example..

    Are there any article for the reporting..??

    01. excel reporting.
    02. TestNG reporting.

    Scenario 01

    let's say there is one TestCase to execute. It is dataDrive testing with 1500 data. end of this test case I need to write on the excel sheet wt happened to these 1500 data or sub test cases.

    ex: - record no 1 is pass or failed
    record no 2 is pass or failed
    record no 1499 is pass or failed
    record no 1500 is pass or failded.


    scenario 02 : -

    like above scenario there are more Test cases like that with Data Driven testing. Then end of the project run how we get the report through TestNG...??

    ReplyDelete
    Replies
    1. For above two scenario you need to use below approaches:

      Create a java code to write data in excel or csv file.
      Use exception handling with throws keyword.
      At starting of test methods create a variable "status" of string type with value "Failed"
      At the end of test method set status value "Pass"
      Put all test case code into try block and at the end of try block call created function and pass "status" to write value in excel sheet or csv file.
      Also you need to create two catch block one for "Exception" and other for "Error" handling again call created function into both catch block.

      Now if test fail then never set status value "pass" and it goes to catch block and write status fail in excel file.
      Also For testNG report testNg automatically generate in test-output folder.

      Note" you need to throw Exception in your methods so that it fails when any exception generate

      Delete
  5. Hello,
    i m facing a error at ReadXLS unable to find

    ReplyDelete
  6. package org;

    import java.io.File;
    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.Assert;
    import org.testng.annotations.AfterSuite;
    import org.testng.annotations.BeforeSuite;
    import org.testng.annotations.Test;
    import jxl.Sheet;
    import jxl.Workbook;
    import org.apache.poi.hssf.usermodel.*;
    public class SearchCountry {
    private WebDriver driver;
    private String baseUrl;

    @BeforeSuite
    public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.wikipedia.org/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testSearchCountry() throws Exception {

    driver.get(baseUrl + "/wiki/Main_Page");
    ReadXLS readXls = new ReadXLS();//Unable find the ReadXLS class i added poi jar to classpath .
    List dataList = readXls.getData();

    for (int i = 1; i < dataList.size(); i++) {
    String[] testCase = new String[5];
    String[] test = (String[]) dataList.get(i);
    String countryName = test[0];
    String countryDesc = test[1];

    driver.findElement(By.id("searchInput")).clear();
    driver.findElement(By.id("searchInput")).sendKeys(countryName);
    driver.findElement(By.id("searchButton")).click();
    String str = driver.findElement(
    By.xpath("//h1[@id='firstHeading']/span")).getText();
    System.out.println(countryDesc);
    Assert.assertTrue(str.contains(countryName));
    }
    }


    @AfterSuite
    public void tearDown() throws Exception {
    driver.quit();
    }
    }

    ReplyDelete
    Replies
    1. Hello sree kanth

      ReadXLS class is not in poi jar file,I created this class with the help of poi jar.

      You should see ReadXLS class in this post, First code is related to this.

      Delete
  7. Please help me what we need to jar files.i tried poi jar file and selenium server,selenium client ,testng jar file used.But not working properly.please help me how to use jar file.some time i used argument type to list .but working.please suggestion me .

    ReplyDelete
  8. hi, i need help, how write the result in existing excel sheet, can you plz help....

    ReplyDelete
  9. hi, could you post the webdriver java code for updating the status as pass/fail against the data rows that's fetched..

    ReplyDelete
  10. Hi
    I have downloaded the poi jar files from

    but all the below functions in the tests are having compilation errors I mean Im not able to import the files
    XSSFWorkbook,XSSFSheet,XSSFRow,XSSFCell

    can anyone help ??
    Thanks

    ReplyDelete
  11. hi..

    Can you provide the code to write the result to Excel sheet for the data set which has been passed from the Excel to the Selenium Webdriver. ?

    ReplyDelete
    Replies
    1. Use above function with some additional modification, while reading data from xls sheet store row number of corresponding data in array list.

      create a function for writing xls file using row number.

      you should have use exception handling in your code with throws, If your code executed for set of data just update pass in xls file and in failed condition using catch block you update failed status for that row.

      Delete
  12. I'm getting an error after giving a correct path of DataSheet.xlsx file...
    java.lang.NullPointerException
    at test.ReadXLS.getData(ReadXLS.java:26)
    at test.SearchCountry.testSearchCountry(SearchCountry.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

    ReplyDelete
    Replies
    1. package com.tarnea;

      import java.io.File;
      import java.io.FileInputStream;

      import jxl.Sheet;
      import jxl.Workbook;

      import org.testng.annotations.Test;
      import org.openqa.selenium.By;
      import org.openqa.selenium.OutputType;
      import org.openqa.selenium.TakesScreenshot;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.testng.annotations.AfterTest;
      import org.testng.annotations.BeforeTest;
      import org.testng.annotations.Listeners;

      import atu.testng.reports.listeners.ATUReportsListener;
      import atu.testng.reports.listeners.ConfigurationListener;
      import atu.testng.reports.listeners.MethodListener;


      //@Test @Listeners({ ATUReportsListener.class, ConfigurationListener.class,MethodListener.class })
      public class Excel_Data
      {

      public WebDriver driver;
      Sheet s;
      int k=0;
      public String uname;
      public String pass;

      //This is for establishing the driver connection
      @BeforeTest(description=" : This is for establishing the driver connection")
      public void sales_Order1_setUp() throws Exception
      {
      driver = new FirefoxDriver();
      }

      @Test(priority=1)
      public void Login_Test() throws Exception
      {
      driver.get("http://103.19.88.39/");
      driver.manage().window().maximize();
      FileInputStream fi=new FileInputStream("D:/Automation/Life-Care/Data_sheet.xls");
      Workbook w=Workbook.getWorkbook(fi);
      Sheet sh = w.getSheet(0);

      for(int rows=0;rows<sh.getRows();rows++)
      {
      for(int cols=0;cols<sh.getColumns();cols++)
      {
      driver.findElement(By.id("_58_login")).clear();
      driver.findElement(By.id("_58_login")).sendKeys(sh.getCell(cols, rows).getContents().trim());
      Thread.sleep(1000);

      driver.findElement(By.id("_58_password")).clear();
      driver.findElement(By.id("_58_password")).sendKeys(sh.getCell(++cols, rows).getContents().trim());
      Thread.sleep(1000);
      }
      driver.findElement(By.id("_58_rememberMeCheckbox")).click();
      driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/div/div/div/div/div/form/div/span/span/input")).click();
      Thread.sleep(2000);
      //This is for writing image or snapshots
      File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      org.apache.commons.io.FileUtils.copyFile(scrFile, new File("D:/InvalidValidLogin"+ ++k+".png")); //Data_sheet.xls
      }

      }



      //This is for releasing the driver connection
      @AfterTest(description=" : This is for Releasing the driver")
      public void releaseDriver() throws Exception
      {
      driver.quit();
      }
      }

      Delete
  13. Hi i am facing error "Source not found", when i debug my script.
    Please reply ASAP thanks in advance

    ReplyDelete
  14. when i changed my string path like C:\\data.xls
    iam getting below error

    ava.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
    at scripts.ReadXLS.getData(ReadXLS.java:22)
    at scripts.Sampletestdriven.test1(Sampletestdriven.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 26 more

    ReplyDelete
  15. I am getting error as "Editor does not contain a main type" Please help

    ReplyDelete
  16. XSSFWorkbook workbook =new XSSFWorkbook(fa); shown remove argument error in this line

    ReplyDelete
  17. Hi
    I am new to selenium and i cannot run the above code as there is no main function , so can you provide proper steps for that.

    ReplyDelete
    Replies
    1. Hello Atul,
      For execution you need to run TestNG not main method.

      You can follow below URL for setup and execution:
      http://roadtoautomation.blogspot.in/2013/02/start-with-java-webdriver-how-to-create.html

      Delete
  18. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hi Every one,

      I am getting below error after running above programs, please help me to resolve this:

      java.lang.ArrayIndexOutOfBoundsException: 3
      at test.ReadXLS.getData(ReadXLS.java:34)
      at test.SearchCountry.testSearchCountry(SearchCountry.java:33)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
      at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
      at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
      at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
      at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
      at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
      at org.testng.TestRunner.privateRun(TestRunner.java:767)
      at org.testng.TestRunner.run(TestRunner.java:617)
      at org.testng.SuiteRunner.runTest(SuiteRunner.java:336)
      at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
      at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
      at org.testng.SuiteRunner.run(SuiteRunner.java:240)
      at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:64)
      at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
      at org.testng.TestNG.runSuitesSequentially(TestNG.java:1225)
      at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
      at org.testng.TestNG.run(TestNG.java:1057)
      at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
      at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:215)
      at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

      Delete
  19. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. *Previous comment deleted due to typo's

      Hi,
      I have tried to implement this code as detailed above. I am fairly new to Webdriver, so I have had not changed anything other then the spreadsheet name and workbook name.

      When I run the script it fails to run. When I debug it falls over at the line:
      XSSFWorkbook workbook = new XSSFWorkbook(fis);

      Before executing this line, the 'fis' variable has the following value:
      fis = FileInputStream (id=58)

      When I step into the line XSSFWorkbook workbook = new XSSFWorkbook(fis); in debug mode I get the following error
      "Source not found." and in the Variables window it has the following values:
      Name - Value
      this - ClassNotFoundException (id=73)
      arg0 - org.apache.poi.xssf.usermodel.XSSFWorkbook
      arg1 - null

      I can't work out what might be causing the script to fail. Has anyone got any suggestions?

      Thanks

      Simon

      Delete
  20. showing an error...


    java.lang.RuntimeException: java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:161)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:429)
    at org.testng.internal.Invoker.handleParameters(Invoker.java:1383)
    at org.testng.internal.Invoker.createParameters(Invoker.java:1075)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
    Caused by: java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
    at datadrivenfromexel.getData(datadrivenfromexel.java:23)
    at datadrivenfromexel2.createData1(datadrivenfromexel2.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:135)
    ... 20 more
    Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 28 more

    ReplyDelete
  21. Hello,

    I used the same script. but getting error


    log4j:WARN No appenders could be found for logger (org.openqa.selenium.server.SeleniumServer).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    23:16:05.175 INFO - Default driver org.openqa.selenium.ie.InternetExplorerDriver registration is skipped: registration capabilities Capabilities [{platform=WINDOWS, ensureCleanSession=true, browserName=internet explorer, version=}] does not match with current platform: LINUX
    FAILED: testSearchCountry
    java.lang.NullPointerException
    at com.bobcares.webdriver.branding.LoginAttemps.testSearchCountry(LoginAttemps.java:23)
    at com.bobcares.webdriver.branding.Main.testSearchCountry(Main.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)


    ===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0



    In the excel file, I have pasted the following,
    Country Description
    India This article is about the Republic of India
    Brazil Brazil bounded by the Atlantic Ocean on the east
    Canada Canada is a North American country 
    England England is a country that is part of the United Kingdom



    I have a separate file for

    public class LoginAttemps extends Setup {
    private String baseUrl;
    public void testSearchCountry() throws Exception {
    driver.get(baseUrl + "/wiki/Main_Page");
    ReadXLS readXls = new ReadXLS();
    List dataList = readXls.getData();
    for (int i = 1; i < dataList.size(); i++) {
    String[] testCase = new String[5];
    String[] test = (String[]) dataList.get(i);
    String countryName = test[0];
    String countryDesc = test[1];
    driver.findElement(By.id("searchInput")).clear();
    driver.findElement(By.id("searchInput")).sendKeys(countryName);
    driver.findElement(By.id("searchButton")).click();
    String str = driver.findElement(
    By.xpath("//h1[@id='firstHeading']/span")).getText();
    System.out.println(countryDesc);
    Assert.assertTrue(str.contains(countryName));
    }
    }

    }

    ReplyDelete


  22. and the same file,

    package com.bobcares.webdriver.branding;

    import java.io.File;
    import java.io.FileInputStream;
    import java.util.ArrayList;
    import java.util.List;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class ReadXLS {

    public List getData() {

    String path = "/tmp/DataSheet.xlsx";
    List dataList = new ArrayList();
    FileInputStream fis = null;
    try {
    fis = new FileInputStream(new File(path));
    XSSFWorkbook workbook = new XSSFWorkbook(fis);
    XSSFSheet sheet = workbook.getSheet("TestData");
    java.util.Iterator rows = sheet.rowIterator();

    while (rows.hasNext()) {
    XSSFRow row = ((XSSFRow) rows.next());
    // int r=row.getRowNum();
    java.util.Iterator cells = row .cellIterator();
    int i = 1;
    String[] testData= new String[3];
    while (cells.hasNext()) {

    XSSFCell cell = (XSSFCell) cells.next();
    String value = cell.getStringCellValue();
    if (!value.equals(null)) {
    testData [i] = value;
    i++;
    }
    }
    dataList.add(testData);
    }
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    return dataList;
    }

    }


    and from main I call
    @Test()
    public void testSearchCountry() throws Exception {
    InvalidLogin.testSearchCountry();
    }


    Please help,

    Thanks!

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution