How to run data driven testing using testNG in selenium

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://192.168.1.33:8081/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(); // Refferer 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 the error I am getting is this

How to run data driven testing using testNG in selenium

Now why the code is not running with testNG and running perfectly as java application

Train Asked on October 1, 2014 in Selenium WebDriver.

Som, Could you please let us know why you are using @BeforeSuite with Static method?Did you try with different annotation or without static method?.

on October 2, 2014.

Also, requesting you to accept your previous queries answer if they were resolved.

on October 2, 2014.

HiĀ  Sachin,

Thanks buddy for taking good care of this Forum.

Regards,
Lakshay Sharma

on October 2, 2014.
Add Comment
  • 1 Answer(s)
    Best answer

    Hi Som,

    This does not look like a TestNg test case, as I am not sure if TestNg have the main[] method. It looks like you have just added annotations to you old test case. If yes, I would suggest you to use the following pattern:

    
    public class NewTest {
    
    @Test
    
    public void f() {
    
    }
    
    @BeforeMethod
    
    public void beforeMethod() {
    
    }
    @AfterMethod
    
    public void afterMethod() {
    
    }
    }
    
    

    Plus I see that you are calling @Test method from your before method, which is not right, as TestNg would execute it automaticallu after before method, so may you do not need to put @Test annotation, so that TestNg would ignore it. Actually the whole code structure needs to revisit.

    Regards,
    Lakshay Sharma

    Professor Answered on October 2, 2014.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.