0

I am using testNG framework and tried to import data from excel sheet in my local. If I run the code as simple java application then its running but with testNG its not

public class NewTest {
   public String strFirstName ="";
   public String strLastName="";
   public String strEmail="";
   @BeforeSuite
   public static void main (String args[]) throws InterruptedException{
      try {
             // Open the Excel file
             // Access the required test data sheet
             FileInputStream fi = new FileInputStream("E:/som/Book2.xlsx");
             XSSFWorkbook workbook = new XSSFWorkbook(fi);
             XSSFSheet sheet = workbook.getSheetAt(0);

             // Loop through all rows in the sheet
             // Start at row 1 as row 0 is our header row
             for(int count = 0;count<=sheet.getLastRowNum();count++){
             XSSFRow row = sheet.getRow(count);
             System.out.println("Running test case " + row.getCell(0).toString());

             // Run the test for the current test data row
             runtest(row.getCell(0).toString(),row.getCell(1).toString(), 
             row.getCell(2).toString());
          }
          fi.close();
          } catch (IOException e) {
               System.out.println("Test data file not found");
          } 
       }

       @Test
       public static void runtest(String strFirstName, String strLastName, 
          String strEmail) throws InterruptedException {
             System.setProperty("webdriver.chrome.driver",
             "E:/som/chromedriver/chromedriver.exe");
             WebDriver driver = new ChromeDriver();
             driver.get("http://<code>192.168.1.33:8081</code>/serosoft-academia-testing/");
             System.out.println("Page title is: " + driver.getTitle());
             driver.findElement(By.id("button-1055-btnIconEl")).click();
             driver.findElement(By.id("ext-comp-1079-btnInnerEl")).click();
             driver.findElement(By.id("button-1089-btnIconEl")).click();
             driver.findElement(By.id("ext-gen1804")).click(); // Enquiry Type
             Thread.sleep(1000);
             driver.findElement(By.xpath(".//*[@id='boundlist-1534- 
                listEl']/ul/li[1]")).click();
             Thread.sleep(2000);
             driver.findElement(By.id("ext-gen1812")).click(); // Referer type
             Thread.sleep(2000);
             driver.findElement(By.xpath(".//*[@id='boundlist- 
                1536']/div/ul/li[1]")).click();
             Thread.sleep(2000);
             driver.findElement(By.id("ext-gen1822")).click(); // Enquiry Registered
             // by
             Thread.sleep(2000);
             driver.findElement(By.xpath(".//*[@id='boundlist- 
                1538']/div/ul/li[1]")).click();
             Thread.sleep(2000);
             driver.findElement(By.id("textfield-1482-inputEl")).sendKeys(strFirstName); // Enquirer

             // first
             // name
             driver.findElement(By.id("textfield-1485-inputEl")).sendKeys(strLastName); // enquirer

             // last
             // name
             driver.findElement(By.id("ext-gen1825")).click(); // country
             Thread.sleep(2000);
             driver.findElement(By.xpath(".//*[@id='boundlist-1540']/div/ul/li[1]")).click();
             Thread.sleep(2000);
             driver.findElement(By.id("ext-gen1827")).click(); // city
             Thread.sleep(2000);
             driver.findElement(By.xpath(".//*[@id='boundlist-1542']/div/ul/li[1]")).click();
             driver.findElement(By.id("textfield-1497-inputEl")).sendKeys(strEmail); // email

And here is the error I am getting: error pic

  • -1: I can hardly read the source code - can you format it better? – dzieciou Oct 6 '17 at 20:11
2

I can see several problems in your code.

Paramaterized @BeforeSuite method with no data provider. Your @BeforeSuite method (main()) takes a parameter (args), but you have not told TestNG how to provide a value for that. That's what that exception message means.

Calling test methods. When you run under TestNG, your @BeforeSuite method calls your test method. That seems like a problem. TestNG wants to call your test on your behalf. You don't have to call it yourself. And you almost certainly should not call it yourself.

Even if it works, when you call your test method in the @BeforeSuite method, any failure in the test would look like a failed configuration method. Probably not what you want. And certainly not what TestNG wants.

Static @BeforeSuite method. Also, you've put a @BeforeSuite annotation on a static method. I don't know for sure, but I suspect that TestNG does not like that.

How to parameterize a test in TestNG. The proper way to provide data for tests is to use a @DataProvider method. See Section 5.6 Parameters in the TestNG documentation, especially section 5.6.2 about @DataProvider methods.

0

Instead of FileinputStream you can use @DataProvider iin TestNG.This annotation is useful in TestNG to perform DataDriven Testing as below . You can get more details here

Code is:

//Identification of Excel file
@DataProvider(name = "Cust_Reg")
 public Object[][] CustRegistration() throws Exception
 {
 FileInputStream filepath = new FileInputStream("E:\\Automation_Testing\\Test_Data.xls");

Workbook wb = Workbook.getWorkbook(filepath);
 Sheet sheet = wb.getSheet("Cust_Data");

int row = sheet.getRows();
 System.out.println("number of rows"+row);
 int column = sheet.getColumns();
 System.out.println("number of columns"+column);
String Testdata[][] = new String[row-1][column];
int count=0;

for (int i = 1; i < row; i++)
 {
 for (int j = 0; j < column; j++)
 {
 Cell cell = sheet.getCell(j, i);
 Testdata[count][j] = cell.getContents();
 }
count++;
}
 filepath.close();
 return Testdata;
 }
}

Pass the @Dataprovider name in @Test method.it will execute easily

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.